Symfony配置使用AttributeAsKey和索引数组


Symfony configuration useAttributeAsKey and indexed array

我想创建一个Symfony配置类,该类采用以下YAML配置:

bundle_name:
    section:
        attributeAsKey:
            - entry 1
            - entry 2

这将产生这样的数组:

array(
   'section' => array(
      'attributeAsKey' => array(
          'entry1',
          'entry2'
       )
   )
)

我试过以下方法,但不起作用。我做错了什么?

->arrayNode('section')
    ->useAttributeAsKey('attributeAsKey')
    ->prototype('array')
        ->children()
            ->arrayNode('entries')
                ->prototype('scalar')->end()
            ->end()
        ->end()
    ->end()
->end()

这就是我最终要做的。。。

            ->arrayNode('formats')
                ->useAttributeAsKey('format', true)
                ->prototype('array')
                ->beforeNormalization()
                    ->ifTrue(function ($v) { return is_array($v) && !isset($v['mimeTypes']); })
                    ->then(function ($v) { return array('mimeTypes' => $v); })
                ->end()
                    ->children()
                        ->arrayNode('mimeTypes')
                            ->prototype('scalar')->end()
                        ->end()
                    ->end()
                ->end()
            ->end()

返回的数组有"mimeTypes"键,buit允许我定义如下配置:

formats:
    json:
        - application/json
        - application/x-json
        - application/vnd.lemon+json

以及使用"mimeTypes"键。