0

I want to parse a YAML configuration that looks like this:

pageRoles:
  Report1: [abc, xyz, def]
  Report2: [fgh, xxx, yyy, rrr]

I want the resulting configuration array to look like this:

'pageRoles':
  'Report1':
    [
      'abc',
      'xyz',
      'def'
    ],
  'Report2': 
    [
      'fgh',
      'xxx',
      'yyy',
      'rrr'
    ]

I have this at the moment:

    ->arrayNode( 'pageRoles' )
      ->prototype( 'array' )
        ->useAttributeAsKey( 'name' )
        ->prototype( 'array' )
          ->prototype( 'scalar' )->end()
        ->end()
      ->end() // array prototype
    ->end() // pageRoles

And am getting this error:

Invalid type for path "site.pageRoles.ActivityReport.0". Expected array, but got string

What am I missing?

1
  • pageRoles is an array or at least I want it to be. Commented Aug 12, 2020 at 18:07

1 Answer 1

5

Making Symfony configuration trees. My favorite way to kill a boring day. This seems to work:

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('my');
        $treeBuilder->getRootNode()
            ->children()
                ->arrayNode('pageRoles')
                    ->useAttributeAsKey('name')
                     ->arrayPrototype()->scalarPrototype()->end()->end()
                ->end() // pageRoles
            ->end() // root node
        ;
        return $treeBuilder;
    }

After processing I get:

array:1 [
  "pageRoles" => array:2 [
    "Report1" => array:3 [
      0 => "abc"
      1 => "xyz"
      2 => "def"
    ]
    "Report2" => array:4 [
      0 => "fgh"
      1 => "xxx"
      2 => "yyy"
      3 => "rrr"
    ]
  ]
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.