8000 [ExpressionLanguage] Make `in` and `not in` handle Traversables by ostrolucky · Pull Request #25506 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ExpressionLanguage] Make in and not in handle Traversables #25506

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

Closed
Closed
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
31 changes: 24 additions & 7 deletions src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class BinaryNode extends Node
private static $functions = array(
'**' => 'pow',
'..' => 'range',
'in' => 'in_array',
'not in' => '!in_array',
);

public function __construct(string $operator, Node $left, Node $right)
Expand All @@ -57,6 +55,28 @@ public function compile(Compiler $compiler)
return;
}

switch ($operator) {
case 'not in':
$compiler->raw('!');
// no break
case 'in':
$compiler
->raw('in_array(')
->compile($this->nodes['left'])
->raw(', ')
->raw('is_array(')
->compile($this->nodes['right'])
->raw(') ? ')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I could use ConditionalNode instead of ->raw() for ternary, but this ternary requires FunctionNode inside, for which Parser would have to have functions is_array and iterator_to_array registered

->compile($this->nodes['right'])
->raw(' : iterator_to_array(')
->compile($this->nodes['right'])
->raw(')')
->raw(')')
;

return;
}

if (isset(self::$functions[$operator])) {
$compiler
->raw(sprintf('%s(', self::$functions[$operator]))
Expand Down Expand Up @@ -92,9 +112,6 @@ public function evaluate($functions, $values)
if (isset(self::$functions[$operator])) {
$right = $this->nodes['right']->evaluate($functions, $values);

if ('not in' === $operator) {
return !in_array($left, $right);
}
$f = self::$functions[$operator];

return $f($left, $right);
Expand Down Expand Up @@ -135,9 +152,9 @@ public function evaluate($functions, $values)
case '<=':
return $left <= $right;
case 'not in':
return !in_array($left, $right);
return !in_array($left, is_array($right) ? $right : iterator_to_array($right));
case 'in':
return in_array($left, $right);
return in_array($left, is_array($right) ? $right : iterator_to_array($right));
case '+':
return $left + $right;
case '-':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\ExpressionLanguage\Tests\Fixtures;

class Collection implements \IteratorAggregate
{
public function getIterator()
{
return new \ArrayIterator(array('a', 'b'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\BinaryNode;
use Symfony\Component\ExpressionLanguage\Node\ArrayNode;
use Symfony\Component\ExpressionLanguage\Node\BinaryNode;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\NameNode;
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\Collection;

class BinaryNodeTest extends AbstractNodeTest
{
Expand All @@ -23,6 +25,8 @@ public function getEvaluateData()
$array->addElement(new ConstantNode('a'));
$array->addElement(new ConstantNode('b'));

$collectionValues = array('collection' => new Collection());

return array(
array(true, new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))),
array(true, new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))),
Expand Down Expand Up @@ -56,9 +60,13 @@ public function getEvaluateData()
array('ab', new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'))),

array(true, new BinaryNode('in', new ConstantNode('a'), $array)),
array(true, new BinaryNode('in', new ConstantNode('a'), new NameNode('collection')), $collectionValues),
array(false, new BinaryNode('in', new ConstantNode('c'), $array)),
array(false, new BinaryNode('in', new ConstantNode('c'), new NameNode('collection')), $collectionValues),
array(true, new BinaryNode('not in', new ConstantNode('c'), $array)),
array(true, new BinaryNode('not in', new ConstantNode('c'), new NameNode('collection')), $collectionValues),
array(false, new BinaryNode('not in', new ConstantNode('a'), $array)),
array(false, new BinaryNode('not in', new ConstantNode('a'), new NameNode('collection')), $collectionValues),

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

Expand Down Expand Up @@ -104,10 +112,10 @@ public function getCompileData()
array('pow(5, 2)', new BinaryNode('**', new ConstantNode(5), new ConstantNode(2))),
array('("a" . "b")', new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'))),

array('in_array("a", array(0 => "a", 1 => "b"))', new BinaryNode('in', new ConstantNode('a'), $array)),
array('in_array("c", array(0 => "a", 1 => "b"))', new BinaryNode('in', new ConstantNode('c'), $array)),
array('!in_array("c", array(0 => "a", 1 => "b"))', new BinaryNode('not in', new ConstantNode('c'), $array)),
array('!in_array("a", array(0 => "a", 1 => "b"))', new BinaryNode('not in', new ConstantNode('a'), $array)),
array('in_array("a", is_array(array(0 => "a", 1 => "b")) ? array(0 => "a", 1 => "b") : iterator_to_array(array(0 => "a", 1 => "b")))', new BinaryNode('in', new ConstantNode('a'), $array)),
array('in_array("c", is_array(array(0 => "a", 1 => "b")) ? array(0 => "a", 1 => "b") : iterator_to_array(array(0 => "a", 1 => "b")))', new BinaryNode('in', new ConstantNode('c'), $array)),
array('!in_array("c", is_array(array(0 => "a", 1 => "b")) ? array(0 => "a", 1 => "b") : iterator_to_array(array(0 => "a", 1 => "b")))', new BinaryNode('not in', new ConstantNode('c'), $array)),
array('!in_array("a", is_array(array(0 => "a", 1 => "b")) ? array(0 => "a", 1 => "b") : iterator_to_array(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))),

Expand Down
0