Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a41781

Browse files
committed
[ExpressionLanguage] added support for regexes
1 parent 9d98fa2 commit 3a41781

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

src/Symfony/Component/ExpressionLanguage/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function getOperatorRegex()
101101
{
102102
$operators = array(
103103
'not', '!', '-', '+',
104-
'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', '**',
104+
'or', '||', '&&', 'and', '|', '^', '&', '==', '===', '!=', '!==', '<', '>', '>=', '<=', 'not in', 'in', '..', '+', '-', '~', '*', '/', '%', '=~', '!~', '**',
105105
);
106106

107107
$operators = array_combine($operators, array_map('strlen', $operators));

src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public function compile(Compiler $compiler)
3838
{
3939
$operator = $this->attributes['operator'];
4040

41+
if ('=~' == $operator || '!~' == $operator) {
42+
$compiler
43+
->raw(('!~' == $operator ? '!' : '').'preg_match(')
44+
->compile($this->nodes['right'])
45+
->raw(', ')
46+
->compile($this->nodes['left'])
47+
->raw(')')
48+
;
49+
50+
return;
51+
}
52+
4153
if (isset($this->functions[$operator])) {
4254
$compiler
4355
->raw(sprintf('%s(', $this->functions[$operator]))
@@ -124,6 +136,10 @@ public function evaluate($functions, $values)
124136
return $left / $right;
125137
case '%':
126138
return $left % $right;
139+
case '=~':
140+
return preg_match($right, $left);
141+
case '!~':
142+
return !preg_match($right, $left);
127143
}
128144
}
129145
}

src/Symfony/Component/ExpressionLanguage/Parser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function __construct(array $functions)
6767
'*' => array('precedence' => 60, 'associativity' => Parser::OPERATOR_LEFT),
6868
'/' => array('precedence' => 60, 'associativity' => Parser::OPERATOR_LEFT),
6969
'%' => array('precedence' => 60, 'associativity' => Parser::OPERATOR_LEFT),
70+
'=~' => array('precedence' => 70, 'associativity' => Parser::OPERATOR_LEFT),
71+
'!~' => array('precedence' => 70, 'associativity' => Parser::OPERATOR_LEFT),
7072
'**' => array('precedence' => 200, 'associativity' => Parser::OPERATOR_RIGHT),
7173
);
7274
}

src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function getEvaluateData()
6161
array(false, new BinaryNode('not in', new ConstantNode('a'), $array)),
6262

6363
array(array(1, 2, 3), new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))),
64+
65+
array(1, new BinaryNode('=~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))),
66+
array(false, new BinaryNode('!~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))),
6467
);
6568
}
6669

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

110113
array('range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))),
114+
115+
array('preg_match("/^[a-z]+/i\$/", "abc")', new BinaryNode('=~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+/i$/'))),
116+
array('!preg_match("/^[a-z]+\$/", "abc")', new BinaryNode('!~', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))),
111117
);
112118
}
113119
}

0 commit comments

Comments
 (0)