1 | <?php |
||
14 | class ExpressionLanguage extends \Symfony\Component\ExpressionLanguage\ExpressionLanguage { |
||
15 | 85 | public function __construct() { |
|
16 | 85 | parent::__construct(); |
|
17 | |||
18 | 85 | $this->registerNodeFunction(GetAttrNode::class, function (\Symfony\Component\ExpressionLanguage\Compiler $compiler, GetAttrNode $node) { |
|
19 | 10 | switch ($node->attributes['type']) { |
|
20 | 10 | case GetAttrNode::METHOD_CALL: |
|
21 | $compiler |
||
22 | ->compile($node->nodes['node']) |
||
23 | ->raw('->') |
||
24 | ->raw($node->nodes['attribute']->attributes['value']) |
||
25 | ->raw('(') |
||
26 | ->compile($node->nodes['arguments']) |
||
27 | ->raw(')') |
||
28 | ; |
||
29 | break; |
||
30 | |||
31 | 10 | case GetAttrNode::PROPERTY_CALL: |
|
32 | 10 | case GetAttrNode::ARRAY_CALL: |
|
33 | 10 | $itExpr = $this->iteratorExpression($node); |
|
34 | |||
35 | 10 | if ($itExpr) { |
|
|
|||
36 | 1 | $compiler->raw($itExpr); |
|
37 | 1 | } else { |
|
38 | $compiler |
||
39 | 9 | ->compile($node->nodes['node']) |
|
40 | 9 | ->raw('[') |
|
41 | 9 | ->compile($node->nodes['attribute'])->raw(']'); |
|
42 | } |
||
43 | 10 | break; |
|
44 | 10 | } |
|
45 | 85 | }); |
|
46 | 85 | } |
|
47 | |||
48 | /** |
||
49 | * Look for a specific iterator expression. |
||
50 | * |
||
51 | * Iterator expressions are one of the following: |
||
52 | * |
||
53 | * - i123.index |
||
54 | * - i123.first |
||
55 | * - i123.last |
||
56 | * - i123.count |
||
57 | * |
||
58 | * @param GetAttrNode $node The node to inspect. |
||
59 | * @return null|string Returns the appropriate variable or **null** if the node isn't an iterator expression. |
||
60 | */ |
||
61 | 10 | private function iteratorExpression(GetAttrNode $node) { |
|
62 | 10 | if (empty($node->nodes['node']) |
|
63 | 10 | || !($node->nodes['node'] instanceof NameNode) |
|
64 | 10 | || !preg_match('`^i(\d+)$`', $node->nodes['node']->attributes['name'], $m) |
|
65 | 10 | || empty($node->nodes['attribute']) |
|
66 | 1 | || !($node->nodes['attribute'] instanceof ConstantNode) |
|
67 | 1 | || !in_array($node->nodes['attribute']->attributes['value'], ['index', 'first', 'last', 'count'], true) |
|
68 | 10 | ) { |
|
69 | |||
70 | 9 | return null; |
|
71 | } |
||
72 | 1 | $i = $m[1]; |
|
73 | 1 | $field = $node->nodes['attribute']->attributes['value']; |
|
74 | 1 | return "\${$field}{$i}"; |
|
75 | } |
||
76 | |||
77 | 40 | public function getFunctionCompiler($name) { |
|
83 | } |
||
84 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: