php - Not able to parse correctly a yaml file with symfony TreeNode -


i'm trying apparently simple.

i want parse yaml structure:

  filters:     filter:       class: parentnamespace\myclassa     filter:       class: parentnamespace\myclassb       params:         customparam: 5         anotherparam: 1 

so, want required filters node can have 1 or more filter node. each of must have 'class' node , have optional params array node.

i'm trying treebuilder taking second filter, wonder if overriding first one.

i tried , can't working.

->arraynode('filters')     ->isrequired()     ->children()         ->arraynode('filter')             ->children()                 ->scalarnode('class')                     ->isrequired()                 ->end()                 ->arraynode('params')                     ->defaultvalue(array())                     ->prototype('variable')->end()                 ->end()             ->end()         ->end()     ->end() ->end() 

you forgot make filter node prototyped array node. overriding itself, since arraynode can have 1 occurrence:

->arraynode('filters')     ->isrequired()     ->children()         ->arraynode('filter')             ->prototype('array')                 ->children()                     ->scalarnode('class')                         ->isrequired()                     ->end()                     ->arraynode('params')                         ->defaultvalue(array())                         ->prototype('variable')->end()                     ->end()                 ->end()             ->end()         ->end()     ->end() ->end() 

besides this, suggest use instead:

filters:   parentnamespace\myclassa: ~   parentnamespace\myclassb:     params:       customparam: 5       anotherparam: 1 

it's more friendly , allows support xml:

->fixxmlconfig('filter') ->children()     ->arraynode('filters')         ->useattributeaskey('class')         ->prototype('array')             ->children()                 ->arraynode('params')                     ->defaultvalue(array())                     ->prototype('variable')->end()                 ->end()             ->end()         ->end()     ->end() ->end() 

at last, recommend use requiresatleastoneelement() instead of isrequired() require @ least 1 element.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -