Skip to content

Commit 0f8f9fd

Browse files
committed
Documented the useAttributeAsKey() method
1 parent a7cff0d commit 0f8f9fd

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

components/config/definition.rst

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,25 +200,58 @@ Array Node Options
200200

201201
Before defining the children of an array node, you can provide options like:
202202

203-
``useAttributeAsKey()``
204-
Provide the name of a child node, whose value should be used as the key in the resulting array.
205-
``requiresAtLeastOneElement()``
206-
There should be at least one element in the array (works only when ``isRequired()`` is also
207-
called).
208203
``addDefaultsIfNotSet()``
209-
If any child nodes have default values, use them if explicit values haven't been provided.
204+
If any child nodes have default values, use them if explicit values haven't
205+
been provided.
206+
``requiresAtLeastOneElement()``
207+
There should be at least one element in the array (works only when
208+
``isRequired()`` is also called).
209+
``useAttributeAsKey()``
210+
Provide the name of a child node, whose value should be used as the key in
211+
the resulting array. This method also defines the way config array keys are
212+
treated, as explained in the following example.
213+
214+
When the ``useAttributeAsKey()`` method is not used, the names of the array
215+
elements (i.e. the array keys) are ignored when parsing the configuration.
216+
Consider this example::
217+
218+
$rootNode
219+
->children()
220+
->arrayNode('parameters')
221+
->prototype('array')
222+
->children()
223+
->scalarNode('parameter1')->end()
224+
->scalarNode('parameter2')->end()
225+
->end()
226+
->end()
227+
->end()
228+
->end()
229+
;
230+
231+
In YAML, the configuration might look like this:
232+
233+
.. code-block:: yaml
234+
235+
database:
236+
parameters: [ 'value1', 'value2' ]
210237
211-
An example of this::
238+
In XML, the configuration might look like this:
239+
240+
.. code-block:: xml
241+
242+
...
243+
244+
However, if the ``useAttributeAsKey()`` method is set, the parsed configuration
245+
will be completely different::
212246

213247
$rootNode
214248
->children()
215249
->arrayNode('parameters')
216-
->isRequired()
217-
->requiresAtLeastOneElement()
218-
->useAttributeAsKey('name')
250+
->useAttributeAsKey('value')
219251
->prototype('array')
220252
->children()
221-
->scalarNode('value')->isRequired()->end()
253+
->scalarNode('parameter1')->end()
254+
->scalarNode('parameter2')->end()
222255
->end()
223256
->end()
224257
->end()
@@ -231,12 +264,19 @@ In YAML, the configuration might look like this:
231264
232265
database:
233266
parameters:
234-
param1: { value: param1val }
267+
parameter1: { value: 'value1' }
268+
parameter2: { value: 'value2' }
269+
270+
In XML, the configuration might look like this:
271+
272+
.. code-block:: xml
273+
274+
...
235275
236-
In XML, each ``parameters`` node would have a ``name`` attribute (along with
276+
In XML, each ``parameters`` node has a ``value`` attribute (along with
237277
``value``), which would be removed and used as the key for that element in
238-
the final array. The ``useAttributeAsKey`` is useful for normalizing how
239-
arrays are specified between different formats like XML and YAML.
278+
the final array. The ``useAttributeAsKey()`` method is useful for normalizing
279+
how arrays are specified between different formats like XML and YAML.
240280

241281
Default and required Values
242282
---------------------------

0 commit comments

Comments
 (0)