Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Config Treebuilder

Tags:

symfony

I'm looking to create a configuration like the following:

root_node:
    static_key:
        dynamic_key_1: [array, of, values]
        dynamic_key_2: [array, of, values]

I can't seem to figure out the correct Treebuilder syntax.

I've tried:

$rootNode
    ->children()
        ->arrayNode('static_key')
            ->prototype('scalar')
            ->end()
        ->end()
    ->end()
;

but I get:

Invalid type for path "root_node.static_key.dynamic_key_1". Expected scalar, but got array.

and when I switch to:

$rootNode
    ->children()
        ->arrayNode('static_key')
            ->prototype('array')
            ->end()
        ->end()
    ->end()
;

I get:

Unrecognized options "0, 1, 2" under "root_node.static_key.dynamic_key_1"

I've eventually figured out I could have the configuration pass using:

$rootNode
    ->children()
        ->arrayNode('static_key')
            ->prototype('variable')
            ->end()
        ->end()
    ->end()
;

but that doesn't guarantee I get an array.

Could someone point me in the right direction?


I know I can do something like this:

/// treebuilder
$rootNode
    ->children()
        ->arrayNode('static_key')
            ->prototype('array')
                ->children()
                    ->scalarNode('attr_1')->end()
                    ->scalarNode('attr_2')->end()
                ->end()
            ->end()
        ->end()
    ->end()
;

# config.yml
root_node:
    static_key:
        # two different syntaxes for emphasis
        dynamic_key_1:
            attr_1: value_1
            attr_2: value_2
        dynamic_key_2: {attr_1: value_3, attr_2: value_4}

and receive the following PHP array:

array('root_node' => array(
    'static_key' => array(
        'dynamic_key_1' => array(
            'attr_1' => 'value_1', 
            'attr_2' => 'value_2'
        ),
        'dynamic_key_2' => array(
            'attr_1' => 'value_3', 
            'attr_2' => 'value_4'
        ),
    ),
));

I've done it before.

I just done know why I can't strictly ask for a non-associative array:

What's so different about {attr_1: value_3, attr_2: value_4} vs [value_3, value_4]? To me, both are arrays: one is associative, and the other is not.

like image 862
captivej Avatar asked Dec 16 '15 22:12

captivej


1 Answers

Dynamic keys are not supported.

you should make something like this:

root_node:
    static_key:
        - { key: dynamic_key_1, value: [array, of, values] }
        - { key: dynamic_key_2, value: [array, of, values] }

and if i am right you might get something like this:

    $rootNode
        ->children()
            ->arrayNode('static_key')
                ->prototype('array')
                    ->children()
                        ->scalarNode('key')->isRequired()->end()
                        ->arrayNode('value')
                            ->prototype('scalar')
                            ->end()
                       ->end()
                    ->end()
                ->end()
            ->end()
        ->end()
like image 196
Frank B Avatar answered Oct 19 '22 15:10

Frank B