@@ -19,13 +19,13 @@ You can install the component in 2 different ways:
19
19
Usage
20
20
-----
21
21
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
24
24
Component.
25
25
26
- First, create the ``Person `` class::
26
+ First, create the ``Mailer `` class::
27
27
28
- class Person
28
+ class Mailer
29
29
{
30
30
protected $options;
31
31
@@ -56,34 +56,28 @@ The ``$options`` property is an instance of
56
56
means you can handle it just like a normal array::
57
57
58
58
// ...
59
- public function getFirstName ()
59
+ public function getHost ()
60
60
{
61
- return $this->options['firstName '];
61
+ return $this->options['host '];
62
62
}
63
63
64
- public function getFullName ()
64
+ public function getPassword ()
65
65
{
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'];
73
67
}
74
68
75
69
Now, try to actually use the class::
76
70
77
- $person = new Person (array(
78
- 'firstName' => 'Wouter ',
79
- 'lastName' => 'de Jong ',
71
+ $mailer = new Mailer (array(
72
+ 'host' => 'smtp.example.org ',
73
+ 'password' => 'pa$$word ',
80
74
));
81
75
82
- echo $person->getFirstName ();
76
+ echo $mailer->getPassword ();
83
77
84
78
Right now, you'll receive a
85
79
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `,
86
- which tells you that the options ``firstName `` and ``lastName `` do not exist.
80
+ which tells you that the options ``host `` and ``password `` do not exist.
87
81
This is because you need to configure the ``OptionsResolver `` first, so it
88
82
knows which options should be resolved.
89
83
@@ -100,7 +94,7 @@ the ``OptionsResolver`` class::
100
94
use Symfony\Component\OptionsResolver\OptionsResolver;
101
95
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
102
96
103
- class Person
97
+ class Mailer
104
98
{
105
99
protected $options;
106
100
@@ -121,23 +115,23 @@ the ``OptionsResolver`` class::
121
115
Required Options
122
116
----------------
123
117
124
- Suppose the `` firstName `` option is required: the class can't work without
125
- it. You can set the required options by calling
118
+ The `` host `` option is required: the class can't work without it. You can set
119
+ the required options by calling
126
120
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setRequired `::
127
121
128
122
// ...
129
123
protected function setDefaultOptions(OptionsResolverInterface $resolver)
130
124
{
131
- $resolver->setRequired(array('firstName '));
125
+ $resolver->setRequired(array('host '));
132
126
}
133
127
134
128
You are now able to use the class without errors::
135
129
136
- $person = new Person (array(
137
- 'firstName ' => 'Wouter ',
130
+ $mailer = new Mailer (array(
131
+ 'host ' => 'smtp.example.org ',
138
132
));
139
133
140
- echo $person->getFirstName (); // 'Wouter '
134
+ echo $person->getHost (); // 'smtp.example.org '
141
135
142
136
If you don't pass a required option, a
143
137
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ MissingOptionsException `
@@ -150,16 +144,16 @@ method.
150
144
Optional Options
151
145
----------------
152
146
153
- Sometimes, an option can be optional (e.g. the ``lastName `` option in the
154
- ``Person `` class). You can configure these options by calling
147
+ Sometimes, an option can be optional (e.g. the ``password `` option in the
148
+ ``Mailer `` class). You can configure these options by calling
155
149
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setOptional `::
156
150
157
151
// ...
158
152
protected function setDefaultOptions(OptionsResolverInterface $resolver)
159
153
{
160
154
// ...
161
155
162
- $resolver->setOptional(array('lastName '));
156
+ $resolver->setOptional(array('password '));
163
157
}
164
158
165
159
Set Default Values
@@ -175,12 +169,12 @@ options by calling
175
169
// ...
176
170
177
171
$resolver->setDefaults(array(
178
- 'age ' => 0 ,
172
+ 'username ' => 'root' ,
179
173
));
180
174
}
181
175
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
176
+ The default username will be ``root `` now. When the user specifies an age, it
177
+ gets replaced. You don't need to configure ``root `` as an optional option. The
184
178
``OptionsResolver `` already knows that options with a default value are
185
179
optional.
186
180
@@ -196,8 +190,8 @@ that is passed has 2 parameters:
196
190
Default Values that depend on another Option
197
191
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198
192
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
193
+ Suppose you add a ``port `` option to the ``Mailer `` class, whose default
194
+ value you guess based on the host . You can do that easily by using a
201
195
Closure as the default value::
202
196
203
197
use Symfony\Component\OptionsResolver\Options;
@@ -208,12 +202,12 @@ Closure as the default value::
208
202
// ...
209
203
210
204
$resolver->setDefaults(array(
211
- 'gender ' => function (Options $options) {
212
- if (GenderGuesser::isMale ($options['firstName'] )) {
213
- return 'male' ;
205
+ 'port ' => function (Options $options) {
206
+ if (in_array ($options['host'], array('127.0.0.1', 'localhost' )) {
207
+ return 80 ;
214
208
}
215
209
216
- return 'female' ;
210
+ return 25 ;
217
211
},
218
212
));
219
213
}
@@ -226,9 +220,9 @@ Closure as the default value::
226
220
Configure allowed Values
227
221
------------------------
228
222
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
223
+ Not all values are valid values for options. Suppose the ``Mailer `` class has
224
+ a `` transport `` option, it can only be one of `` sendmail ``, `` mail `` or
225
+ `` smtp ``. You can configure these allowed values by calling
232
226
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setAllowedValues `::
233
227
234
228
// ...
@@ -237,7 +231,7 @@ values by calling
237
231
// ...
238
232
239
233
$resolver->setAllowedValues(array(
240
- 'gender ' => array('male ', 'female '),
234
+ 'transport ' => array('sendmail ', 'mail', 'smtp '),
241
235
));
242
236
}
243
237
@@ -249,8 +243,8 @@ set allowed values.
249
243
Configure allowed Types
250
244
~~~~~~~~~~~~~~~~~~~~~~~
251
245
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
246
+ You can also specify allowed types. For instance, the ``port `` option can
247
+ be anything, but it must be an integer . You can configure these types by calling
254
248
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setAllowedTypes `::
255
249
256
250
// ...
@@ -259,14 +253,13 @@ be anything, but it must be a string. You can configure these types by calling
259
253
// ...
260
254
261
255
$resolver->setAllowedTypes(array(
262
- 'firstName ' => 'string ',
256
+ 'port ' => 'integer ',
263
257
));
264
258
}
265
259
266
- Possible types are the one associated with the ``is_* `` php functions or a
260
+ Possible types are the ones associated with the ``is_* `` php functions or a
267
261
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 ``.
262
+ ``array('null', 'string') `` allows ``port `` to be ``null `` or a ``string ``.
270
263
271
264
There is also an
272
265
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedTypes `
@@ -276,7 +269,7 @@ Normalize the Options
276
269
---------------------
277
270
278
271
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
272
+ ``host `` should always start `` http:// `` . To do that, you can
280
273
write normalizers. These Closures will be executed after all options are
281
274
passed and return the normalized value. You can configure these normalizers by
282
275
calling
@@ -288,13 +281,37 @@ calling
288
281
// ...
289
282
290
283
$resolver->setNormalizers(array(
291
- 'firstName' => function (Options $options, $value) {
292
- return ucfirst($value);
284
+ 'host' => function (Options $options, $value) {
285
+ if ('http://' !== substr($value, 0, 7)) {
286
+ $value = 'http://'.$value;
287
+ }
288
+
289
+ return $value;
293
290
},
294
291
));
295
292
}
296
293
297
294
You see that the closure also get an ``$options `` parameter. Sometimes, you
298
- need to use the other options for normalizing.
295
+ need to use the other options for normalizing::
296
+
297
+ // ...
298
+ protected function setDefaultOptions(OptionsResolverInterface $resolver)
299
+ {
300
+ // ...
301
+
302
+ $resolver->setNormalizers(array(
303
+ 'host' => function (Options $options, $value) {
304
+ if (!in_array(substr($value, 0, 7), array('http://', 'https://')) {
305
+ if ($options['ssl']) {
306
+ $value = 'https://'.$value;
307
+ } else {
308
+ $value = 'http://'.$value;
309
+ }
310
+ }
311
+
312
+ return $value;
313
+ },
314
+ ));
315
+ }
299
316
300
317
.. _Packagist : https://packagist.org/packages/symfony/options-resolver
0 commit comments