Merge pull request #137 from zonuexe/feature/deny_array

type check
This commit is contained in:
Vance Lucas 2015-11-24 11:20:47 -06:00
commit a06309ce75
2 changed files with 78 additions and 8 deletions

View File

@ -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);
} }
@ -316,7 +322,9 @@ class Validator
*/ */
protected function validateMin($field, $value, $params) protected function validateMin($field, $value, $params)
{ {
if (function_exists('bccomp')) { if (!is_numeric($value)) {
return false;
} elseif (function_exists('bccomp')) {
return !(bccomp($params[0], $value, 14) == 1); return !(bccomp($params[0], $value, 14) == 1);
} else { } else {
return $params[0] <= $value; return $params[0] <= $value;
@ -334,7 +342,9 @@ class Validator
*/ */
protected function validateMax($field, $value, $params) protected function validateMax($field, $value, $params)
{ {
if (function_exists('bccomp')) { if (!is_numeric($value)) {
return false;
} elseif (function_exists('bccomp')) {
return !(bccomp($value, $params[0], 14) == 1); return !(bccomp($value, $params[0], 14) == 1);
} else { } else {
return $params[0] >= $value; return $params[0] >= $value;

View File

@ -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()
@ -206,6 +222,14 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('num' => 5)); $v = new Validator(array('num' => 5));
$v->rule('min', 'num', 6); $v->rule('min', 'num', 6);
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
$v = new Validator(array('test' => array()));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
$v = new Validator(array('test' => new stdClass));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
} }
public function testMinInvalidFloat() public function testMinInvalidFloat()
@ -246,6 +270,14 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('num' => 5)); $v = new Validator(array('num' => 5));
$v->rule('max', 'num', 4); $v->rule('max', 'num', 4);
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
$v = new Validator(array('test' => array()));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
$v = new Validator(array('test' => new stdClass));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
} }
public function testMaxInvalidFloat() public function testMaxInvalidFloat()
@ -1025,6 +1057,34 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
/**
* @dataProvider dataProviderFor_testError
*/
public function testError($expected, $input, $test, $message)
{
$v = new Validator(array('test' => $input));
$v->error('test', $message, $test);
$this->assertEquals(array('test' => array($expected)), $v->errors());
}
public function dataProviderFor_testError()
{
return array(
array(
'expected' => 'Test must be at least 140 long',
'input' => 'tweeet',
'test' => array(140),
'message' => '{field} must be at least %d long',
),
array(
'expected' => 'Test must be between 1 and 140 characters',
'input' => array(1, 2, 3),
'test' => array(1, 140),
'message' => 'Test must be between %d and %d characters',
),
);
}
} }
function sampleFunctionCallback($field, $value, array $params) { function sampleFunctionCallback($field, $value, array $params) {