10000 New Component: Expression Language by fabpot · Pull Request #8913 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

New Component: Expression Language #8913

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 12 commits into from
Sep 19, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[ExpressionLanguage] added support for regexes
  • Loading branch information
fabpot committed Sep 19, 2013
commit 3a417816405906da17248aa9d413956831f2817e
2 changes: 1 addition & 1 deletion src/Symfony/Component/ExpressionLanguage/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private function getOperatorRegex()
{
$operators = array(
'not', '!', '-', '+',
'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', '**',
'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', '=~', '!~', '**',
);

$operators = array_combine($operators, array_map('strlen', $operators));
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public function compile(Compiler $compiler)
{
$operator = $this->attributes['operator'];

if ('=~' == $operator || '!~' == $operator) {
$compiler
->raw(('!~' == $operator ? '!' : '').'preg_match(')
->compile($this->nodes['right'])
->raw(', ')
->compile($this->nodes['left'])
->raw(')')
;

return;
}

if (isset($this->functions[$operator])) {
$compiler
->raw(sprintf('%s(', $this->functions[$operator]))
Expand Down Expand Up @@ -124,6 +136,10 @@ public function evaluate($functions, $values)
return $left / $right;
case '%':
return $left % $right;
case '=~':
return preg_match($right, $left);
case '!~':
return !preg_match($right, $left);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function __construct(array $functions)
'*' => array('precedence' => 60, 'associativity' => Parser::OPERATOR_LEFT),
'/' => array('precedence' => 60, 'associativity' => Parser::OPERATOR_LEFT),
'%' => array('precedence' => 60, 'associativity' => Parser::OPERATOR_LEFT),
'=~' => array('precedence' => 70, 'associativity' => Parser::OPERATOR_LEFT),
'!~' => array('precedence' => 70, 'associativity' => Parser::OPERATOR_LEFT),
'**' => array('precedence' => 200, 'associativity' => Parser::OPERATOR_RIGHT),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function getEvaluateData()
array(false, new BinaryNode('not in', new ConstantNode('a'), $array)),

array(array(1, 2, 3), new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))),

array(1, new BinaryNode('=~', new ConstantNode('abc'), new ConstantNode('/^[a 693D -z]+$/'))),
array(false, new BinaryNode('!~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))),
);
}

Expand Down Expand Up @@ -108,6 +111,9 @@ public function getCompileData()
array('!in_array("a", array(0 => "a", 1 => "b"))', new BinaryNode('not in', new ConstantNode('a'), $array)),

array('range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))),

array('preg_match("/^[a-z]+/i\$/", "abc")', new BinaryNode('=~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+/i$/'))),
array('!preg_match("/^[a-z]+\$/", "abc")', new BinaryNode('!~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))),
);
}
}
0