Skip to content
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function tokenize(string $expression): TokenStream
// strings
$tokens[] = new Token(Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)), $cursor + 1);
$cursor += \strlen($match[0]);
} elseif (preg_match('/(?<=^|[\s(])not in(?=[\s(])|\!\=\=|(?<=^|[\s(])not(?=[\s(])|(?<=^|[\s(])and(?=[\s(])|\=\=\=|\>\=|(?<=^|[\s(])or(?=[\s(])|\<\=|\*\*|\.\.|(?<=^|[\s(])in(?=[\s(])|&&|\|\||(?<=^|[\s(])matches|\=\=|\!\=|\*|~|%|\/|\>|\||\!|\^|&|\+|\<|\-/A', $expression, $match, 0, $cursor)) {
} elseif (preg_match('/(?<=^|[\s(])starts with(?=[\s(])|(?<=^|[\s(])ends with(?=[\s(])|(?<=^|[\s(])contains(?=[\s(])|(?<=^|[\s(])matches(?=[\s(])|(?<=^|[\s(])not in(?=[\s(])|(?<=^|[\s(])not(?=[\s(])|(?<=^|[\s(])and(?=[\s(])|\=\=\=|\!\=\=|(?<=^|[\s(])or(?=[\s(])|\|\||&&|\=\=|\!\=|\>\=|\<\=|(?<=^|[\s(])in(?=[\s(])|\.\.|\*\*|\!|\||\^|&|\<|\>|\+|\-|~|\*|\/|%/A', $expression, $match, 0, $cursor)) {
// operators
$tokens[] = new Token(Token::OPERATOR_TYPE, $match[0], $cursor + 1);
$cursor += \strlen($match[0]);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class BinaryNode extends Node
'..' => 'range',
'in' => 'in_array',
'not in' => '!in_array',
'contains' => 'str_contains',
'starts with' => 'str_starts_with',
'ends with' => 'str_ends_with',
];

public function __construct(string $operator, Node $left, Node $right)
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function __construct(array $functions)
'<=' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'not in' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'in' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'contains' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'starts with' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'ends with' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'matches' => ['precedence' => 20, 'associativity' => self::OPERATOR_LEFT],
'..' => ['precedence' => 25, 'associativity' => self::OPERATOR_LEFT],
'+' => ['precedence' => 30, 'associativity' => self::OPERATOR_LEFT],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

$operators = ['not', '!', 'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'matches', '**'];
$operators = ['not', '!', 'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', 'contains', 'starts with', 'ends with', 'matches', '**'];
$operators = array_combine($operators, array_map('strlen', $operators));
arsort($operators);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function getEvaluateData()

[[1, 2, 3], new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],

[true, new BinaryNode('starts with', new ConstantNode('abc'), new ConstantNode('a'))],
[false, new BinaryNode('starts with', new ConstantNode('abc'), new ConstantNode('b'))],
[true, new BinaryNode('ends with', new ConstantNode('abc'), new ConstantNode('c'))],
[false, new BinaryNode('ends with', new ConstantNode('abc'), new ConstantNode('b'))],
[1, new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))],
];
}
Expand Down Expand Up @@ -114,6 +118,8 @@ public function getCompileData()

['range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],

['str_starts_with("abc", "a")', new BinaryNode('starts with', new ConstantNode('abc'), new ConstantNode('a'))],
['str_ends_with("abc", "a")', new BinaryNode('ends with', new ConstantNode('abc'), new ConstantNode('a'))],
['(static function ($regexp, $str) { set_error_handler(function ($t, $m) use ($regexp, $str) { throw new \Symfony\Component\ExpressionLanguage\SyntaxError(sprintf(\'Regexp "%s" passed to "matches" is not valid\', $regexp).substr($m, 12)); }); try { return preg_match($regexp, $str); } finally { restore_error_handler(); } })("/^[a-z]+\$/", "abc")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))],
];
}
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ public function getParseData()
new Node\BinaryNode('matches', new Node\ConstantNode('foo'), new Node\ConstantNode('/foo/')),
'"foo" matches "/foo/"',
],
[
new Node\BinaryNode('starts with', new Node\ConstantNode('foo'), new Node\ConstantNode('f')),
'"foo" starts with "f"',
],
[
new Node\BinaryNode('ends with', new Node\ConstantNode('foo'), new Node\ConstantNode('f')),
'"foo" ends with "f"',
],
[
new Node\BinaryNode('contains', new Node\ConstantNode('foo'), new Node\ConstantNode('f')),
'"foo" contains "f"',
],

// chained calls
[
Expand Down