GergelyPolonkai
GergelyPolonkai

Reputation: 6421

Symfony 2 - configuration value: array of associative arrays

I want to process the following configuration:

acme_demo:
    transitions:
        - { hc_cba: 180 }
        - { cba_hc: -1 }

It is clear that I will need to add an arrayNode, like

$rootNode
    ->children()
        ->arrayNode('transitions')
            ->beforeNormalization()
                ->ifArray()
                    ->then(function($values) {
                        return array('transition' => $values);
                    })
                ->end()
            ->end()
        ->end()
    ->end()
;

but this gives me an InvalidConfigurationException with the message

Unrecognized options "transitions" under "acme_demo.state_machine"

How should I process those "inner" values?

Upvotes: 3

Views: 1147

Answers (1)

GergelyPolonkai
GergelyPolonkai

Reputation: 6421

A large amount of stress after hours of trying drove me to the solution just after writing down the question:

$rootNode
        ->children()
            ->arrayNode('state_machine')
                ->requiresAtLeastOneElement()
                ->beforeNormalization()
                    ->ifArray()
                        ->then(function($values) {
                            $ret = array();

                            foreach ($values as $value) {
                                foreach ($value as $transition => $time) {
                                    $ret[] = array('transition' => $transition, 'time' => e);
                                }
                            }

                            return $ret;
                        })
                    ->end()
                    ->prototype('array')
                    ->children()
                        ->scalarNode('transition')->end()
                        ->scalarNode('time')->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ;

Upvotes: 3

Related Questions