David Patterson
David Patterson

Reputation: 1910

How do I define an array of arrays of scalars in Symfony Configuration.php?

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?

Upvotes: 0

Views: 1042

Answers (1)

Cerad
Cerad

Reputation: 48865

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"
    ]
  ]
]

Upvotes: 5

Related Questions