From c4c27018bccde76ce3955b36430febd1786e62c5 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Wed, 4 Nov 2015 22:00:18 -0500 Subject: [PATCH] Removing old Validation class syntax. Better pass the parameters than arrays Using is_scalar instead in some Validation methods as it makes it more flexible --- src/Validation/Validation.php | 70 ++++--------------- tests/TestCase/Validation/ValidationTest.php | 71 +++++--------------- 2 files changed, 30 insertions(+), 111 deletions(-) diff --git a/src/Validation/Validation.php b/src/Validation/Validation.php index 6af752f9bcb..1ab05f52d31 100644 --- a/src/Validation/Validation.php +++ b/src/Validation/Validation.php @@ -82,10 +82,6 @@ public static function notEmpty($check) */ public static function notBlank($check) { - if (is_array($check)) { - extract(static::_defaults($check)); - } - if (empty($check) && $check !== '0') { return false; } @@ -105,10 +101,6 @@ public static function notBlank($check) */ public static function alphaNumeric($check) { - if (is_array($check)) { - extract(static::_defaults($check)); - } - if (empty($check) && $check !== '0') { return false; } @@ -127,6 +119,9 @@ public static function alphaNumeric($check) */ public static function lengthBetween($check, $min, $max) { + if (!is_string($check)) { + return false; + } $length = mb_strlen($check); return ($length >= $min && $length <= $max); } @@ -145,9 +140,6 @@ public static function lengthBetween($check, $min, $max) public static function blank($check) { trigger_error('Validation::blank() is deprecated.', E_USER_DEPRECATED); - if (is_array($check)) { - extract(static::_defaults($check)); - } return !static::_check($check, '/[^\\s]/'); } @@ -166,8 +158,8 @@ public static function blank($check) */ public static function cc($check, $type = 'fast', $deep = false, $regex = null) { - if (is_array($check)) { - extract(static::_defaults($check)); + if (!is_scalar($check)) { + return false; } $check = str_replace(['-', ' '], '', $check); @@ -177,7 +169,7 @@ public static function cc($check, $type = 'fast', $deep = false, $regex = null) if ($regex !== null) { if (static::_check($check, $regex)) { - return static::luhn($check, $deep); + return !$deep || static::luhn($check, $deep); } } $cards = [ @@ -228,7 +220,7 @@ public static function cc($check, $type = 'fast', $deep = false, $regex = null) /** * Used to compare 2 numeric values. * - * @param string|array $check1 if string is passed for, a string must also be passed for $check2 + * @param string $check1 if string is passed for, a string must also be passed for $check2 * used as an array it must be passed as ['check1' => value, 'operator' => 'value', 'check2' => value] * @param string $operator Can be either a word or operand * is greater >, is less <, greater or equal >= @@ -236,11 +228,8 @@ public static function cc($check, $type = 'fast', $deep = false, $regex = null) * @param int $check2 only needed if $check1 is a string * @return bool Success */ - public static function comparison($check1, $operator = null, $check2 = null) + public static function comparison($check1, $operator, $check2) { - if (is_array($check1)) { - extract($check1, EXTR_OVERWRITE); - } if ((float)$check1 != $check1) { return false; } @@ -318,7 +307,7 @@ public static function compareWith($check, $field, $context) */ public static function containsNonAlphaNumeric($check, $count = 1) { - if (!is_string($check)) { + if (!is_scalar($check)) { return false; } @@ -336,9 +325,6 @@ public static function containsNonAlphaNumeric($check, $count = 1) */ public static function custom($check, $regex = null) { - if (is_array($check)) { - extract(static::_defaults($check)); - } if ($regex === null) { static::$errors[] = 'You must define a regular expression for Validation::custom()'; return false; @@ -548,8 +534,8 @@ public static function decimal($check, $places = null, $regex = null) */ public static function email($check, $deep = false, $regex = null) { - if (is_array($check)) { - extract(static::_defaults($check)); + if (!is_string($check)) { + return false; } if ($regex === null) { @@ -854,31 +840,7 @@ public static function uuid($check) */ protected static function _check($check, $regex) { - return is_string($regex) && preg_match($regex, $check); - } - - /** - * Get the values to use when value sent to validation method is - * an array. - * - * @param array $params Parameters sent to validation method - * @return array - */ - protected static function _defaults($params) - { - static::_reset(); - $defaults = [ - 'check' => null, - 'regex' => null, - 'country' => null, - 'deep' => false, - 'type' => null - ]; - $params += $defaults; - if ($params['country'] !== null) { - $params['country'] = mb_strtolower($params['country']); - } - return $params; + return is_string($regex) && is_scalar($check) && preg_match($regex, $check); } /** @@ -891,13 +853,7 @@ protected static function _defaults($params) */ public static function luhn($check, $deep = false) { - if (is_array($check)) { - extract(static::_defaults($check)); - } - if ($deep !== true) { - return true; - } - if ((int)$check === 0) { + if (!is_scalar($check) || (int)$check === 0) { return false; } $sum = 0; diff --git a/tests/TestCase/Validation/ValidationTest.php b/tests/TestCase/Validation/ValidationTest.php index 9bba5bc5764..c3a9e3abf72 100644 --- a/tests/TestCase/Validation/ValidationTest.php +++ b/tests/TestCase/Validation/ValidationTest.php @@ -122,17 +122,18 @@ public function testAlphaNumeric() */ public function testAlphaNumericPassedAsArray() { - $this->assertTrue(Validation::alphaNumeric(['check' => 'frferrf'])); - $this->assertTrue(Validation::alphaNumeric(['check' => '12234'])); - $this->assertTrue(Validation::alphaNumeric(['check' => '1w2e2r3t4y'])); - $this->assertTrue(Validation::alphaNumeric(['check' => '0'])); - $this->assertFalse(Validation::alphaNumeric(['check' => '12 234'])); - $this->assertFalse(Validation::alphaNumeric(['check' => 'dfd 234'])); - $this->assertFalse(Validation::alphaNumeric(['check' => "\n"])); - $this->assertFalse(Validation::alphaNumeric(['check' => "\t"])); - $this->assertFalse(Validation::alphaNumeric(['check' => "\r"])); - $this->assertFalse(Validation::alphaNumeric(['check' => ' '])); - $this->assertFalse(Validation::alphaNumeric(['check' => ''])); + $this->assertTrue(Validation::alphaNumeric('frferrf')); + $this->assertTrue(Validation::alphaNumeric('12234')); + $this->assertTrue(Validation::alphaNumeric('1w2e2r3t4y')); + $this->assertTrue(Validation::alphaNumeric('0')); + $this->assertFalse(Validation::alphaNumeric('12 234')); + $this->assertFalse(Validation::alphaNumeric('dfd 234')); + $this->assertFalse(Validation::alphaNumeric("\n")); + $this->assertFalse(Validation::alphaNumeric("\t")); + $this->assertFalse(Validation::alphaNumeric("\r")); + $this->assertFalse(Validation::alphaNumeric(' ')); + $this->assertFalse(Validation::alphaNumeric('')); + $this->assertFalse(Validation::alphaNumeric(['foo'])); } /** @@ -657,9 +658,8 @@ public function testLuhn() */ public function testCustomRegexForCc() { - $this->assertTrue(Validation::cc('12332105933743585', null, null, '/123321\\d{11}/')); - $this->assertFalse(Validation::cc('1233210593374358', null, null, '/123321\\d{11}/')); - $this->assertFalse(Validation::cc('12312305933743585', null, null, '/123321\\d{11}/')); + $this->assertTrue(Validation::cc('370482756063980', null, false, '/123321\\d{11}/')); + $this->assertFalse(Validation::cc('1233210593374358', null, false, '/123321\\d{11}/')); } /** @@ -864,43 +864,6 @@ public function testComparisonTypeChecks() $this->assertFalse(Validation::comparison('0x02', '>=', 1.5), 'hex string data fails'); } - /** - * testComparisonAsArray method - * - * @return void - */ - public function testComparisonAsArray() - { - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'is greater', 'check2' => 6])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '>', 'check2' => 6])); - $this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => 'is less', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => '<', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'greater or equal', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '>=', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'greater or equal', 'check2' => 6])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '>=', 'check2' => 6])); - $this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => 'less or equal', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => '<=', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'equal to', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '==', 'check2' => 7])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'not equal', 'check2' => 6])); - $this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '!=', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => 'is greater', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => '>', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'is less', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '<', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => 'greater or equal', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => '>=', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => 'greater or equal', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => '>=', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'less or equal', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '<=', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'equal to', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '==', 'check2' => 6])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'not equal', 'check2' => 7])); - $this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '!=', 'check2' => 7])); - } - /** * testCustom method * @@ -921,9 +884,9 @@ public function testCustom() */ public function testCustomAsArray() { - $this->assertTrue(Validation::custom(['check' => '12345', 'regex' => '/(?assertFalse(Validation::custom(['check' => 'Text', 'regex' => '/(?assertFalse(Validation::custom(['check' => '123.45', 'regex' => '/(?assertTrue(Validation::custom('12345', '/(?assertFalse(Validation::custom('Text', '/(?assertFalse(Validation::custom('123.45', '/(?