Expose "length" filter, make it work with Countable objects#103
Closed
rundef wants to merge 4 commits intonette:masterfrom
rundef:master
Closed
Expose "length" filter, make it work with Countable objects#103rundef wants to merge 4 commits intonette:masterfrom rundef:master
rundef wants to merge 4 commits intonette:masterfrom
rundef:master
Conversation
src/Latte/Runtime/Filters.php
Outdated
| public static function length($s) | ||
| { | ||
| return strlen(utf8_decode($s)); // fastest way | ||
| return is_string($s) ? |
Contributor
There was a problem hiding this comment.
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
}
}
Member
There was a problem hiding this comment.
if (is_array($val) || $val instanceof \Countable)
Contributor
|
A test would be nice ;-) |
Contributor
Author
|
Thanks for the quick feedback :) |
Member
|
Thanks, merged |
| if (is_array($s) || $s instanceof \Countable) { | ||
| return count($s); | ||
| } elseif ($s instanceof \Traversable) { | ||
| return iterator_count($s); |
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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!