Skip to content
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

Merged
merged 3 commits into from
Oct 14, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions components/config/definition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Member

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

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this also possible?

drivers:
  - mysql
  - sqlite

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but we don't want to write this example explicitly, too?

Copy link
Member

Choose a reason for hiding this comment

The 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

...
Copy link
Member

Choose a reason for hiding this comment

The 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()
Expand All @@ -231,12 +264,19 @@ In YAML, the configuration might look like this:

database:
parameters:
param1: { value: param1val }
parameter1: { value: 'value1' }
parameter2: { value: 'value2' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong, it'll be:

parameters:
    parameter1: value1
    parameter2: value2

Copy link
Member

Choose a reason for hiding this comment

The 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

...
Copy link
Member

Choose a reason for hiding this comment

The 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 ->useAttributeAsKey('name') or the like and then use the name attribute


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
---------------------------
Expand Down