Merge pull request #155 from misantron/validators-patch

Contains and between validators
This commit is contained in:
Willem Wollebrants 2016-05-22 14:02:59 +02:00
commit e55681c9ab
3 changed files with 102 additions and 12 deletions

View File

@ -1,16 +1,14 @@
<?php <?php
namespace Valitron; namespace Valitron;
use InvalidArgumentException;
/** /**
* Validation Class * Validation Class
* *
* Validates input against certain criteria * Validates input against certain criteria
* *
* @package Valitron * @package Valitron
* @author Vance Lucas <vance@vancelucas.com> * @author Vance Lucas <vance@vancelucas.com>
* @link http://www.vancelucas.com/ * @link http://www.vancelucas.com/
*/ */
class Validator class Validator
{ {
@ -91,7 +89,7 @@ class Validator
$langMessages = include $langFile; $langMessages = include $langFile;
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages); static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
} else { } else {
throw new \InvalidArgumentException("fail to load language file '$langFile'"); throw new \InvalidArgumentException("Fail to load language file '" . $langFile . "'");
} }
} }
@ -325,7 +323,7 @@ class Validator
if (!is_numeric($value)) { if (!is_numeric($value)) {
return false; return false;
} elseif (function_exists('bccomp')) { } 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;
} }
@ -345,12 +343,35 @@ class Validator
if (!is_numeric($value)) { if (!is_numeric($value)) {
return false; return false;
} elseif (function_exists('bccomp')) { } 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;
} }
} }
/**
* Validate the size of a field is between min and max values
*
* @param string $field
* @param mixed $value
* @param array $params
* @return bool
*/
protected function validateBetween($field, $value, $params)
{
if (!is_numeric($value)) {
return false;
}
if (!isset($params[0]) || !is_array($params[0]) || count($params[0]) !== 2) {
return false;
}
list($min, $max) = $params[0];
return $this->validateMin($field, $value, array($min)) && $this->validateMax($field, $value, array($max));
}
/** /**
* Validate a field is contained within a list of values * Validate a field is contained within a list of values
* *
@ -406,7 +427,26 @@ class Validator
return false; return false;
} }
return (strpos($value, $params[0]) !== false); $strict = true;
if (isset($params[1])) {
$strict = (bool) $params[1];
}
$isContains = false;
if ($strict) {
if (function_exists('mb_strpos')) {
$isContains = mb_strpos($value, $params[0]) !== false;
} else {
$isContains = strpos($value, $params[0]) !== false;
}
} else {
if (function_exists('mb_stripos')) {
$isContains = mb_stripos($value, $params[0]) !== false;
} else {
$isContains = stripos($value, $params[0]) !== false;
}
}
return $isContains;
} }
/** /**

View File

@ -44,7 +44,7 @@ class LangTest extends BaseTestCase
/** /**
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
* @expectedExceptionMessage fail to load language file '/this/dir/does/not/exists/en.php' * @expectedExceptionMessage Fail to load language file '/this/dir/does/not/exists/en.php'
*/ */
public function testLangException() public function testLangException()
{ {

View File

@ -287,6 +287,42 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testBetweenValid()
{
$v = new Validator(array('num' => 5));
$v->rule('between', 'num', array(3, 7));
$this->assertTrue($v->validate());
}
public function testBetweenInvalid()
{
$v = new Validator(array('num' => 3));
$v->rule('between', 'num', array(5, 10));
$this->assertFalse($v->validate());
}
public function testBetweenInvalidValue()
{
$v = new Validator(array('num' => array(3)));
$v->rule('between', 'num', array(5, 10));
$this->assertFalse($v->validate());
}
public function testBetweenInvalidRange()
{
$v = new Validator(array('num' => 3));
$v->rule('between', 'num');
$this->assertFalse($v->validate());
$v = new Validator(array('num' => 3));
$v->rule('between', 'num', 5);
$this->assertFalse($v->validate());
$v = new Validator(array('num' => 3));
$v->rule('between', 'num', array(5));
$this->assertFalse($v->validate());
}
public function testInValid() public function testInValid()
{ {
$v = new Validator(array('color' => 'green')); $v = new Validator(array('color' => 'green'));
@ -666,8 +702,15 @@ class ValidateTest extends BaseTestCase
public function testContainsValid() public function testContainsValid()
{ {
$v = new Validator(array('test_string' => 'this is a test')); $v = new Validator(array('test_string' => 'this is a Test'));
$v->rule('contains', 'test_string', 'a test'); $v->rule('contains', 'test_string', 'Test');
$this->assertTrue($v->validate());
}
public function testContainsNonStrictValid()
{
$v = new Validator(array('test_string' => 'this is a Test'));
$v->rule('contains', 'test_string', 'test', false);
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
} }
@ -678,6 +721,13 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testContainsStrictNotFound()
{
$v = new Validator(array('test_string' => 'this is a Test'));
$v->rule('contains', 'test_string', 'test');
$this->assertFalse($v->validate());
}
public function testContainsInvalidValue() public function testContainsInvalidValue()
{ {
$v = new Validator(array('test_string' => 'this is a test')); $v = new Validator(array('test_string' => 'this is a test'));
@ -965,7 +1015,7 @@ class ValidateTest extends BaseTestCase
} }
} }
public function testcreditCardInvalid() public function testCreditCardInvalid()
{ {
$visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622); $visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622);
$mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965); $mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965);