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

[templates] add shuffle filter #3966

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 89 additions & 0 deletions doc/filters/shuffle.rst
@@ -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".
33 changes: 33 additions & 0 deletions src/Extension/CoreExtension.php
Expand Up @@ -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]),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
new TwigFilter('shuffle', [self::class, 'shuffleFilter'], ['needs_environment' => true]),
new TwigFilter('shuffle', [self::class, 'shuffleFilter'], ['needs_charset' => 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]),
Expand Down Expand Up @@ -920,6 +921,38 @@ public static function reverseFilter(Environment $env, $item, $preserveKeys = fa
return $string;
}

/**
* Shuffle an array, a \Traversable instance, or a string.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Shuffle an array, a \Traversable instance, or a string.
* Shuffles an array, a \Traversable instance, or a string.

* The function does not preserve keys.
*
* @internal
*/
public static function shuffleFilter(Environment $env, mixed $item): mixed
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public static function shuffleFilter(Environment $env, mixed $item): mixed
public static function shuffleFilter(string $charset $item): mixed

For $item, we can be more precise than mixed o the phpdocs.

{
if (\is_string($item)) {
$charset = $env->getCharset();
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/filters/shuffle.test
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests don't really test anything, right?