Browse Source

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
Jose Lorenzo Rodriguez 10 years ago
parent
commit
c4c27018bc
2 changed files with 30 additions and 111 deletions
  1. 13 57
      src/Validation/Validation.php
  2. 17 54
      tests/TestCase/Validation/ValidationTest.php

+ 13 - 57
src/Validation/Validation.php

@@ -82,10 +82,6 @@ class Validation
      */
     public static function notBlank($check)
     {
-        if (is_array($check)) {
-            extract(static::_defaults($check));
-        }
-
         if (empty($check) && $check !== '0') {
             return false;
         }
@@ -105,10 +101,6 @@ class Validation
      */
     public static function alphaNumeric($check)
     {
-        if (is_array($check)) {
-            extract(static::_defaults($check));
-        }
-
         if (empty($check) && $check !== '0') {
             return false;
         }
@@ -127,6 +119,9 @@ class Validation
      */
     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 @@ class Validation
     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 @@ class Validation
      */
     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 @@ class Validation
 
         if ($regex !== null) {
             if (static::_check($check, $regex)) {
-                return static::luhn($check, $deep);
+                return !$deep || static::luhn($check, $deep);
             }
         }
         $cards = [
@@ -228,7 +220,7 @@ class Validation
     /**
      * 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 @@ class Validation
      * @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 @@ class Validation
      */
     public static function containsNonAlphaNumeric($check, $count = 1)
     {
-        if (!is_string($check)) {
+        if (!is_scalar($check)) {
             return false;
         }
 
@@ -336,9 +325,6 @@ class Validation
      */
     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 @@ class Validation
      */
     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 @@ class Validation
      */
     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 @@ class Validation
      */
     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;

+ 17 - 54
tests/TestCase/Validation/ValidationTest.php

@@ -122,17 +122,18 @@ class ValidationTest extends TestCase
      */
     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 @@ class ValidationTest extends TestCase
      */
     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}/'));
     }
 
     /**
@@ -865,43 +865,6 @@ class ValidationTest extends TestCase
     }
 
     /**
-     * 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
      *
      * @return void
@@ -921,9 +884,9 @@ class ValidationTest extends TestCase
      */
     public function testCustomAsArray()
     {
-        $this->assertTrue(Validation::custom(['check' => '12345', 'regex' => '/(?<!\\S)\\d++(?!\\S)/']));
-        $this->assertFalse(Validation::custom(['check' => 'Text', 'regex' => '/(?<!\\S)\\d++(?!\\S)/']));
-        $this->assertFalse(Validation::custom(['check' => '123.45', 'regex' => '/(?<!\\S)\\d++(?!\\S)/']));
+        $this->assertTrue(Validation::custom('12345', '/(?<!\\S)\\d++(?!\\S)/'));
+        $this->assertFalse(Validation::custom('Text', '/(?<!\\S)\\d++(?!\\S)/'));
+        $this->assertFalse(Validation::custom('123.45', '/(?<!\\S)\\d++(?!\\S)/'));
     }
 
     /**