mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 02:46:29 +00:00
added the spaceship operator
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 2.12.0 (2019-XX-XX)
|
||||
|
||||
* added the spaceship operator ("<=>"), useful when using an arrow function in the "sort" filter
|
||||
* added support for an "arrow" function on the "sort" filter
|
||||
* added the CssInliner extension in the "extra" repositories: "inline_css"
|
||||
filter
|
||||
|
||||
@@ -28,15 +28,18 @@ You can pass an arrow function to sort the array:
|
||||
{ name: 'Grapes', quantity: 4 },
|
||||
] %}
|
||||
|
||||
{% for fruit in fruits|sort((a, b) => a.quantity == b.quantity ? 0 : (a.quantity > b.quantity ? 1 : -1))|column('name') %}
|
||||
{% for fruit in fruits|sort((a, b) => a.quantity <=> b.quantity)|column('name') %}
|
||||
{{ fruit }}
|
||||
{% endfor %}
|
||||
|
||||
{# output in this order: Oranges, Grapes, Apples #}
|
||||
|
||||
Note the usage of the `spaceship`_ operator to simplify the comparison.
|
||||
|
||||
Arguments
|
||||
---------
|
||||
|
||||
* ``arrow``: An arrow function
|
||||
|
||||
.. _`asort`: https://secure.php.net/asort
|
||||
.. _`spaceship`: https://www.php.net/manual/en/language.operators.comparison.php
|
||||
|
||||
+3
-3
@@ -509,9 +509,9 @@ Twig allows expressions everywhere.
|
||||
|
||||
The operator precedence is as follows, with the lowest-precedence operators
|
||||
listed first: ``?:`` (ternary operator), ``b-and``, ``b-xor``, ``b-or``,
|
||||
``or``, ``and``, ``==``, ``!=``, ``<``, ``>``, ``>=``, ``<=``, ``in``,
|
||||
``matches``, ``starts with``, ``ends with``, ``..``, ``+``, ``-``, ``~``,
|
||||
``*``, ``/``, ``//``, ``%``, ``is`` (tests), ``**``, ``??``, ``|``
|
||||
``or``, ``and``, ``==``, ``!=``, ``<=>``, ``<``, ``>``, ``>=``, ``<=``,
|
||||
``in``, ``matches``, ``starts with``, ``ends with``, ``..``, ``+``, ``-``,
|
||||
``~``, ``*``, ``/``, ``//``, ``%``, ``is`` (tests), ``**``, ``??``, ``|``
|
||||
(filters), ``[]``, and ``.``:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
@@ -34,6 +34,7 @@ use Twig\Node\Expression\Binary\NotInBinary;
|
||||
use Twig\Node\Expression\Binary\OrBinary;
|
||||
use Twig\Node\Expression\Binary\PowerBinary;
|
||||
use Twig\Node\Expression\Binary\RangeBinary;
|
||||
use Twig\Node\Expression\Binary\SpaceshipBinary;
|
||||
use Twig\Node\Expression\Binary\StartsWithBinary;
|
||||
use Twig\Node\Expression\Binary\SubBinary;
|
||||
use Twig\Node\Expression\Filter\DefaultFilter;
|
||||
@@ -309,6 +310,7 @@ final class CoreExtension extends AbstractExtension
|
||||
'b-and' => ['precedence' => 18, 'class' => BitwiseAndBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
'==' => ['precedence' => 20, 'class' => EqualBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
'!=' => ['precedence' => 20, 'class' => NotEqualBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
'<=>' => ['precedence' => 20, 'class' => SpaceshipBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
'<' => ['precedence' => 20, 'class' => LessBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
'>' => ['precedence' => 20, 'class' => GreaterBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
'>=' => ['precedence' => 20, 'class' => GreaterEqualBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Node\Expression\Binary;
|
||||
|
||||
use Twig\Compiler;
|
||||
|
||||
class SpaceshipBinary extends AbstractBinary
|
||||
{
|
||||
public function operator(Compiler $compiler)
|
||||
{
|
||||
return $compiler->raw('<=>');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
"sort" filter
|
||||
--TEMPLATE--
|
||||
{{ fruits|sort((a, b) => a.quantity == b.quantity ? 0 : (a.quantity > b.quantity ? 1 : -1))|column('name')|join(', ') }}
|
||||
{{ fruits|sort((a, b) => a.quantity <=> b.quantity)|column('name')|join(', ') }}
|
||||
--DATA--
|
||||
return [
|
||||
'fruits' => [
|
||||
@@ -12,3 +13,4 @@ return [
|
||||
]
|
||||
--EXPECT--
|
||||
Oranges, Grapes, Apples
|
||||
Oranges, Grapes, Apples
|
||||
|
||||
Reference in New Issue
Block a user