@@ -109,31 +109,33 @@ Array nodes
109
109
~~~~~~~~~~~
110
110
111
111
It is possible to add a deeper level to the hierarchy, by adding an array
112
- node. The array node itself, may have a pre-defined set of variable nodes:
113
-
114
- .. code-block :: php
112
+ node. The array node itself, may have a pre-defined set of variable nodes::
115
113
116
114
$rootNode
117
- ->arrayNode('connection')
118
- ->scalarNode('driver')->end()
119
- ->scalarNode('host')->end()
120
- ->scalarNode('username')->end()
121
- ->scalarNode('password')->end()
115
+ ->children()
116
+ ->arrayNode('connection')
117
+ ->children()
118
+ ->scalarNode('driver')->end()
119
+ ->scalarNode('host')->end()
120
+ ->scalarNode('username')->end()
121
+ ->scalarNode('password')->end()
122
+ ->end()
123
+ ->end()
122
124
->end()
123
125
;
124
126
125
- Or you may define a prototype for each node inside an array node:
126
-
127
- .. code-block :: php
127
+ Or you may define a prototype for each node inside an array node::
128
128
129
129
$rootNode
130
- ->arrayNode('connections')
131
- ->prototype('array')
130
+ ->children()
131
+ ->arrayNode('connections')
132
+ ->prototype('array')
132
133
->children()
133
134
->scalarNode('driver')->end()
134
135
->scalarNode('host')->end()
135
136
->scalarNode('username')->end()
136
137
->scalarNode('password')->end()
138
+ ->end()
137
139
->end()
138
140
->end()
139
141
->end()
@@ -155,24 +157,36 @@ Before defining the children of an array node, you can provide options like:
155
157
There should be at least one element in the array (works only when ``isRequired() `` is also
156
158
called).
157
159
158
- An example of this:
159
-
160
- .. code-block :: php
160
+ An example of this::
161
161
162
162
$rootNode
163
- ->arrayNode('parameters')
164
- ->isRequired()
165
- ->requiresAtLeastOneElement()
166
- ->useAttributeAsKey('name')
167
- ->prototype('array')
168
- ->children()
169
- ->scalarNode('name')->isRequired()->end()
170
- ->scalarNode('value')->isRequired()->end()
163
+ ->children()
164
+ ->arrayNode('parameters')
165
+ ->isRequired()
166
+ ->requiresAtLeastOneElement()
167
+ ->useAttributeAsKey('name')
168
+ ->prototype('array')
169
+ ->children()
170
+ ->scalarNode('value')->isRequired()->end()
171
+ ->end()
171
172
->end()
172
173
->end()
173
174
->end()
174
175
;
175
176
177
+ In YAML, the configuration might look like this:
178
+
179
+ .. code-block :: yaml
180
+
181
+ database :
182
+ parameters :
183
+ param1 : { value: param1val }
184
+
185
+ In XML, each ``parameters `` node would have a ``name `` attribute (along with
186
+ ``value ``), which would be removed and used as the key for that element in
187
+ the final array. The ``useAttributeAsKey `` is useful for normalizing how
188
+ arrays are specified between different formats like XML and YAML.
189
+
176
190
Default and required values
177
191
---------------------------
178
192
@@ -194,19 +208,21 @@ has a certain value:
194
208
.. code-block :: php
195
209
196
210
$rootNode
197
- ->arrayNode('connection')
198
- ->children()
199
- ->scalarNode('driver')
200
- ->isRequired()
201
- ->cannotBeEmpty()
202
- ->end()
203
- ->scalarNode('host')
204
- ->defaultValue('localhost')
205
- ->end()
206
- ->scalarNode('username')->end()
207
- ->scalarNode('password')->end()
208
- ->booleanNode('memory')
209
- ->defaultFalse()
211
+ ->children()
212
+ ->arrayNode('connection')
213
+ ->children()
214
+ ->scalarNode('driver')
215
+ ->isRequired()
216
+ ->cannotBeEmpty()
217
+ ->end()
218
+ ->scalarNode('host')
219
+ ->defaultValue('localhost')
220
+ ->end()
221
+ ->scalarNode('username')->end()
222
+ ->scalarNode('password')->end()
223
+ ->booleanNode('memory')
224
+ ->defaultFalse()
225
+ ->end()
210
226
->end()
211
227
->end()
212
228
->end()
@@ -240,22 +256,24 @@ with ``append()``::
240
256
$rootNode = $treeBuilder->root('database');
241
257
242
258
$rootNode
243
- ->arrayNode('connection')
244
- ->children()
245
- ->scalarNode('driver')
246
- ->isRequired()
247
- ->cannotBeEmpty()
248
- ->end()
249
- ->scalarNode('host')
250
- ->defaultValue('localhost')
251
- ->end()
252
- ->scalarNode('username')->end()
253
- ->scalarNode('password')->end()
254
- ->booleanNode('memory')
255
- ->defaultFalse()
259
+ ->children()
260
+ ->arrayNode('connection')
261
+ ->children()
262
+ ->scalarNode('driver')
263
+ ->isRequired()
264
+ ->cannotBeEmpty()
265
+ ->end()
266
+ ->scalarNode('host')
267
+ ->defaultValue('localhost')
268
+ ->end()
269
+ ->scalarNode('username')->end()
270
+ ->scalarNode('password')->end()
271
+ ->booleanNode('memory')
272
+ ->defaultFalse()
273
+ ->end()
256
274
->end()
275
+ ->append($this->addParametersNode())
257
276
->end()
258
- ->append($this->addParametersNode())
259
277
->end()
260
278
;
261
279
@@ -273,7 +291,6 @@ with ``append()``::
273
291
->useAttributeAsKey('name')
274
292
->prototype('array')
275
293
->children()
276
- ->scalarNode('name')->isRequired()->end()
277
294
->scalarNode('value')->isRequired()->end()
278
295
->end()
279
296
->end()
@@ -378,13 +395,17 @@ you can allow the following as well:
378
395
By changing a string value into an associative array with ``name `` as the key::
379
396
380
397
$rootNode
381
- ->arrayNode('connection')
382
- ->beforeNormalization()
383
- ->ifString()
384
- ->then(function($v) { return array('name'=> $v); })
385
- ->end()
386
- ->scalarNode('name')->isRequired()
387
- // ...
398
+ ->children()
399
+ ->arrayNode('connection')
400
+ ->beforeNormalization()
401
+ ->ifString()
402
+ ->then(function($v) { return array('name'=> $v); })
403
+ ->end()
404
+ ->children()
405
+ ->scalarNode('name')->isRequired()
406
+ // ...
407
+ ->end()
408
+ ->end()
388
409
->end()
389
410
;
390
411
@@ -397,13 +418,15 @@ builder implements a fluent interface for a well-known control structure.
397
418
The builder is used for adding advanced validation rules to node definitions, like::
398
419
399
420
$rootNode
400
- ->arrayNode('connection')
401
- ->children()
402
- ->scalarNode('driver')
403
- ->isRequired()
404
- ->validate()
421
+ ->children()
422
+ ->arrayNode('connection')
423
+ ->children()
424
+ ->scalarNode('driver')
425
+ ->isRequired()
426
+ ->validate()
405
427
->ifNotInArray(array('mysql', 'sqlite', 'mssql'))
406
- ->thenInvalid('Invalid database driver "%s"')
428
+ ->thenInvalid('Invalid database driver "%s"')
429
+ ->end()
407
430
->end()
408
431
->end()
409
432
->end()
0 commit comments