ConstantNode example

class ArrayNode extends Node
{
    protected int $index;

    public function __construct()
    {
        $this->index = -1;
    }

    public function addElement(Node $value, Node $key = null): void
    {
        $key ??= new ConstantNode(++$this->index);

        array_push($this->nodes, $key$value);
    }

    /** * Compiles the node to PHP. */
    public function compile(Compiler $compiler): void
    {
        $compiler->raw('[');
        $this->compileArguments($compiler);
        
namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\UnaryNode;

class UnaryNodeTest extends AbstractNodeTestCase
{
    public static function getEvaluateData(): array
    {
        return [
            [-1, new UnaryNode('-', new ConstantNode(1))],
            [3, new UnaryNode('+', new ConstantNode(3))],
            [false, new UnaryNode('!', new ConstantNode(true))],
            [false, new UnaryNode('not', new ConstantNode(true))],
        ];
    }

    public static function getCompileData(): array
    {
        return [
            ['(-1)', new UnaryNode('-', new ConstantNode(1))],
            ['(+3)', new UnaryNode('+', new ConstantNode(3))],
            [
namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\FunctionNode;
use Symfony\Component\ExpressionLanguage\Node\Node;

class FunctionNodeTest extends AbstractNodeTestCase
{
    public static function getEvaluateData(): array
    {
        return [
            ['bar', new FunctionNode('foo', new Node([new ConstantNode('bar')]))[]['foo' => static::getCallables()]],
        ];
    }

    public static function getCompileData(): array
    {
        return [
            ['foo("bar")', new FunctionNode('foo', new Node([new ConstantNode('bar')]))['foo' => static::getCallables()]],
        ];
    }

    public static function getDumpData(): array
    {


namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\ConstantNode;

class ConstantNodeTest extends AbstractNodeTestCase
{
    public static function getEvaluateData(): array
    {
        return [
            [false, new ConstantNode(false)],
            [true, new ConstantNode(true)],
            [null, new ConstantNode(null)],
            [3, new ConstantNode(3)],
            [3.3, new ConstantNode(3.3)],
            ['foo', new ConstantNode('foo')],
            [[1, 'b' => 'a']new ConstantNode([1, 'b' => 'a'])],
        ];
    }

    public static function getCompileData(): array
    {
        
namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\ArrayNode;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;

class ArrayNodeTest extends AbstractNodeTestCase
{
    public function testSerialization()
    {
        $node = $this->createArrayNode();
        $node->addElement(new ConstantNode('foo'));

        $serializedNode = serialize($node);
        $unserializedNode = unserialize($serializedNode);

        $this->assertEquals($node$unserializedNode);
        $this->assertNotEquals($this->createArrayNode()$unserializedNode);
    }

    public static function getEvaluateData(): array
    {
        return [
            [[

    public function testParse($node$expression$names = [])
    {
        $lexer = new Lexer();
        $parser = new Parser([]);
        $this->assertEquals($node$parser->parse($lexer->tokenize($expression)$names));
    }

    public static function getParseData()
    {
        $arguments = new Node\ArgumentsNode();
        $arguments->addElement(new Node\ConstantNode('arg1'));
        $arguments->addElement(new Node\ConstantNode(2));
        $arguments->addElement(new Node\ConstantNode(true));

        $arrayNode = new Node\ArrayNode();
        $arrayNode->addElement(new Node\NameNode('bar'));

        return [
            [
                new Node\NameNode('a'),
                'a',
                ['a'],
            ],
namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\ConditionalNode;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;

class ConditionalNodeTest extends AbstractNodeTestCase
{
    public static function getEvaluateData(): array
    {
        return [
            [1, new ConditionalNode(new ConstantNode(true)new ConstantNode(1)new ConstantNode(2))],
            [2, new ConditionalNode(new ConstantNode(false)new ConstantNode(1)new ConstantNode(2))],
        ];
    }

    public static function getCompileData(): array
    {
        return [
            ['((true) ? (1) : (2))', new ConditionalNode(new ConstantNode(true)new ConstantNode(1)new ConstantNode(2))],
            ['((false) ? (1) : (2))', new ConditionalNode(new ConstantNode(false)new ConstantNode(1)new ConstantNode(2))],
        ];
    }

    
namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\Node;

class NodeTest extends TestCase
{
    public function testToString()
    {
        $node = new Node([new ConstantNode('foo')]);

        $this->assertEquals(<<<'EOF' Node( ConstantNode(value: 'foo') ) EOF
            , (string) $node);
    }

    public function testSerialization()
    {
        
namespace Symfony\Component\ExpressionLanguage\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\ParsedExpression;

class ParsedExpressionTest extends TestCase
{
    public function testSerialization()
    {
        $expression = new ParsedExpression('25', new ConstantNode('25'));

        $serializedExpression = serialize($expression);
        $unserializedExpression = unserialize($serializedExpression);

        $this->assertEquals($expression$unserializedExpression);
    }
}
use Symfony\Component\ExpressionLanguage\Node\ArrayNode;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\GetAttrNode;
use Symfony\Component\ExpressionLanguage\Node\NameNode;

class GetAttrNodeTest extends AbstractNodeTestCase
{
    public static function getEvaluateData(): array
    {
        return [
            ['b', new GetAttrNode(new NameNode('foo')new ConstantNode(0), self::getArrayNode(), GetAttrNode::ARRAY_CALL)['foo' => ['b' => 'a', 'b']]],
            ['a', new GetAttrNode(new NameNode('foo')new ConstantNode('b'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)['foo' => ['b' => 'a', 'b']]],

            ['bar', new GetAttrNode(new NameNode('foo')new ConstantNode('foo'), self::getArrayNode(), GetAttrNode::PROPERTY_CALL)['foo' => new Obj()]],

            ['baz', new GetAttrNode(new NameNode('foo')new ConstantNode('foo'), self::getArrayNode(), GetAttrNode::METHOD_CALL)['foo' => new Obj()]],
            ['a', new GetAttrNode(new NameNode('foo')new NameNode('index'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)['foo' => ['b' => 'a', 'b'], 'index' => 'b']],
        ];
    }

    public static function getCompileData(): array
    {
        
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\NameNode;
use Symfony\Component\ExpressionLanguage\SyntaxError;

class BinaryNodeTest extends AbstractNodeTestCase
{
    use ExpectDeprecationTrait;

    public static function getEvaluateData(): array
    {
        $array = new ArrayNode();
        $array->addElement(new ConstantNode('a'));
        $array->addElement(new ConstantNode('b'));

        return [
            [true, new BinaryNode('or', new ConstantNode(true)new ConstantNode(false))],
            [true, new BinaryNode('||', new ConstantNode(true)new ConstantNode(false))],
            [false, new BinaryNode('and', new ConstantNode(true)new ConstantNode(false))],
            [false, new BinaryNode('&&', new ConstantNode(true)new ConstantNode(false))],

            [0, new BinaryNode('&', new ConstantNode(2)new ConstantNode(4))],
            [6, new BinaryNode('|', new ConstantNode(2)new ConstantNode(4))],
            [6, new BinaryNode('^', new ConstantNode(2)new ConstantNode(4))],

            [
$expr = new Node\NullCoalesceNode($expr$expr2);
        }

        while ($this->stream->current->test(Token::PUNCTUATION_TYPE, '?')) {
            $this->stream->next();
            if (!$this->stream->current->test(Token::PUNCTUATION_TYPE, ':')) {
                $expr2 = $this->parseExpression();
                if ($this->stream->current->test(Token::PUNCTUATION_TYPE, ':')) {
                    $this->stream->next();
                    $expr3 = $this->parseExpression();
                } else {
                    $expr3 = new Node\ConstantNode(null);
                }
            } else {
                $this->stream->next();
                $expr2 = $expr;
                $expr3 = $this->parseExpression();
            }

            $expr = new Node\ConditionalNode($expr$expr2$expr3);
        }

        return $expr;
    }
Home | Imprint | This part of the site doesn't use cookies.