Skip to content

[ExpressionLanguage] add XOR operator #34329

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

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* add `xor` operator

4.0.0
-----

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function tokenize($expression)
// strings
$tokens[] = new Token(Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)), $cursor + 1);
$cursor += \strlen($match[0]);
} elseif (preg_match('/not in(?=[\s(])|\!\=\=|not(?=[\s(])|and(?=[\s(])|\=\=\=|\>\=|or(?=[\s(])|\<\=|\*\*|\.\.|in(?=[\s(])|&&|\|\||matches|\=\=|\!\=|\*|~|%|\/|\>|\||\!|\^|&|\+|\<|\-/A', $expression, $match, 0, $cursor)) {
} elseif (preg_match('/not in(?=[\s(])|\!\=\=|not(?=[\s(])|and(?=[\s(])|\=\=\=|\>\=|or(?=[\s(])|xor(?=[\s(])|\<\=|\*\*|\.\.|in(?=[\s(])|&&|\|\||matches|\=\=|\!\=|\*|~|%|\/|\>|\||\!|\^|&|\+|\<|\-/A', $expression, $match, 0, $cursor)) {
// operators
$tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
$cursor += \strlen($match[0]);
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public function evaluate($functions, $values)
case 'or':
case '||':
return $left || $this->nodes['right']->evaluate($functions, $values);
case 'xor':
return $left xor $this->nodes['right']->evaluate($functions, $values);
case 'and':
case '&&':
return $left && $this->nodes['right']->evaluate($functions, $values);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(array $functions)
$this->binaryOperators = [
'or' => ['precedence' => 10, 'associativity' => self::OPERATOR_LEFT],
'||' => ['precedence' => 10, 'associativity' => self::OPERATOR_LEFT],
'xor' => ['precedence' => 13, 'associativity' => self::OPERATOR_LEFT],
'and' => ['precedence' => 15, 'associativity' => self::OPERATOR_LEFT],
'&&' => ['precedence' => 15, 'associativity' => self::OPERATOR_LEFT],
'|' => ['precedence' => 16, 'associativity' => self::OPERATOR_LEFT],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,35 @@ function (ExpressionLanguage $el) {
],
];
}

/**
* @dataProvider getLogicalOperators
*/
public function testLogicalOperators($expression, $expected)
{
$this->assertSame($expected, (new ExpressionLanguage())->evaluate($expression));
}

public function getLogicalOperators()
{
return [
// AND
['true and true', true],
['true and false', false],
['false and true', false],
['false and false', false],

// OR
['true or true', true],
['true or false', true],
['false or true', true],
['false or false', false],

// XOR
['true xor true', false],
['true xor false', true],
['false xor true', true],
['false xor false', false],
];
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public function getTokenizeData()
[new Token('string', '#foo', 1)],
'"#foo"',
],
[
[
new Token('name', 'a', 1),
new Token('operator', 'xor', 3),
new Token('name', 'b', 7),
],
'a xor b',
],
];
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ public function getParseData()
'bar',
['foo' => 'bar'],
],

[
new Node\BinaryNode('xor', new Node\ConstantNode(true), new Node\ConstantNode(false)),
'true xor false',
],
];
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy