Browse Source

Add truthy() and falsey() to Validation

Merge branch 'validateIsTrueIsFalse' into 3.next

Refs #9563
Mark Story 9 years ago
parent
commit
620aa6fa36
2 changed files with 128 additions and 6 deletions
  1. 47 6
      src/Validation/Validation.php
  2. 81 0
      tests/TestCase/Validation/ValidationTest.php

+ 47 - 6
src/Validation/Validation.php

@@ -521,16 +521,57 @@ class Validation
     }
 
     /**
-     * Boolean validation, determines if value passed is a boolean integer or true/false.
+     * Validates if passed value is boolean-like.
      *
-     * @param string $check a valid boolean
-     * @return bool Success
+     * The list of what is considered to be boolean values, may be set via $booleanValues.
+     *
+     * @param bool|int|string $check Value to check.
+     * @param string $booleanValues List of valid boolean values, defaults to `[true, false, 0, 1, '0', '1']`.
+     * @return bool Success.
+     */
+    public static function boolean($check, array $booleanValues = [])
+    {
+        if (!$booleanValues) {
+            $booleanValues = [true, false, 0, 1, '0', '1'];
+        }
+
+        return in_array($check, $booleanValues, true);
+    }
+
+    /**
+     * Validates if given value is truthy.
+     *
+     * The list of what is considered to be truthy values, may be set via $truthyValues.
+     *
+     * @param bool|int|string $check Value to check.
+     * @param array $truthyValues List of valid truthy values, defaults to `[true, 1, '1']`.
+     * @return bool Success.
+     */
+    public static function truthy($check, array $truthyValues = [])
+    {
+        if (!$truthyValues) {
+            $truthyValues = [true, 1, '1'];
+        }
+
+        return in_array($check, $truthyValues, true);
+    }
+
+    /**
+     * Validates if given value is falsey.
+     *
+     * The list of what is considered to be falsey values, may be set via $falseyValues.
+     *
+     * @param bool|int|string $check Value to check.
+     * @param array $falseyValues List of valid falsey values, defaults to `[false, 0, '0']`.
+     * @return bool Success.
      */
-    public static function boolean($check)
+    public static function falsey($check, array $falseyValues = [])
     {
-        $booleanList = [0, 1, '0', '1', true, false];
+        if (!$falseyValues) {
+            $falseyValues = [false, 0, '0'];
+        }
 
-        return in_array($check, $booleanList, true);
+        return in_array($check, $falseyValues, true);
     }
 
     /**

+ 81 - 0
tests/TestCase/Validation/ValidationTest.php

@@ -1635,6 +1635,87 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * testBooleanWithOptions method
+     *
+     * @return void
+     */
+    public function testBooleanWithOptions()
+    {
+        $this->assertTrue(Validation::boolean('0', ['0', '1']));
+        $this->assertTrue(Validation::boolean('1', ['0', '1']));
+        $this->assertFalse(Validation::boolean(0, ['0', '1']));
+        $this->assertFalse(Validation::boolean(1, ['0', '1']));
+        $this->assertFalse(Validation::boolean(false, ['0', '1']));
+        $this->assertFalse(Validation::boolean(true, ['0', '1']));
+        $this->assertFalse(Validation::boolean('false', ['0', '1']));
+        $this->assertFalse(Validation::boolean('true', ['0', '1']));
+        $this->assertTrue(Validation::boolean(0, [0, 1]));
+        $this->assertTrue(Validation::boolean(1, [0, 1]));
+    }
+
+    /**
+     * testTruthy method
+     *
+     * @return void
+     */
+    public function testTruthy()
+    {
+        $this->assertTrue(Validation::truthy(1));
+        $this->assertTrue(Validation::truthy(true));
+        $this->assertTrue(Validation::truthy('1'));
+
+        $this->assertFalse(Validation::truthy('true'));
+        $this->assertFalse(Validation::truthy('on'));
+        $this->assertFalse(Validation::truthy('yes'));
+
+        $this->assertFalse(Validation::truthy(0));
+        $this->assertFalse(Validation::truthy(false));
+        $this->assertFalse(Validation::truthy('0'));
+        $this->assertFalse(Validation::truthy('false'));
+
+        $this->assertTrue(Validation::truthy('on', ['on', 'yes', 'true']));
+        $this->assertTrue(Validation::truthy('yes', ['on', 'yes', 'true']));
+        $this->assertTrue(Validation::truthy('true', ['on', 'yes', 'true']));
+
+        $this->assertFalse(Validation::truthy(1, ['on', 'yes', 'true']));
+        $this->assertFalse(Validation::truthy(true, ['on', 'yes', 'true']));
+        $this->assertFalse(Validation::truthy('1', ['on', 'yes', 'true']));
+
+        $this->assertTrue(Validation::truthy('true', ['on', 'yes', 'true']));
+    }
+
+    /**
+     * testTruthy method
+     *
+     * @return void
+     */
+    public function testFalsey()
+    {
+        $this->assertTrue(Validation::falsey(0));
+        $this->assertTrue(Validation::falsey(false));
+        $this->assertTrue(Validation::falsey('0'));
+
+        $this->assertFalse(Validation::falsey('false'));
+        $this->assertFalse(Validation::falsey('off'));
+        $this->assertFalse(Validation::falsey('no'));
+
+        $this->assertFalse(Validation::falsey(1));
+        $this->assertFalse(Validation::falsey(true));
+        $this->assertFalse(Validation::falsey('1'));
+        $this->assertFalse(Validation::falsey('true'));
+
+        $this->assertTrue(Validation::falsey('off', ['off', 'no', 'false']));
+        $this->assertTrue(Validation::falsey('no', ['off', 'no', 'false']));
+        $this->assertTrue(Validation::falsey('false', ['off', 'no', 'false']));
+
+        $this->assertFalse(Validation::falsey(0, ['off', 'no', 'false']));
+        $this->assertFalse(Validation::falsey(false, ['off', 'no', 'false']));
+        $this->assertFalse(Validation::falsey('0', ['off', 'yes', 'false']));
+
+        $this->assertTrue(Validation::falsey('false', ['off', 'no', 'false']));
+    }
+
+    /**
      * testDateCustomRegx method
      *
      * @return void