Browse Source

New Validation::(min|max)ByteLength() addition

Mischa ter Smitten 9 years ago
parent
commit
df1d2decf4
2 changed files with 58 additions and 2 deletions
  1. 26 2
      src/Validation/Validation.php
  2. 32 0
      tests/TestCase/Validation/ValidationTest.php

+ 26 - 2
src/Validation/Validation.php

@@ -676,7 +676,7 @@ class Validation
     }
 
     /**
-     * Checks whether the length of a string is greater or equal to a minimal length.
+     * Checks whether the length of a string (in character) is greater or equal to a minimal length.
      *
      * @param string $check The string to test
      * @param int $min The minimal string length
@@ -688,7 +688,7 @@ class Validation
     }
 
     /**
-     * Checks whether the length of a string is smaller or equal to a maximal length..
+     * Checks whether the length of a string (in character) is smaller or equal to a maximal length.
      *
      * @param string $check The string to test
      * @param int $max The maximal string length
@@ -700,6 +700,30 @@ class Validation
     }
 
     /**
+     * Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
+     *
+     * @param string $check The string to test
+     * @param int $min The minimal string length (in bytes)
+     * @return bool Success
+     */
+    public static function minLengthBytes($check, $min)
+    {
+        return strlen($check) >= $min;
+    }
+
+    /**
+     * Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.
+     *
+     * @param string $check The string to test
+     * @param int $max The maximal string length
+     * @return bool Success
+     */
+    public static function maxLengthBytes($check, $max)
+    {
+        return strlen($check) <= $max;
+    }
+
+    /**
      * Checks that a value is a monetary amount.
      *
      * @param string $check Value to check

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

@@ -1986,6 +1986,22 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * maxLengthBytes method
+     *
+     * @return void
+     */
+    public function testMaxLengthBytes()
+    {
+        $this->assertTrue(Validation::maxLengthBytes('ab', 3));
+        $this->assertTrue(Validation::maxLengthBytes('abc', 3));
+        $this->assertTrue(Validation::maxLengthBytes('ÆΔΩЖÇ', 10));
+        $this->assertTrue(Validation::maxLengthBytes('ÆΔΩЖÇ', 11));
+
+        $this->assertFalse(Validation::maxLengthBytes('abcd', 3));
+        $this->assertFalse(Validation::maxLengthBytes('ÆΔΩЖÇ', 9));
+    }
+
+    /**
      * testMinLength method
      *
      * @return void
@@ -2001,6 +2017,22 @@ class ValidationTest extends TestCase
     }
 
     /**
+     * minLengthBytes method
+     *
+     * @return void
+     */
+    public function testMinLengthBytes()
+    {
+        $this->assertFalse(Validation::minLengthBytes('ab', 3));
+        $this->assertFalse(Validation::minLengthBytes('ÆΔΩЖÇ', 11));
+
+        $this->assertTrue(Validation::minLengthBytes('abc', 3));
+        $this->assertTrue(Validation::minLengthBytes('abcd', 3));
+        $this->assertTrue(Validation::minLengthBytes('ÆΔΩЖÇ', 10));
+        $this->assertTrue(Validation::minLengthBytes('ÆΔΩЖÇ', 9));
+    }
+
+    /**
      * testUrl method
      *
      * @return void