Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New Validation::(min|max)ByteLength() addition
  • Loading branch information
tersmitten committed Dec 16, 2016
1 parent aeb05a5 commit df1d2de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Validation/Validation.php
Expand Up @@ -676,7 +676,7 @@ public static function ip($check, $type = 'both')
}

/**
* 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
Expand All @@ -688,7 +688,7 @@ public static function minLength($check, $min)
}

/**
* 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
Expand All @@ -699,6 +699,30 @@ public static function maxLength($check, $max)
return mb_strlen($check) <= $max;
}

/**
* 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.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -1985,6 +1985,22 @@ public function testMaxLength()
$this->assertFalse(Validation::maxLength('ÆΔΩЖÇ', 3));
}

/**
* 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
*
Expand All @@ -2000,6 +2016,22 @@ public function testMinLength()
$this->assertTrue(Validation::minLength('ÆΔΩЖÇ', 2));
}

/**
* 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
*
Expand Down

0 comments on commit df1d2de

Please sign in to comment.