-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[templates] add shuffle filter #3966
base: 3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
``shuffle`` | ||
=========== | ||
|
||
The ``shuffle`` filter shuffles a sequence, a mapping, or a string: | ||
|
||
.. code-block:: twig | ||
|
||
{% for user in users|shuffle %} | ||
... | ||
{% endfor %} | ||
|
||
.. caution:: | ||
|
||
The shuffled array does not preserve keys. So if the input had not sequential keys | ||
but indexed keys (using the user id for instance), | ||
it is not the case anymore after shuffling it. | ||
|
||
Example 1: | ||
|
||
.. code-block:: html+twig | ||
|
||
{% set items = [ | ||
'a', | ||
'b', | ||
'c', | ||
] %} | ||
|
||
<ul> | ||
{% for item in items|shuffle %} | ||
<li>{{ item }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
The above example will be rendered as: | ||
|
||
.. code-block:: html | ||
|
||
<ul> | ||
<li>a</li> | ||
<li>c</li> | ||
<li>b</li> | ||
</ul> | ||
|
||
Note, results can also be : | ||
"a, b, c" or "b, a, c" or "b, c, a" or "c, a, b" or "c, b, a". | ||
|
||
Example 2: | ||
|
||
.. code-block:: html+twig | ||
|
||
{% set items = { | ||
'a': 'd', | ||
'b': 'e', | ||
'c': 'f', | ||
} %} | ||
|
||
<ul> | ||
{% for index, item in items|shuffle %} | ||
<li>{{ index }} - {{ item }}</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
The above example will be rendered as: | ||
|
||
.. code-block:: html | ||
|
||
<ul> | ||
<li>0 - d</li> | ||
<li>1 - f</li> | ||
<li>2 - e</li> | ||
</ul> | ||
|
||
Note, results can also be : | ||
"d, e, f" or "e, d, f" or "e, f, d" or "f, d, e" or "f, e, d". | ||
|
||
.. code-block:: html+twig | ||
|
||
{% set string = 'ghi' %} | ||
|
||
<p>{{ string|shuffle }}</p> | ||
|
||
The above example will be rendered as: | ||
|
||
.. code-block:: html | ||
|
||
<p>gih</p> | ||
|
||
Note, results can also be : | ||
"ghi" or "hgi" or "hig" or "igh" or "ihg". |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -222,6 +222,7 @@ public function getFilters(): array | |||||
|
||||||
// string/array filters | ||||||
new TwigFilter('reverse', [self::class, 'reverseFilter'], ['needs_environment' => true]), | ||||||
new TwigFilter('shuffle', [self::class, 'shuffleFilter'], ['needs_environment' => true]), | ||||||
new TwigFilter('length', [self::class, 'lengthFilter'], ['needs_environment' => true]), | ||||||
new TwigFilter('slice', [self::class, 'slice'], ['needs_environment' => true]), | ||||||
new TwigFilter('first', [self::class, 'first'], ['needs_environment' => true]), | ||||||
|
@@ -920,6 +921,38 @@ public static function reverseFilter(Environment $env, $item, $preserveKeys = fa | |||||
return $string; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Shuffle an array, a \Traversable instance, or a string. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* The function does not preserve keys. | ||||||
* | ||||||
* @internal | ||||||
*/ | ||||||
public static function shuffleFilter(Environment $env, mixed $item): mixed | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
For |
||||||
{ | ||||||
if (\is_string($item)) { | ||||||
$charset = $env->getCharset(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed |
||||||
|
||||||
if ('UTF-8' !== $charset) { | ||||||
$item = self::convertEncoding($item, 'UTF-8', $charset); | ||||||
} | ||||||
|
||||||
$item = preg_split('/(?<!^)(?!$)/u', $item, -1); | ||||||
shuffle($item); | ||||||
$item = implode('', $item); | ||||||
|
||||||
if ('UTF-8' !== $charset) { | ||||||
$item = self::convertEncoding($item, $charset, 'UTF-8'); | ||||||
} | ||||||
} | ||||||
|
||||||
if ($item instanceof \Traversable || \is_array($item)) { | ||||||
$item = self::toArray($item, false); | ||||||
shuffle($item); | ||||||
} | ||||||
|
||||||
return $item; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Sorts an array. | ||||||
* | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
"shuffle" filter | ||
--TEMPLATE-- | ||
{{ 'bar'|shuffle|length }} | ||
{{ [3, 1]|shuffle|join()|length }} | ||
{{ ['foo', 'bar']|shuffle|join()|length }} | ||
{{ {'a': 'd', 'b': 'e', 'c': 'f'}|shuffle|join()|length }} | ||
{{ traversable|shuffle|join|length }} | ||
--DATA-- | ||
return ['traversable' => new \ArrayObject([0 => 3, 1 => 2, 2 => 1])] | ||
--EXPECT-- | ||
3 | ||
2 | ||
6 | ||
3 | ||
3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests don't really test anything, right? |
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.