Skip to content

Commit

Permalink
minor #2469 Length filter non-mb_string on 7.x fix. (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

Length filter non-mb_string on 7.x fix.

follow up on #2462
after #2464 (comment)

Commits
-------

3aa2e88 Length filter non-mb_string on 7.x fix.
  • Loading branch information
fabpot committed May 12, 2017
2 parents 97d741a + 3aa2e88 commit 860f801
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,10 @@ function twig_capitalize_string_filter(Twig_Environment $env, $string)
*/
function twig_length_filter(Twig_Environment $env, $thing)
{
if (null === $thing) {
return 0;
}

if (is_scalar($thing)) {
return strlen($thing);
}
Expand All @@ -1371,7 +1375,11 @@ function twig_length_filter(Twig_Environment $env, $thing)
return strlen((string) $thing);
}

return count($thing);
if ($thing instanceof \Countable || is_array($thing)) {
return count($thing);
}

return 1;
}

/**
Expand Down

0 comments on commit 860f801

Please sign in to comment.