5
5
The OptionsResolver Component
6
6
=============================
7
7
8
- The OptionsResolver Component helps you at configuring objects with option
8
+ The OptionsResolver Component helps you configure objects with option
9
9
arrays. It supports default values, option constraints and lazy options.
10
10
11
- .. versionadded :: 2.1
12
- The OptionsResolver Component is new in Symfony2.1
13
-
14
11
Installation
15
12
------------
16
13
@@ -26,7 +23,7 @@ Imagine you have a ``Person`` class which has 2 options: ``firstName`` and
26
23
``lastName ``. These options are going to be handled by the OptionsResolver
27
24
Component.
28
25
29
- First of all, you should create some basic skeleton ::
26
+ First, create the `` Person `` class ::
30
27
31
28
class Person
32
29
{
@@ -37,11 +34,11 @@ First of all, you should create some basic skeleton::
37
34
}
38
35
}
39
36
40
- Now, you should handle the ``$options `` parameter with the
41
- :class: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver ` class. To do
42
- this, you should instantiate the `` OptionsResolver `` class and let it resolve
43
- the options by calling
44
- :method: ` Symfony \\ Component \\ OptionsResolver \\ OptionsResolver::resolve ` ::
37
+ You could of course set the ``$options `` value directly on the property. Instead,
38
+ use the :class: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver ` class
39
+ and let it resolve the options by calling
40
+ :method: ` Symfony \\ Component \\ OptionsResolver \\ OptionsResolver::resolve `.
41
+ The advantages of doing this will become more obvious as you continue ::
45
42
46
43
use Symfony\Component\OptionsResolver\OptionsResolver;
47
44
@@ -56,7 +53,7 @@ the options by calling
56
53
The ``$options `` property is an instance of
57
54
:class: `Symfony\\ Component\\ OptionsResolver\\ Options `, which implements
58
55
:phpclass: `ArrayAccess `, :phpclass: `Iterator ` and :phpclass: `Countable `. That
59
- means you can handle it as a normal array::
56
+ means you can handle it just like a normal array::
60
57
61
58
// ...
62
59
public function getFirstName()
@@ -75,7 +72,7 @@ means you can handle it as a normal array::
75
72
return $name;
76
73
}
77
74
78
- Let's use the class::
75
+ Now, try to actually use the class::
79
76
80
77
$person = new Person(array(
81
78
'firstName' => 'Wouter',
@@ -84,18 +81,19 @@ Let's use the class::
84
81
85
82
echo $person->getFirstName();
86
83
87
- As you see , you get a
88
- :class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `
89
- which tells you that the options ``firstName `` and ``lastName `` not exists .
90
- You need to configure the ``OptionsResolver `` first, so it knows which options
91
- should be resolved.
84
+ Right now , you'll receive a
85
+ :class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `,
86
+ which tells you that the options ``firstName `` and ``lastName `` do not exist .
87
+ This is because you need to configure the ``OptionsResolver `` first, so it
88
+ knows which options should be resolved.
92
89
93
90
.. tip ::
94
91
95
92
To check if an option exists, you can use the
96
- :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isKnown ` isser.
93
+ :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isKnown `
94
+ function.
97
95
98
- A best practise is to put the configuration in a method (e.g.
96
+ A best practice is to put the configuration in a method (e.g.
99
97
``setDefaultOptions ``). You call this method in the constructor to configure
100
98
the ``OptionsResolver `` class::
101
99
@@ -123,8 +121,8 @@ the ``OptionsResolver`` class::
123
121
Required Options
124
122
----------------
125
123
126
- The ``firstName `` option is required; the class can't work without that
127
- option . You can set the required options by calling
124
+ Suppose the ``firstName `` option is required: the class can't work without
125
+ it . You can set the required options by calling
128
126
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setRequired `::
129
127
130
128
// ...
@@ -141,7 +139,7 @@ You are now able to use the class without errors::
141
139
142
140
echo $person->getFirstName(); // 'Wouter'
143
141
144
- If you don't pass a required option, an
142
+ If you don't pass a required option, a
145
143
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ MissingOptionsException `
146
144
will be thrown.
147
145
@@ -195,12 +193,12 @@ that is passed has 2 parameters:
195
193
instance), with all the default options
196
194
* ``$value ``, the previous set default value
197
195
198
- Default values that depend on another option
196
+ Default Values that depend on another Option
199
197
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
198
201
- If you add a ``gender `` option to the ``Person `` class, it should get a
202
- default value which guess the gender based on the first name. You can do that
203
- easily by using a Closure as default value::
199
+ Suppose you add a ``gender `` option to the ``Person `` class, whose default
200
+ value you guess based on the first name. You can do that easily by using a
201
+ Closure as the default value::
204
202
205
203
use Symfony\Component\OptionsResolver\Options;
206
204
@@ -222,10 +220,10 @@ easily by using a Closure as default value::
222
220
223
221
.. caution ::
224
222
225
- The first argument of the Closure must be typehinted as `Options `,
223
+ The first argument of the Closure must be typehinted as `` Options ` `,
226
224
otherwise it is considered as the value.
227
225
228
- Configure allowed values
226
+ Configure allowed Values
229
227
------------------------
230
228
231
229
Not all values are valid values for options. For instance, the ``gender ``
@@ -243,10 +241,10 @@ values by calling
243
241
));
244
242
}
245
243
246
- There is also a
244
+ There is also an
247
245
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedValues `
248
246
method, which you can use if you want to add an allowed value to the previous
249
- setted allowed values.
247
+ set allowed values.
250
248
251
249
Configure allowed Types
252
250
~~~~~~~~~~~~~~~~~~~~~~~
@@ -266,18 +264,18 @@ be anything, but it must be a string. You can configure these types by calling
266
264
}
267
265
268
266
Possible types are the one associated with the ``is_* `` php functions or a
269
- class name. You can also pass an array of types as value. For instance,
267
+ class name. You can also pass an array of types as the value. For instance,
270
268
``array('null', 'string') `` allows ``firstName `` to be ``null `` or a
271
269
``string ``.
272
270
273
- There is also a
271
+ There is also an
274
272
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedTypes `
275
273
method, which you can use to add an allowed type to the previous allowed types.
276
274
277
275
Normalize the Options
278
276
---------------------
279
277
280
- Some values needs to be normalized before you can use them. For instance, the
278
+ Some values need to be normalized before you can use them. For instance, the
281
279
``firstName `` should always start with an uppercase letter. To do that, you can
282
280
write normalizers. These Closures will be executed after all options are
283
281
passed and return the normalized value. You can configure these normalizers by
0 commit comments