mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-03 05:56:43 +00:00
Add === and !== operators
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# 3.22.2 (2026-XX-XX)
|
||||
|
||||
* n/a
|
||||
* Add `===` and `!==` operators (equivalent to the `same as` and `not same as` tests)
|
||||
|
||||
# 3.22.2 (2025-12-14)
|
||||
|
||||
|
||||
@@ -72,6 +72,10 @@
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| | ``has every`` | | | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| | ``===`` | | | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| | ``!==`` | | | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| 18 | ``b-and`` | infix | Left | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| 17 | ``b-xor`` | infix | Left | |
|
||||
@@ -170,6 +174,10 @@ Here is the same table for Twig 4.0 with adjusted precedences:
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| | ``has every`` | | | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| | ``===`` | | | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| | ``!==`` | | | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| 18 | ``b-and`` | infix | Left | |
|
||||
+------------+------------------+---------+---------------+-------------------------------------------------------------------+
|
||||
| 17 | ``b-xor`` | infix | Left | |
|
||||
|
||||
@@ -722,6 +722,9 @@ Comparisons
|
||||
The following mathematical comparison operators are supported in any
|
||||
expression: ``==``, ``!=``, ``<``, ``>``, ``>=``, and ``<=``.
|
||||
|
||||
In addition, the ``===`` and ``!==`` strict comparison operators are supported
|
||||
(they are equivalent to the ``same as`` and ``not same as`` tests).
|
||||
|
||||
Spaceship Operator
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
@@ -100,10 +100,6 @@ final class LiteralExpressionParser extends AbstractExpressionParser implements
|
||||
return new ContextVariable($token->getValue(), $token->getLine());
|
||||
}
|
||||
|
||||
if ('=' === $token->getValue() && ('==' === $stream->look(-1)->getValue() || '!=' === $stream->look(-1)->getValue())) {
|
||||
throw new SyntaxError(\sprintf('Unexpected operator of value "%s". Did you try to use "===" or "!==" for strict comparison? Use "is same as(value)" instead.', $token->getValue()), $token->getLine(), $stream->getSourceContext());
|
||||
}
|
||||
|
||||
// no break
|
||||
default:
|
||||
throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', $token->toEnglish(), $token->getValue()), $token->getLine(), $stream->getSourceContext());
|
||||
|
||||
@@ -55,10 +55,12 @@ use Twig\Node\Expression\Binary\ModBinary;
|
||||
use Twig\Node\Expression\Binary\MulBinary;
|
||||
use Twig\Node\Expression\Binary\NotEqualBinary;
|
||||
use Twig\Node\Expression\Binary\NotInBinary;
|
||||
use Twig\Node\Expression\Binary\NotSameAsBinary;
|
||||
use Twig\Node\Expression\Binary\NullCoalesceBinary;
|
||||
use Twig\Node\Expression\Binary\OrBinary;
|
||||
use Twig\Node\Expression\Binary\PowerBinary;
|
||||
use Twig\Node\Expression\Binary\RangeBinary;
|
||||
use Twig\Node\Expression\Binary\SameAsBinary;
|
||||
use Twig\Node\Expression\Binary\SpaceshipBinary;
|
||||
use Twig\Node\Expression\Binary\StartsWithBinary;
|
||||
use Twig\Node\Expression\Binary\SubBinary;
|
||||
@@ -360,6 +362,8 @@ final class CoreExtension extends AbstractExtension
|
||||
new BinaryOperatorExpressionParser(EndsWithBinary::class, 'ends with', 20),
|
||||
new BinaryOperatorExpressionParser(HasSomeBinary::class, 'has some', 20),
|
||||
new BinaryOperatorExpressionParser(HasEveryBinary::class, 'has every', 20),
|
||||
new BinaryOperatorExpressionParser(SameAsBinary::class, '===', 20),
|
||||
new BinaryOperatorExpressionParser(NotSameAsBinary::class, '!==', 20),
|
||||
new BinaryOperatorExpressionParser(RangeBinary::class, '..', 25),
|
||||
new BinaryOperatorExpressionParser(AddBinary::class, '+', 30),
|
||||
new BinaryOperatorExpressionParser(SubBinary::class, '-', 30),
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?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;
|
||||
use Twig\Node\Expression\ReturnBoolInterface;
|
||||
|
||||
class NotSameAsBinary extends AbstractBinary implements ReturnBoolInterface
|
||||
{
|
||||
public function operator(Compiler $compiler): Compiler
|
||||
{
|
||||
return $compiler->raw('!==');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?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;
|
||||
use Twig\Node\Expression\ReturnBoolInterface;
|
||||
|
||||
class SameAsBinary extends AbstractBinary implements ReturnBoolInterface
|
||||
{
|
||||
public function operator(Compiler $compiler): Compiler
|
||||
{
|
||||
return $compiler->raw('===');
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
--TEST--
|
||||
The PHP === strict comparison operator is not supported
|
||||
--TEMPLATE--
|
||||
{{ 1 === 2 }}
|
||||
--EXCEPTION--
|
||||
Twig\Error\SyntaxError: Unexpected operator of value "=". Did you try to use "===" or "!==" for strict comparison? Use "is same as(value)" instead in "index.twig" at line 2.
|
||||
@@ -3,10 +3,12 @@ Twig supports the "same as" operator
|
||||
--TEMPLATE--
|
||||
{{ 1 is same as(1) ? 'OK' }}
|
||||
{{ 1 is same as 1 ? 'OK' }}
|
||||
{{ 1 === 1 ? 'OK' }}
|
||||
|
||||
{{ 1 is not same as(true) ? 'OK' }}
|
||||
{{ 1 is not same as true ? 'OK' }}
|
||||
{{ 1 is same as(1) ? 'OK' }}
|
||||
{{ 1 is not same as(true) ? 'OK' }}
|
||||
{{ 1 !== true ? 'OK' }}
|
||||
|
||||
{{ 1 is same as (1) ? 'OK' }}
|
||||
{{ 1 is same as 1 ? 'OK' }}
|
||||
{{ 1 is not same as '1' ? 'OK' }}
|
||||
@@ -20,9 +22,11 @@ return []
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
|
||||
Reference in New Issue
Block a user