5
5
The OptionsResolver Component
6
6
=============================
7
7
8
- The OptionsResolver component is `array_replace() ` on steroids.
8
+ The OptionsResolver component is `array_replace() ` on steroids. It
9
+ allows you to create an options system with required options, defaults,
10
+ validation (type, value), normalization and more.
9
11
10
12
Installation
11
13
------------
@@ -21,7 +23,7 @@ Notes on Previous Versions
21
23
.. versionadded :: 2.6
22
24
This documentation was written for Symfony 2.6 and later. If you use an older
23
25
version, please read the corresponding documentation using the version
24
- drop-down on the upper right.
26
+ drop-down on the upper right. For a list of changes, see the ` CHANGELOG `_
25
27
26
28
Usage
27
29
-----
@@ -65,7 +67,7 @@ check which options are set::
65
67
}
66
68
67
69
This boilerplate is hard to read and repetitive. Also, the default values of the
68
- options are buried in the business logic of your code. We can use
70
+ options are buried in the business logic of your code. We can use the
69
71
:phpfunction: `array_replace ` to fix that::
70
72
71
73
class Mailer
@@ -91,10 +93,9 @@ the ``Mailer`` class does a mistake?
91
93
'usernme' => 'johndoe',
92
94
));
93
95
94
- No error will be shown. In the best case, the bug will be appear during testing.
95
- The developer will possibly spend a lot of time looking for the problem. In the
96
- worst case, however, the bug won't even appear and will be deployed to the live
97
- system.
96
+ No error will be shown. In the best case, the bug will appear during testing,
97
+ but the developer will spend time looking for the problem. In the worst case,
98
+ the bug might not appear until it's deployed to the live system.
98
99
99
100
Let's use the :class: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver `
100
101
class to fix this problem::
@@ -268,8 +269,8 @@ retrieve the names of all required options::
268
269
269
270
If you want to check whether a required option is still missing from the default
270
271
options, you can use :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isMissing `.
271
- The difference to :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isRequired `
272
- is that this method will return false for required options that have already
272
+ The difference between this and :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isRequired `
273
+ is that this method will return false if a required option has already
273
274
been set::
274
275
275
276
// ...
@@ -360,8 +361,8 @@ Value Validation
360
361
Some options can only take one of a fixed list of predefined values. For
361
362
example, suppose the ``Mailer `` class has a ``transport `` option which can be
362
363
one of ``sendmail ``, ``mail `` and ``smtp ``. Use the method
363
- :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setAllowedValues ` to verify
364
- that the passed option contains one of these values::
364
+ :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setAllowedValues `
365
+ to verify that the passed option contains one of these values::
365
366
366
367
// ...
367
368
class Mailer
@@ -470,9 +471,9 @@ Suppose you want to set the default value of the ``port`` option based on the
470
471
encryption chosen by the user of the ``Mailer `` class. More precisely, we want
471
472
to set the port to ``465 `` if SSL is used and to ``25 `` otherwise.
472
473
473
- You can implement this feature by passing a closure as default value of the
474
- ``port `` option. The closure receives the options as argument. Based on these
475
- options, you can return the desired default value::
474
+ You can implement this feature by passing a closure as the default value of
475
+ the ``port `` option. The closure receives the options as argument. Based on
476
+ these options, you can return the desired default value::
476
477
477
478
use Symfony\Component\OptionsResolver\Options;
478
479
@@ -546,8 +547,10 @@ Options without Default Values
546
547
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
547
548
548
549
In some cases, it is useful to define an option without setting a default value.
549
- Mostly, you will need this when you want to know whether an option was passed
550
- or not. If you set a default value for that option, this is not possible::
550
+ This is useful if you need to know whether or not the user *actually * set
551
+ an option or not. For example, if you set the default value for an option,
552
+ it's not possible to know whether the user passed this value or if it simply
553
+ comes from the default::
551
554
552
555
// ...
553
556
class Mailer
@@ -713,3 +716,4 @@ options in your code.
713
716
714
717
.. _Packagist : https://packagist.org/packages/symfony/options-resolver
715
718
.. _Form component : http://symfony.com/doc/current/components/form/introduction.html
719
+ .. _CHANGELOG : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/OptionsResolver/CHANGELOG.md
0 commit comments