mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Add instanceOf validation
This commit is contained in:
parent
04c52ae778
commit
ad0f6b9708
@ -667,6 +667,25 @@ class Validator
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function validateInstanceOf($field, $value, $params)
|
||||
{
|
||||
$isInstanceOf = false;
|
||||
if(is_object($value)) {
|
||||
if(is_object($params[0]) && $value instanceof $params[0]) {
|
||||
$isInstanceOf = true;
|
||||
}
|
||||
if(get_class($value) === $params[0]) {
|
||||
$isInstanceOf = true;
|
||||
}
|
||||
}
|
||||
if(is_string($value)) {
|
||||
if(is_string($params[0]) && get_class($value) === $params[0]) {
|
||||
$isInstanceOf = true;
|
||||
}
|
||||
}
|
||||
return $isInstanceOf;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get array of fields and data
|
||||
@ -713,8 +732,10 @@ class Validator
|
||||
$param = $param->format('Y-m-d');
|
||||
}
|
||||
// Use custom label instead of field name if set
|
||||
if (isset($this->_labels[$param])) {
|
||||
$param = $this->_labels[$param];
|
||||
if(is_string($params[0])) {
|
||||
if (isset($this->_labels[$param])) {
|
||||
$param = $this->_labels[$param];
|
||||
}
|
||||
}
|
||||
$values[] = $param;
|
||||
}
|
||||
|
||||
@ -800,6 +800,35 @@ class ValidateTest extends BaseTestCase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testInstanceOfValidWithString()
|
||||
{
|
||||
$v = new Validator(['attributeName' => new stdClass()]);
|
||||
$v->rule('instanceOf', 'attributeName', 'stdClass');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testInstanceOfInvalidWithInstance()
|
||||
{
|
||||
$v = new Validator(['attributeName' => new stdClass()]);
|
||||
$v->rule('instanceOf', 'attributeName', new Validator([]));
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testInstanceOfValidWithInstance()
|
||||
{
|
||||
$v = new Validator(['attributeName' => new stdClass()]);
|
||||
$v->rule('instanceOf', 'attributeName', new stdClass());
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testInstanceOfInvalidWithString()
|
||||
{
|
||||
$v = new Validator(['attributeName' => new stdClass()]);
|
||||
$v->rule('instanceOf', 'attributeName', 'SomeOtherClass');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function sampleFunctionCallback($field, $value, array $params) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user