From a89c0972c130344fdb489298171b4f0ded606ef1 Mon Sep 17 00:00:00 2001 From: misantron Date: Sat, 26 Mar 2016 00:55:08 +0300 Subject: [PATCH 1/2] Contains validator update, between validator added --- src/Valitron/Validator.php | 56 ++++++++++++++++++++++++++++----- tests/Valitron/LangTest.php | 2 +- tests/Valitron/ValidateTest.php | 54 +++++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 90a9aa1..dbc7d09 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -1,16 +1,14 @@ - * @link http://www.vancelucas.com/ + * @author Vance Lucas + * @link http://www.vancelucas.com/ */ class Validator { @@ -91,7 +89,7 @@ class Validator $langMessages = include $langFile; static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages); } 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)) { return false; } elseif (function_exists('bccomp')) { - return !(bccomp($params[0], $value, 14) == 1); + return !(bccomp($params[0], $value, 14) === 1); } else { return $params[0] <= $value; } @@ -345,12 +343,35 @@ class Validator 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 { 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 * @@ -406,7 +427,26 @@ class Validator return false; } - return (strpos($value, $params[0]) !== false); + $strict = false; + 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; } /** diff --git a/tests/Valitron/LangTest.php b/tests/Valitron/LangTest.php index 7660dba..659d5f1 100644 --- a/tests/Valitron/LangTest.php +++ b/tests/Valitron/LangTest.php @@ -44,7 +44,7 @@ class LangTest extends BaseTestCase /** * @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() { diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 996c4a2..5bbf54f 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -287,6 +287,42 @@ class ValidateTest extends BaseTestCase $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() { $v = new Validator(array('color' => 'green')); @@ -666,11 +702,18 @@ class ValidateTest extends BaseTestCase 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'); $this->assertTrue($v->validate()); } + public function testContainsStrictValid() + { + $v = new Validator(array('test_string' => 'this is a Test')); + $v->rule('contains', 'test_string', 'Test', true); + $this->assertTrue($v->validate()); + } + public function testContainsNotFound() { $v = new Validator(array('test_string' => 'this is a test')); @@ -678,6 +721,13 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testContainsStrictNotFound() + { + $v = new Validator(array('test_string' => 'this is a Test')); + $v->rule('contains', 'test_string', 'test', true); + $this->assertFalse($v->validate()); + } + public function testContainsInvalidValue() { $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); $mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965); From d47c6206edb0c5fd9af9a72be26605052ebcf9b3 Mon Sep 17 00:00:00 2001 From: misantron Date: Sat, 21 May 2016 20:48:01 +0300 Subject: [PATCH 2/2] Change contains validator default mode to strict --- src/Valitron/Validator.php | 2 +- tests/Valitron/ValidateTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index dbc7d09..4496e2d 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -427,7 +427,7 @@ class Validator return false; } - $strict = false; + $strict = true; if (isset($params[1])) { $strict = (bool) $params[1]; } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 5bbf54f..ac3ffca 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -703,14 +703,14 @@ class ValidateTest extends BaseTestCase public function testContainsValid() { $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 testContainsStrictValid() + public function testContainsNonStrictValid() { $v = new Validator(array('test_string' => 'this is a Test')); - $v->rule('contains', 'test_string', 'Test', true); + $v->rule('contains', 'test_string', 'test', false); $this->assertTrue($v->validate()); } @@ -724,7 +724,7 @@ class ValidateTest extends BaseTestCase public function testContainsStrictNotFound() { $v = new Validator(array('test_string' => 'this is a Test')); - $v->rule('contains', 'test_string', 'test', true); + $v->rule('contains', 'test_string', 'test'); $this->assertFalse($v->validate()); }