New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a validation method to validate the count of a value #8716
Changes from 4 commits
f055942
dae46da
e61f21d
92e6f56
7bec27a
6e907dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,6 +215,36 @@ public static function cc($check, $type = 'fast', $deep = false, $regex = null) | |
return false; | ||
} | ||
|
||
/** | ||
* Used to check the count of a given value of type string, int, or array. | ||
* | ||
* If a string value is passed the string length is used as count. | ||
* | ||
* @param array|int|string $check1 The value to check the count on. | ||
* @param string $operator Can be either a word or operand | ||
* is greater >, is less <, greater or equal >= | ||
* less or equal <=, is less <, equal to ==, not equal != | ||
* @param int $expectedCount The expected count value. | ||
* @return bool Success | ||
*/ | ||
public static function count($check1, $operator, $expectedCount) | ||
{ | ||
$count = null; | ||
if (is_array($check1) || $check1 instanceof \Countable) { | ||
$count = count($check1); | ||
} elseif (is_string($check1)) { | ||
$count = mb_strlen($check1); | ||
} elseif (is_int($check1)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this method should only array, traversable or countable stuff. We already have method for intgers and strings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh where? I think this method is pretty convenient now as it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If you think we need more / better methods for strings or integers, you are very welcome to add them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lorenzo Would you mind to remove it from the PR if you want to get that removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can do that tomorrow after work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I strongly disagree with that but fine, I've removed it. It was just convenient. There is no equals, just equalTo and it does not allow me to take a string and compare the count of it to something. http://api.cakephp.org/3.2/source-class-Cake.Validation.Validation.html#592 In fact it's just a strong typed comparison. lengthBetween requirs a min and max value. http://api.cakephp.org/3.2/source-class-Cake.Validation.Validation.html#121 Both don't do the same my implementation allowed you to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mind adding more methods to the validation if you want this for strings |
||
$count = $check1; | ||
} | ||
|
||
if ($count === null) { | ||
return false; | ||
} | ||
|
||
return self::comparison($count, $operator, $expectedCount); | ||
} | ||
|
||
/** | ||
* Used to compare 2 numeric values. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2762,4 +2762,33 @@ public function testUtf8Extended() | |
// Grinning face | ||
$this->assertTrue(Validation::utf8('some' . "\xf0\x9f\x98\x80" . 'value', ['extended' => true])); | ||
} | ||
|
||
/** | ||
* Test count | ||
* | ||
* @return void | ||
*/ | ||
public function testCount() | ||
{ | ||
$array = ['cake', 'php']; | ||
$this->assertTrue(Validation::count($array, '==', 2)); | ||
$this->assertFalse(Validation::count($array, '>', 3)); | ||
$this->assertFalse(Validation::count($array, '<', 1)); | ||
|
||
$string = 'cakephp'; | ||
$this->assertTrue(Validation::count($string, '==', 7)); | ||
$this->assertFalse(Validation::count($string, '>', 8)); | ||
$this->assertFalse(Validation::count($string, '<', 1)); | ||
|
||
$int = 7; | ||
$this->assertTrue(Validation::count($int, '==', 7)); | ||
$this->assertFalse(Validation::count($int, '>', 8)); | ||
$this->assertFalse(Validation::count($int, '<', 1)); | ||
|
||
$int = 0; | ||
$this->assertTrue(Validation::count($int, '==', 0)); | ||
|
||
$this->assertFalse(Validation::count(null, '==', 0)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could be extra thorough and include test cases for |
||
$this->assertFalse(Validation::count(new \stdClass(), '==', 0)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please rename this to
numElements()