55The OptionsResolver Component
66=============================
77
8- The OptionsResolver Component helps you at configuring objects with option
8+ The OptionsResolver Component helps you configure objects with option
99 arrays. It supports default values, option constraints and lazy options.
1010
11- .. versionadded :: 2.1
12- The OptionsResolver Component is new in Symfony2.1
13-
1411Installation
1512------------
1613
@@ -26,7 +23,7 @@ Imagine you have a ``Person`` class which has 2 options: ``firstName`` and
2623``lastName ``. These options are going to be handled by the OptionsResolver
2724Component.
2825
29- First of all, you should create some basic skeleton ::
26+ First, create the `` Person `` class ::
3027
3128 class Person
3229 {
@@ -37,11 +34,11 @@ First of all, you should create some basic skeleton::
3734 }
3835 }
3936
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 ::
4542
4643 use Symfony\Component\OptionsResolver\OptionsResolver;
4744
@@ -56,7 +53,7 @@ the options by calling
5653The ``$options `` property is an instance of
5754:class: `Symfony\\ Component\\ OptionsResolver\\ Options `, which implements
5855: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::
6057
6158 // ...
6259 public function getFirstName()
@@ -75,7 +72,7 @@ means you can handle it as a normal array::
7572 return $name;
7673 }
7774
78- Let's use the class::
75+ Now, try to actually use the class::
7976
8077 $person = new Person(array(
8178 'firstName' => 'Wouter',
@@ -84,18 +81,19 @@ Let's use the class::
8481
8582 echo $person->getFirstName();
8683
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.
9289
9390.. tip ::
9491
9592 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.
9795
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.
9997``setDefaultOptions ``). You call this method in the constructor to configure
10098the ``OptionsResolver `` class::
10199
@@ -123,8 +121,8 @@ the ``OptionsResolver`` class::
123121Required Options
124122----------------
125123
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
128126:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setRequired `::
129127
130128 // ...
@@ -141,7 +139,7 @@ You are now able to use the class without errors::
141139
142140 echo $person->getFirstName(); // 'Wouter'
143141
144- If you don't pass a required option, an
142+ If you don't pass a required option, a
145143:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ MissingOptionsException `
146144will be thrown.
147145
@@ -195,12 +193,12 @@ that is passed has 2 parameters:
195193 instance), with all the default options
196194* ``$value ``, the previous set default value
197195
198- Default values that depend on another option
196+ Default Values that depend on another Option
199197~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200198
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::
204202
205203 use Symfony\Component\OptionsResolver\Options;
206204
@@ -222,10 +220,10 @@ easily by using a Closure as default value::
222220
223221.. caution ::
224222
225- The first argument of the Closure must be typehinted as `Options `,
223+ The first argument of the Closure must be typehinted as `` Options ` `,
226224 otherwise it is considered as the value.
227225
228- Configure allowed values
226+ Configure allowed Values
229227------------------------
230228
231229Not all values are valid values for options. For instance, the ``gender ``
@@ -243,10 +241,10 @@ values by calling
243241 ));
244242 }
245243
246- There is also a
244+ There is also an
247245:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedValues `
248246method, which you can use if you want to add an allowed value to the previous
249- setted allowed values.
247+ set allowed values.
250248
251249Configure allowed Types
252250~~~~~~~~~~~~~~~~~~~~~~~
@@ -266,18 +264,18 @@ be anything, but it must be a string. You can configure these types by calling
266264 }
267265
268266Possible 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,
270268``array('null', 'string') `` allows ``firstName `` to be ``null `` or a
271269``string ``.
272270
273- There is also a
271+ There is also an
274272:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedTypes `
275273method, which you can use to add an allowed type to the previous allowed types.
276274
277275Normalize the Options
278276---------------------
279277
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
281279``firstName `` should always start with an uppercase letter. To do that, you can
282280write normalizers. These Closures will be executed after all options are
283281passed and return the normalized value. You can configure these normalizers by
0 commit comments