Skip to content

Commit 4215763

Browse files
committed
Merge branch '2.1' into 2.2
2 parents 42f80e0 + cbb304f commit 4215763

File tree

1 file changed

+86
-65
lines changed

1 file changed

+86
-65
lines changed

components/options_resolver.rst

+86-65
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ You can install the component in 2 different ways:
1919
Usage
2020
-----
2121

22-
Imagine you have a ``Person`` class which has 2 options: ``firstName`` and
23-
``lastName``. These options are going to be handled by the OptionsResolver
22+
Imagine you have a ``Mailer`` class which has 2 options: ``host`` and
23+
``password``. These options are going to be handled by the OptionsResolver
2424
Component.
2525

26-
First, create the ``Person`` class::
26+
First, create the ``Mailer`` class::
2727

28-
class Person
28+
class Mailer
2929
{
3030
protected $options;
3131

@@ -56,34 +56,31 @@ The ``$options`` property is an instance of
5656
means you can handle it just like a normal array::
5757

5858
// ...
59-
public function getFirstName()
59+
public function getHost()
6060
{
61-
return $this->options['firstName'];
61+
return $this->options['host'];
6262
}
6363

64-
public function getFullName()
64+
public function getPassword()
6565
{
66-
$name = $this->options['firstName'];
67-
68-
if (isset($this->options['lastName'])) {
69-
$name .= ' '.$this->options['lastName'];
70-
}
71-
72-
return $name;
66+
return $this->options['password'];
7367
}
7468

69+
Configuring the OptionsResolver
70+
-------------------------------
71+
7572
Now, try to actually use the class::
7673

77-
$person = new Person(array(
78-
'firstName' => 'Wouter',
79-
'lastName' => 'de Jong',
74+
$mailer = new Mailer(array(
75+
'host' => 'smtp.example.org',
76+
'password' => 'pa$$word',
8077
));
8178

82-
echo $person->getFirstName();
79+
echo $mailer->getPassword();
8380

8481
Right now, you'll receive a
8582
:class:`Symfony\\Component\\OptionsResolver\\Exception\\InvalidOptionsException`,
86-
which tells you that the options ``firstName`` and ``lastName`` do not exist.
83+
which tells you that the options ``host`` and ``password`` do not exist.
8784
This is because you need to configure the ``OptionsResolver`` first, so it
8885
knows which options should be resolved.
8986

@@ -100,7 +97,7 @@ the ``OptionsResolver`` class::
10097
use Symfony\Component\OptionsResolver\OptionsResolver;
10198
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
10299

103-
class Person
100+
class Mailer
104101
{
105102
protected $options;
106103

@@ -119,25 +116,25 @@ the ``OptionsResolver`` class::
119116
}
120117

121118
Required Options
122-
----------------
119+
~~~~~~~~~~~~~~~~
123120

124-
Suppose the ``firstName`` option is required: the class can't work without
125-
it. You can set the required options by calling
121+
The ``host`` option is required: the class can't work without it. You can set
122+
the required options by calling
126123
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setRequired`::
127124

128125
// ...
129126
protected function setDefaultOptions(OptionsResolverInterface $resolver)
130127
{
131-
$resolver->setRequired(array('firstName'));
128+
$resolver->setRequired(array('host'));
132129
}
133130

134131
You are now able to use the class without errors::
135132

136-
$person = new Person(array(
137-
'firstName' => 'Wouter',
133+
$mailer = new Mailer(array(
134+
'host' => 'smtp.example.org',
138135
));
139136

140-
echo $person->getFirstName(); // 'Wouter'
137+
echo $mailer->getHost(); // 'smtp.example.org'
141138

142139
If you don't pass a required option, a
143140
:class:`Symfony\\Component\\OptionsResolver\\Exception\\MissingOptionsException`
@@ -148,22 +145,22 @@ To determine if an option is required, you can use the
148145
method.
149146

150147
Optional Options
151-
----------------
148+
~~~~~~~~~~~~~~~~
152149

153-
Sometimes, an option can be optional (e.g. the ``lastName`` option in the
154-
``Person`` class). You can configure these options by calling
150+
Sometimes, an option can be optional (e.g. the ``password`` option in the
151+
``Mailer`` class). You can configure these options by calling
155152
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setOptional`::
156153

157154
// ...
158155
protected function setDefaultOptions(OptionsResolverInterface $resolver)
159156
{
160157
// ...
161158

162-
$resolver->setOptional(array('lastName'));
159+
$resolver->setOptional(array('password'));
163160
}
164161

165162
Set Default Values
166-
------------------
163+
~~~~~~~~~~~~~~~~~~
167164

168165
Most of the optional options have a default value. You can configure these
169166
options by calling
@@ -175,14 +172,15 @@ options by calling
175172
// ...
176173

177174
$resolver->setDefaults(array(
178-
'age' => 0,
175+
'username' => 'root',
179176
));
180177
}
181178

182-
The default age will be ``0`` now. When the user specifies an age, it gets
183-
replaced. You don't need to configure ``age`` as an optional option. The
184-
``OptionsResolver`` already knows that options with a default value are
185-
optional.
179+
This would add a third option - ``username`` - and give it a default value
180+
of ``root``. If the user passes in a ``username`` option, that value will
181+
override this default. You don't need to configure ``username`` as an optional
182+
option. The ``OptionsResolver`` already knows that options with a default
183+
value are optional.
186184

187185
The ``OptionsResolver`` component also has an
188186
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::replaceDefaults`
@@ -196,8 +194,8 @@ that is passed has 2 parameters:
196194
Default Values that depend on another Option
197195
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198196

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
197+
Suppose you add a ``port`` option to the ``Mailer`` class, whose default
198+
value you guess based on the host. You can do that easily by using a
201199
Closure as the default value::
202200

203201
use Symfony\Component\OptionsResolver\Options;
@@ -208,12 +206,12 @@ Closure as the default value::
208206
// ...
209207

210208
$resolver->setDefaults(array(
211-
'gender' => function (Options $options) {
212-
if (GenderGuesser::isMale($options['firstName'])) {
213-
return 'male';
209+
'port' => function (Options $options) {
210+
if (in_array($options['host'], array('127.0.0.1', 'localhost')) {
211+
return 80;
214212
}
215213
216-
return 'female';
214+
return 25;
217215
},
218216
));
219217
}
@@ -224,11 +222,11 @@ Closure as the default value::
224222
otherwise it is considered as the value.
225223

226224
Configure allowed Values
227-
------------------------
225+
~~~~~~~~~~~~~~~~~~~~~~~~
228226

229-
Not all values are valid values for options. For instance, the ``gender``
230-
option can only be ``female`` or ``male``. You can configure these allowed
231-
values by calling
227+
Not all values are valid values for options. Suppose the ``Mailer`` class has
228+
a ``transport`` option, it can only be one of ``sendmail``, ``mail`` or
229+
``smtp``. You can configure these allowed values by calling
232230
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setAllowedValues`::
233231

234232
// ...
@@ -237,20 +235,20 @@ values by calling
237235
// ...
238236

239237
$resolver->setAllowedValues(array(
240-
'gender' => array('male', 'female'),
238+
'transport' => array('sendmail', 'mail', 'smtp'),
241239
));
242240
}
243241

244242
There is also an
245243
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`
246-
method, which you can use if you want to add an allowed value to the previous
244+
method, which you can use if you want to add an allowed value to the previously
247245
set allowed values.
248246

249247
Configure allowed Types
250248
~~~~~~~~~~~~~~~~~~~~~~~
251249

252-
You can also specify allowed types. For instance, the ``firstName`` option can
253-
be anything, but it must be a string. You can configure these types by calling
250+
You can also specify allowed types. For instance, the ``port`` option can
251+
be anything, but it must be an integer. You can configure these types by calling
254252
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setAllowedTypes`::
255253

256254
// ...
@@ -259,27 +257,26 @@ be anything, but it must be a string. You can configure these types by calling
259257
// ...
260258

261259
$resolver->setAllowedTypes(array(
262-
'firstName' => 'string',
260+
'port' => 'integer',
263261
));
264262
}
265263

