Add support for logical xor operator

This commit is contained in:
HypeMC
2024-10-01 18:43:07 +02:00
parent 7d74d28e2e
commit 8893944e0c
5 changed files with 42 additions and 1 deletions
+1
View File
@@ -19,6 +19,7 @@
* Deprecate the `sandbox` tag
* Improve the way one can deprecate a Twig callable (use `deprecation_info` instead of the other callable options)
* Add the `enum` function
* Add support for logical `xor` operator
# 3.14.0 (2024-09-09)
+3
View File
@@ -708,6 +708,8 @@ You can combine multiple expressions with the following operators:
* ``and``: Returns true if the left and the right operands are both true.
* ``xor``: Returns true if **either** the left or the right operand is true, but not both.
* ``or``: Returns true if the left or the right operand is true.
* ``not``: Negates a statement.
@@ -958,6 +960,7 @@ Operator Score of precedence Description
============================= =================================== =====================================================
``?:`` 0 Ternary operator, conditional statement
``or`` 10 Logical OR operation between two boolean expressions
``xor`` 12 Logical XOR operation between two boolean expressions
``and`` 15 Logical AND operation between two boolean expressions
``b-or`` 16 Bitwise OR operation on integers
``b-xor`` 17 Bitwise XOR operation on integers
+2
View File
@@ -47,6 +47,7 @@ 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\Binary\XorBinary;
use Twig\Node\Expression\BlockReferenceExpression;
use Twig\Node\Expression\Filter\DefaultFilter;
use Twig\Node\Expression\FunctionNode\EnumCasesFunction;
@@ -302,6 +303,7 @@ final class CoreExtension extends AbstractExtension
],
[
'or' => ['precedence' => 10, 'class' => OrBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
'xor' => ['precedence' => 12, 'class' => XorBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
'and' => ['precedence' => 15, 'class' => AndBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
'b-or' => ['precedence' => 16, 'class' => BitwiseOrBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
'b-xor' => ['precedence' => 17, 'class' => BitwiseXorBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT],
+23
View File
@@ -0,0 +1,23 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* 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 XorBinary extends AbstractBinary
{
public function operator(Compiler $compiler): Compiler
{
return $compiler->raw('xor');
}
}
+13 -1
View File
@@ -1,5 +1,5 @@
--TEST--
Twig supports binary operations (+, -, *, /, ~, %, and, or)
Twig supports binary operations (+, -, *, /, ~, %, and, xor, or)
--TEMPLATE--
{{ 1 + 1 }}
{{ 2 - 1 }}
@@ -16,6 +16,12 @@ Twig supports binary operations (+, -, *, /, ~, %, and, or)
{{ 0 or 0 }}
{{ 0 or 1 and 0 }}
{{ 1 or 0 and 1 }}
{{ 1 xor 1 }}
{{ 1 xor 0 }}
{{ 0 xor 1 }}
{{ 0 xor 0 }}
{{ 0 and 1 or 1 xor 1 }}
{{ 0 and 1 or 0 xor 1 }}
{{ "foo" ~ "bar" }}
{{ foo ~ "bar" }}
{{ "foo" ~ bar }}
@@ -38,6 +44,12 @@ return ['foo' => 'bar', 'bar' => 'foo']
1
1
1
1
1
foobar
barbar