Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Validation/Validation.php
Expand Up @@ -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)
Copy link
Member

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()

{
$count = null;
if (is_array($check1) || $check1 instanceof \Countable) {
$count = count($check1);
} elseif (is_string($check1)) {
$count = mb_strlen($check1);
} elseif (is_int($check1)) {
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation::equals() validates numbers, Validation::lengthBetween() validates string length. This method should only validate stuff that can be safely iterated.

If you think we need more / better methods for strings or integers, you are very welcome to add them.

Copy link
Contributor Author

@burzum burzum Apr 27, 2016

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can do that tomorrow after work

Copy link
Contributor Author

@burzum burzum Apr 28, 2016

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could be extra thorough and include test cases for true and false as well.

$this->assertFalse(Validation::count(new \stdClass(), '==', 0));
}
}