266-
Possible types are the one associated with the ``is_*`` php functions or a
264+
Possible types are the ones associated with the ``is_*`` php functions or a
267265
class name. You can also pass an array of types as the value. For instance,
268-
``array('null', 'string')`` allows ``firstName`` to be ``null`` or a
269-
``string``.
266+
``array('null', 'string')`` allows ``port`` to be ``null`` or a ``string``.
270267

271268
There is also an
272269
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedTypes`
273270
method, which you can use to add an allowed type to the previous allowed types.
274271

275272
Normalize the Options
276-
---------------------
273+
~~~~~~~~~~~~~~~~~~~~~
277274

278-
Some values need to be normalized before you can use them. For instance, the
279-
``firstName`` should always start with an uppercase letter. To do that, you can
280-
write normalizers. These Closures will be executed after all options are
281-
passed and return the normalized value. You can configure these normalizers by
282-
calling
275+
Some values need to be normalized before you can use them. For instance,
276+
pretend that the ``host`` should always start with ``http://``. To do that,
277+
you can write normalizers. These Closures will be executed after all options
278+
are passed and should return the normalized value. You can configure these
279+
normalizers by calling
283280
:method:`Symfony\\Components\\OptionsResolver\\OptionsResolver::setNormalizers`::
284281

285282
// ...
@@ -288,13 +285,37 @@ calling
288285
// ...
289286

290287
$resolver->setNormalizers(array(
291-
'firstName' => function (Options $options, $value) {
292-
return ucfirst($value);
288+
'host' => function (Options $options, $value) {
289+
if ('http://' !== substr($value, 0, 7)) {
290+
$value = 'http://'.$value;
291+
}
292+
293+
return $value;
293294
},
294295
));
295296
}
296297

297-
You see that the closure also get an ``$options`` parameter. Sometimes, you
298-
need to use the other options for normalizing.
298+
You see that the closure also gets an ``$options`` parameter. Sometimes, you
299+
need to use the other options for normalizing::
300+
301+
// ...
302+
protected function setDefaultOptions(OptionsResolverInterface $resolver)
303+
{
304+
// ...
305+
306+
$resolver->setNormalizers(array(
307+
'host' => function (Options $options, $value) {
308+
if (!in_array(substr($value, 0, 7), array('http://', 'https://')) {
309+
if ($options['ssl']) {
310+
$value = 'https://'.$value;
311+
} else {
312+
$value = 'http://'.$value;
313+
}
314+
}
315+
316+
return $value;
317+
},
318+
));
319+
}
299320

300321
.. _Packagist: https://packagist.org/packages/symfony/options-resolver

0 commit comments

Comments
 (0)