-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documented the useAttributeAsKey() method #5314
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,25 +200,58 @@ Array Node Options | |
|
||
Before defining the children of an array node, you can provide options like: | ||
|
||
``useAttributeAsKey()`` | ||
Provide the name of a child node, whose value should be used as the key in the resulting array. | ||
``requiresAtLeastOneElement()`` | ||
There should be at least one element in the array (works only when ``isRequired()`` is also | ||
called). | ||
``addDefaultsIfNotSet()`` | ||
If any child nodes have default values, use them if explicit values haven't been provided. | ||
If any child nodes have default values, use them if explicit values haven't | ||
been provided. | ||
``requiresAtLeastOneElement()`` | ||
There should be at least one element in the array (works only when | ||
``isRequired()`` is also called). | ||
``useAttributeAsKey()`` | ||
Provide the name of a child node, whose value should be used as the key in | ||
the resulting array. This method also defines the way config array keys are | ||
treated, as explained in the following example. | ||
|
||
When the ``useAttributeAsKey()`` method is not used, the names of the array | ||
elements (i.e. the array keys) are ignored when parsing the configuration. | ||
Consider this example:: | ||
|
||
$rootNode | ||
->children() | ||
->arrayNode('parameters') | ||
->prototype('array') | ||
->children() | ||
->scalarNode('parameter1')->end() | ||
->scalarNode('parameter2')->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
In YAML, the configuration might look like this: | ||
|
||
.. code-block:: yaml | ||
|
||
database: | ||
parameters: [ 'value1', 'value2' ] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this also possible? drivers:
- mysql
- sqlite There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it is, because this is exactly the same in YAML, so the Config component receives the same input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, but we don't want to write this example explicitly, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no need in doing that, this chapter is teaching you the Config component, not the Yaml syntax :) |
||
An example of this:: | ||
In XML, the configuration might look like this: | ||
|
||
.. code-block:: xml | ||
|
||
... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <parameter>value1</parameter>
<parameter>value2</parameter> |
||
|
||
However, if the ``useAttributeAsKey()`` method is set, the parsed configuration | ||
will be completely different:: | ||
|
||
$rootNode | ||
->children() | ||
->arrayNode('parameters') | ||
->isRequired() | ||
->requiresAtLeastOneElement() | ||
->useAttributeAsKey('name') | ||
->useAttributeAsKey('value') | ||
->prototype('array') | ||
->children() | ||
->scalarNode('value')->isRequired()->end() | ||
->scalarNode('parameter1')->end() | ||
->scalarNode('parameter2')->end() | ||
->end() | ||
->end() | ||
->end() | ||
|
@@ -231,12 +264,19 @@ In YAML, the configuration might look like this: | |
|
||
database: | ||
parameters: | ||
param1: { value: param1val } | ||
parameter1: { value: 'value1' } | ||
parameter2: { value: 'value2' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is wrong, it'll be: parameters:
parameter1: value1
parameter2: value2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, the example seems very weird... I'll post a longer comment |
||
|
||
In XML, the configuration might look like this: | ||
|
||
.. code-block:: xml | ||
|
||
... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <parameter value="parameter1">value1</parameter>
<parameter value="parameter2">value2</parameter> All in all, I think you should use |
||
|
||
In XML, each ``parameters`` node would have a ``name`` attribute (along with | ||
In XML, each ``parameters`` node has a ``value`` attribute (along with | ||
``value``), which would be removed and used as the key for that element in | ||
the final array. The ``useAttributeAsKey`` is useful for normalizing how | ||
arrays are specified between different formats like XML and YAML. | ||
the final array. The ``useAttributeAsKey()`` method is useful for normalizing | ||
how arrays are specified between different formats like XML and YAML. | ||
|
||
Default and required Values | ||
--------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
->fixXmlConfig('parameter')
before line 219