ArrayNode example


    protected function getNodeBuilder(): NodeBuilder
    {
        $this->nodeBuilder ??= new NodeBuilder();

        return $this->nodeBuilder->setParent($this);
    }

    protected function createNode(): NodeInterface
    {
        if (!isset($this->prototype)) {
            $node = new ArrayNode($this->name, $this->parent, $this->pathSeparator);

            $this->validateConcreteNode($node);

            $node->setAddIfNotSet($this->addDefaults);

            foreach ($this->children as $child) {
                $child->parent = $node;
                $node->addChild($child->getNode());
            }
        } else {
            $node = new PrototypedArrayNode($this->name, $this->parent, $this->pathSeparator);

            
use Symfony\Component\ExpressionLanguage\Node\BinaryNode;
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))],
            [
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\Config\Definition\ScalarNode;

class ArrayNodeTest extends TestCase
{
    public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
    {
        $this->expectException(InvalidTypeException::class);
        $node = new ArrayNode('root');
        $node->normalize(false);
    }

    public function testExceptionThrownOnUnrecognizedChild()
    {
        $this->expectException(InvalidConfigurationException::class);
        $this->expectExceptionMessage('Unrecognized option "foo" under "root"');
        $node = new ArrayNode('root');
        $node->normalize(['foo' => 'bar']);
    }

    
public function testSetDeprecated()
    {
        $childNode = new ScalarNode('foo');
        $childNode->setDeprecated('vendor/package', '1.1', '"%node%" is deprecated');

        $this->assertTrue($childNode->isDeprecated());
        $deprecation = $childNode->getDeprecation($childNode->getName()$childNode->getPath());
        $this->assertSame('"foo" is deprecated', $deprecation['message']);
        $this->assertSame('vendor/package', $deprecation['package']);
        $this->assertSame('1.1', $deprecation['version']);

        $node = new ArrayNode('root');
        $node->addChild($childNode);

        $deprecationTriggered = 0;
        $deprecationHandler = function D$level$message$file$line) use (&$prevErrorHandler, &$deprecationTriggered) {
            if (\E_USER_DEPRECATED === $level) {
                return ++$deprecationTriggered;
            }

            return $prevErrorHandler ? $prevErrorHandler($level$message$file$line) : false;
        };

        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\VariableNode;

class PrototypedArrayNodeTest extends TestCase
{
    public function testGetDefaultValueReturnsAnEmptyArrayForPrototypes()
    {
        $node = new PrototypedArrayNode('root');
        $prototype = new ArrayNode(null, $node);
        $node->setPrototype($prototype);
        $this->assertEmpty($node->getDefaultValue());
    }

    public function testGetDefaultValueReturnsDefaultValueForPrototypes()
    {
        $node = new PrototypedArrayNode('root');
        $prototype = new ArrayNode(null, $node);
        $node->setPrototype($prototype);
        $node->setDefaultValue(['test']);
        $this->assertEquals(['test']$node->getDefaultValue());
    }
return $this->parsePostfixExpression($node);
    }

    /** * @return Node\ArrayNode */
    public function parseArrayExpression()
    {
        $this->stream->expect(Token::PUNCTUATION_TYPE, '[', 'An array element was expected');

        $node = new Node\ArrayNode();
        $first = true;
        while (!$this->stream->current->test(Token::PUNCTUATION_TYPE, ']')) {
            if (!$first) {
                $this->stream->expect(Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma');

                // trailing ,?                 if ($this->stream->current->test(Token::PUNCTUATION_TYPE, ']')) {
                    break;
                }
            }
            $first = false;

            
'foo["b"]', new GetAttrNode(new NameNode('foo')new ConstantNode('b'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],

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

            ['foo.foo({"b": "a", 0: "b"})', new GetAttrNode(new NameNode('foo')new NameNode('foo'), self::getArrayNode(), GetAttrNode::METHOD_CALL)['foo' => new Obj()]],
            ['foo[index]', new GetAttrNode(new NameNode('foo')new NameNode('index'), self::getArrayNode(), GetAttrNode::ARRAY_CALL)],
        ];
    }

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

        return $array;
    }
}

class Obj
{
    public $foo = 'bar';

    
private function getPrototypeChildren(PrototypedArrayNode $node): array
    {
        $prototype = $node->getPrototype();
        $key = $node->getKeyAttribute();

        // Do not expand prototype if it isn't an array node nor uses attribute as key         if (!$key && !$prototype instanceof ArrayNode) {
            return $node->getChildren();
        }

        if ($prototype instanceof ArrayNode) {
            $keyNode = new ArrayNode($key$node);
            $children = $prototype->getChildren();

            if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) {
                $children = $this->getPrototypeChildren($prototype);
            }

            // add children             foreach ($children as $childNode) {
                $keyNode->addChild($childNode);
            }
        } else {
            
protected static function getArrayNode(): ArrayNode
    {
        $array = static::createArrayNode();
        $array->addElement(new ConstantNode('a')new ConstantNode('b'));
        $array->addElement(new ConstantNode('b'));

        return $array;
    }

    protected static function createArrayNode(): ArrayNode
    {
        return new ArrayNode();
    }
}
$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'],
            ],
            [
                new Node\ConstantNode('a'),
                '"a"',
            ],
Home | Imprint | This part of the site doesn't use cookies.