Symfony配置树构建器useAttributeAsKey无法按预期工作

时间:2018-11-17 11:02:14

标签: php symfony symfony4

抱歉,我所知道的标题没有我想要的解释。

我正在为分发包创建一个配置文件,并且希望以多种方式定义en元素。

这些是我想要定义条目的不同方式:

properties:
  property1: ~
  property2: ~
  property3: Custom label3
  property4:
    label: Custom label4
    type: int

properties:
  - property1
  - property2
  - { property: property3, label: Custom label3 }
  - { property: property4, label: Custom label4, type: int }

两种配置都应具有以下结构:

"properties" => [
  "property1" => [
     "label": null
     "type": null
  ], 
  "property2" => [
     "label": null
     "type": null
  ], 
  "property3" => [
     "label": "Custom label 3"
     "type": null
  ], 
  "property4" => [
     "label": "Custom label 4"
     "type": "int"
  ], 
]

这是我当前拥有的代码:

->arrayNode('properties')
    ->useAttributeAsKey('property')
    ->arrayPrototype()
        ->beforeNormalization()
            ->ifString()->then(function($item) { return ['property' => $item]; })
        ->end()
        ->children()
            ->scalarNode('property')->defaultNull()->end()
            ->scalarNode('label')->defaultNull()->end()
            ->scalarNode('type')->defaultNull()->end()
        ->end()
    ->end()
->end()

请问我收到的第一个代码如下结果,这几乎是完美的:

"properties" => array:4 [
  "property1" => array:3 [
    "property" => null
    "label" => null
    "type" => null
  ]
  "property2" => array:3 [
    "property" => null
    "label" => null
    "type" => null
  ]
  "property3" => array:3 [
    "property" => "Custom label3"
    "label" => null
    "type" => null
  ]
  "property4" => array:3 [
    "label" => "Custom label4"
    "type" => "int"
    "property" => null
  ]
]

在每个条目中都有"property" => null如何删除密钥中已经存在的属性?

在第二个示例中,我收到以下结果:

"properties" => array:4 [
  0 => array:3 [
    "property" => "property1"
    "label" => null
    "type" => null
  ]
  1 => array:3 [
    "property" => "property2"
    "label" => null
    "type" => null
  ]
  "property3" => array:3 [
    "label" => "Custom label 3"
    "property" => null
    "type" => null
  ]
  "property4" => array:3 [
    "label" => "Custom label 4"
    "type" => "int"
    "property" => null
  ]
]

如您所见,前两个条目没有property作为键,它的编号类似于普通数组。 我该如何解决?

谢谢

0 个答案:

没有答案