Browse Source

Merge pull request #7258 from cakephp/more-validation

More validation methods
ADmad 10 years ago
parent
commit
4f123219a4
2 changed files with 165 additions and 0 deletions
  1. 63 0
      src/Validation/Validation.php
  2. 102 0
      tests/TestCase/Validation/ValidationTest.php

+ 63 - 0
src/Validation/Validation.php

@@ -1098,6 +1098,69 @@ class Validation
     }
 
     /**
+     * Check that the input value is within the ascii byte range.
+     *
+     * This method will reject all non-string values.
+     *
+     * @param string $value The value to check
+     * @return bool
+     */
+    public static function ascii($value)
+    {
+        if (!is_string($value)) {
+            return false;
+        }
+        return strlen($value) <= mb_strlen($value, 'utf-8');
+    }
+
+    /**
+     * Check that the input value is a utf8 string.
+     *
+     * This method will reject all non-string values.
+     *
+     * # Options
+     *
+     * - `extended` - Disallow bytes higher within the basic multilingual plane.
+     *   MySQL's older utf8 encoding type does not allow characters above
+     *   the basic multilingual plane. Defaults to false.
+     *
+     * @param string $value The value to check
+     * @param array $options An array of options. See above for the supported options.
+     * @return bool
+     */
+    public static function utf8($value, array $options = [])
+    {
+        if (!is_string($value)) {
+            return false;
+        }
+        $options += ['extended' => false];
+        if ($options['extended']) {
+            return true;
+        }
+        return preg_match('/[\x{10000}-\x{10FFFF}]/u', $value) === 0;
+    }
+
+    /**
+     * Check that the input value is an integer
+     *
+     * This method will accept strings that contain only integer data
+     * as well.
+     *
+     * @param string $value The value to check
+     * @return bool
+     */
+    public static function isInteger($value)
+    {
+        if (!is_scalar($value) || is_float($value)) {
+            return false;
+        }
+        if (is_int($value)) {
+            return true;
+        }
+        return (bool)preg_match('/^-?[0-9]+$/', $value);
+    }
+
+    /**
      * Converts an array representing a date or datetime into a ISO string.
      * The arrays are typically sent for validation from a form generated by
      * the CakePHP FormHelper.

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

@@ -2564,4 +2564,106 @@ class ValidationTest extends TestCase
         $this->assertTrue(Validation::longitude('10.451526'));
         $this->assertFalse(Validation::longitude('-190.52236'));
     }
+
+    /**
+     * Test isInteger
+     *
+     * @return void
+     */
+    public function testIsInteger()
+    {
+        $this->assertTrue(Validation::isInteger(-10));
+        $this->assertTrue(Validation::isInteger(0));
+        $this->assertTrue(Validation::isInteger(10));
+        $this->assertTrue(Validation::isInteger('-10'));
+        $this->assertTrue(Validation::isInteger('0'));
+        $this->assertTrue(Validation::isInteger('10'));
+
+        $this->assertFalse(Validation::isInteger('2.5'));
+        $this->assertFalse(Validation::isInteger([]));
+        $this->assertFalse(Validation::isInteger(new \StdClass));
+        $this->assertFalse(Validation::isInteger('2 bears'));
+    }
+
+    /**
+     * Test ascii
+     *
+     * @return void
+     */
+    public function testAscii()
+    {
+        $this->assertTrue(Validation::ascii('1 big blue bus.'));
+        $this->assertTrue(Validation::ascii(',.<>[]{;/?\)()'));
+
+        $this->assertFalse(Validation::ascii([]));
+        $this->assertFalse(Validation::ascii(1001));
+        $this->assertFalse(Validation::ascii(3.14));
+        $this->assertFalse(Validation::ascii(new \StdClass));
+
+        // Latin-1 supplement
+        $this->assertFalse(Validation::ascii('some' . "\xc2\x82" . 'value'));
+        $this->assertFalse(Validation::ascii('some' . "\xc3\xbf" . 'value'));
+
+        // End of BMP
+        $this->assertFalse(Validation::ascii('some' . "\xef\xbf\xbd" . 'value'));
+
+        // Start of supplementary multilingual plane
+        $this->assertFalse(Validation::ascii('some' . "\xf0\x90\x80\x80" . 'value'));
+    }
+
+    /**
+     * Test utf8 basic
+     *
+     * @return void
+     */
+    public function testUtf8Basic()
+    {
+        $this->assertFalse(Validation::utf8([]));
+        $this->assertFalse(Validation::utf8(1001));
+        $this->assertFalse(Validation::utf8(3.14));
+        $this->assertFalse(Validation::utf8(new \StdClass));
+        $this->assertTrue(Validation::utf8('1 big blue bus.'));
+        $this->assertTrue(Validation::utf8(',.<>[]{;/?\)()'));
+
+        // Latin-1 supplement
+        $this->assertTrue(Validation::utf8('some' . "\xc2\x82" . 'value'));
+        $this->assertTrue(Validation::utf8('some' . "\xc3\xbf" . 'value'));
+
+        // End of BMP
+        $this->assertTrue(Validation::utf8('some' . "\xef\xbf\xbd" . 'value'));
+
+        // Start of supplementary multilingual plane
+        $this->assertFalse(Validation::utf8('some' . "\xf0\x90\x80\x80" . 'value'));
+
+        // Grinning face
+        $this->assertFalse(Validation::utf8('some' . "\xf0\x9f\x98\x80" . 'value'));
+    }
+
+    /**
+     * Test utf8 extended
+     *
+     * @return void
+     */
+    public function testUtf8Extended()
+    {
+        $this->assertFalse(Validation::utf8([], ['extended' => true]));
+        $this->assertFalse(Validation::utf8(1001, ['extended' => true]));
+        $this->assertFalse(Validation::utf8(3.14, ['extended' => true]));
+        $this->assertFalse(Validation::utf8(new \StdClass, ['extended' => true]));
+        $this->assertTrue(Validation::utf8('1 big blue bus.', ['extended' => true]));
+        $this->assertTrue(Validation::utf8(',.<>[]{;/?\)()', ['extended' => true]));
+
+        // Latin-1 supplement
+        $this->assertTrue(Validation::utf8('some' . "\xc2\x82" . 'value', ['extended' => true]));
+        $this->assertTrue(Validation::utf8('some' . "\xc3\xbf" . 'value', ['extended' => true]));
+
+        // End of BMP
+        $this->assertTrue(Validation::utf8('some' . "\xef\xbf\xbd" . 'value', ['extended' => true]));
+
+        // Start of supplementary multilingual plane
+        $this->assertTrue(Validation::utf8('some' . "\xf0\x90\x80\x80" . 'value', ['extended' => true]));
+
+        // Grinning face
+        $this->assertTrue(Validation::utf8('some' . "\xf0\x9f\x98\x80" . 'value', ['extended' => true]));
+    }
 }