-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expose "length" filter, make it work with Countable objects #103
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
Conversation
src/Latte/Runtime/Filters.php
Outdated
| public static function length($s) | ||
| { | ||
| return strlen(utf8_decode($s)); // fastest way | ||
| return is_string($s) ? |
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.
imho it should be something like
public static function length($s)
{
if (is_array($s)) {
return count($s);
} elseif ($s instanceof \Traversable) {
return iterator_count($s);
} else {
return strlen(utf8_decode($s)): // fastest way
}
}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.
if (is_array($val) || $val instanceof \Countable)
|
A test would be nice ;-) |
|
Thanks for the quick feedback :) |
|
Thanks, merged |
| if (is_array($s) || $s instanceof \Countable) { | ||
| return count($s); | ||
| } elseif ($s instanceof \Traversable) { | ||
| return iterator_count($s); |
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.
this (may) mutate the iterator, imo it's best to let decide the programmer.
iterator_count() is not guaranteed to retain the current position of the iterator.
http://php.net/iterator_count
I needed to count the number of elements in an array ...
I saw that this feature has been talked about in issue #33
I wasn't sure if I should have created a new filter
|countfor objects only ... Tell me what you think!