mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Add type check for stringLength
Return 0 if $value is not string
This commit is contained in:
parent
29c7d35db6
commit
28254e76c2
@ -244,7 +244,7 @@ class Validator
|
|||||||
return $length >= $params[0] && $length <= $params[1];
|
return $length >= $params[0] && $length <= $params[1];
|
||||||
}
|
}
|
||||||
// Length same
|
// Length same
|
||||||
return $length == $params[0];
|
return ($length !== false) && $length == $params[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -259,7 +259,7 @@ class Validator
|
|||||||
{
|
{
|
||||||
$length = $this->stringLength($value);
|
$length = $this->stringLength($value);
|
||||||
|
|
||||||
return $length >= $params[0] && $length <= $params[1];
|
return ($length !== false) && $length >= $params[0] && $length <= $params[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -273,7 +273,9 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
protected function validateLengthMin($field, $value, $params)
|
protected function validateLengthMin($field, $value, $params)
|
||||||
{
|
{
|
||||||
return $this->stringLength($value) >= $params[0];
|
$length = $this->stringLength($value);
|
||||||
|
|
||||||
|
return ($length !== false) && $length >= $params[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -287,18 +289,22 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
protected function validateLengthMax($field, $value, $params)
|
protected function validateLengthMax($field, $value, $params)
|
||||||
{
|
{
|
||||||
return $this->stringLength($value) <= $params[0];
|
$length = $this->stringLength($value);
|
||||||
|
|
||||||
|
return ($length !== false) && $length <= $params[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the length of a string
|
* Get the length of a string
|
||||||
*
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return int
|
* @return int|false
|
||||||
*/
|
*/
|
||||||
protected function stringLength($value)
|
protected function stringLength($value)
|
||||||
{
|
{
|
||||||
if (function_exists('mb_strlen')) {
|
if (!is_string($value)) {
|
||||||
|
return false;
|
||||||
|
} elseif (function_exists('mb_strlen')) {
|
||||||
return mb_strlen($value);
|
return mb_strlen($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -131,6 +131,14 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v = new Validator(array('str' => 'sad'));
|
$v = new Validator(array('str' => 'sad'));
|
||||||
$v->rule('length', 'str', 6);
|
$v->rule('length', 'str', 6);
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
|
|
||||||
|
$v = new Validator(array('test' => array()));
|
||||||
|
$v->rule('length', 'test', 1);
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
|
||||||
|
$v = new Validator(array('test' => new stdClass));
|
||||||
|
$v->rule('length', 'test', 1);
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLengthBetweenValid()
|
public function testLengthBetweenValid()
|
||||||
@ -145,6 +153,14 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v = new Validator(array('str' => 'sad'));
|
$v = new Validator(array('str' => 'sad'));
|
||||||
$v->rule('lengthBetween', 'str', 4, 10);
|
$v->rule('lengthBetween', 'str', 4, 10);
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
|
|
||||||
|
$v = new Validator(array('test' => array()));
|
||||||
|
$v->rule('lengthBetween', 'test', 50, 60);
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
|
||||||
|
$v = new Validator(array('test' => new stdClass));
|
||||||
|
$v->rule('lengthBetween', 'test', 99, 100);
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLengthMinValid()
|
public function testLengthMinValid()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user