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

Adding a validation method to validate the count of a value #8716

wants to merge 6 commits into from

Conversation

burzum
Copy link
Contributor

@burzum burzum commented Apr 27, 2016

Used to check the count of a given value of type string, int, array or countable. If a string value is passed the string length is used as count.

@burzum burzum self-assigned this Apr 27, 2016
@markstory markstory added this to the 3.2.9 milestone Apr 27, 2016
$count = $check1;
}

if (!$count) {
Copy link
Member

Choose a reason for hiding this comment

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

What if count is 0 and you wanted to ensure the count was 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@burzum
Copy link
Contributor Author

burzum commented Apr 27, 2016

@dereuromark how is the null check you propose better than isset() here?

@dereuromark
Copy link
Member

dereuromark commented Apr 27, 2016

@burzum What you just did is an anti-pattern. See the discussion about wrong usage of isset and !empty() cloaking possible typos etc. Always prefer non-cloaking check if possible, and in this case it is by simply declaring $count = null first.
The alternative could be to return early on else probably.

@burzum
Copy link
Contributor Author

burzum commented Apr 27, 2016

@dereuromark

        if ($count === null) {
            return false;
        }

results in Undefined variable: count as I expected... I've added $count = null; on top.

@dereuromark
Copy link
Member

dereuromark commented Apr 27, 2016

👍
The only think I ask myself is, do people understand that Validation::count() is a validation method of "count" and returns a boolean decision - or do they think this counts the validation rules or sth and returns an integer?
"isCount" as name might prevent this misleading. But it is just a question at this point.

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

$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

@dereuromark
Copy link
Member

Even better now 👍

@burzum
Copy link
Contributor Author

burzum commented Apr 28, 2016

@dereuromark well I consider it a huge step back. See my comment on the change. #8716 (comment)

*/
public static function numElements($check1, $operator, $expectedCount)
{
if (!is_array($check1) || $check1 instanceof \Countable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

! is needed before $check1 instanceof \Countable. Adding ArrayObject test might be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants