@@ -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,31 @@ 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
69
+ Configuring the OptionsResolver
70
+ -------------------------------
71
+
75
72
Now, try to actually use the class::
76
73
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 ',
80
77
));
81
78
82
- echo $person->getFirstName ();
79
+ echo $mailer->getPassword ();
83
80
84
81
Right now, you'll receive a
85
82
: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.
87
84
This is because you need to configure the ``OptionsResolver `` first, so it
88
85
knows which options should be resolved.
89
86
@@ -100,7 +97,7 @@ the ``OptionsResolver`` class::
100
97
use Symfony\Component\OptionsResolver\OptionsResolver;
101
98
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
102
99
103
- class Person
100
+ class Mailer
104
101
{
105
102
protected $options;
106
103
@@ -119,25 +116,25 @@ the ``OptionsResolver`` class::
119
116
}
120
117
121
118
Required Options
122
- ----------------
119
+ ~~~~~~~~~~~~~~~~
123
120
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
126
123
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setRequired `::
127
124
128
125
// ...
129
126
protected function setDefaultOptions(OptionsResolverInterface $resolver)
130
127
{
131
- $resolver->setRequired(array('firstName '));
128
+ $resolver->setRequired(array('host '));
132
129
}
133
130
134
131
You are now able to use the class without errors::
135
132
136
- $person = new Person (array(
137
- 'firstName ' => 'Wouter ',
133
+ $mailer = new Mailer (array(
134
+ 'host ' => 'smtp.example.org ',
138
135
));
139
136
140
- echo $person->getFirstName (); // 'Wouter '
137
+ echo $mailer->getHost (); // 'smtp.example.org '
141
138
142
139
If you don't pass a required option, a
143
140
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ MissingOptionsException `
@@ -148,22 +145,22 @@ To determine if an option is required, you can use the
148
145
method.
149
146
150
147
Optional Options
151
- ----------------
148
+ ~~~~~~~~~~~~~~~~
152
149
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
155
152
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setOptional `::
156
153
157
154
// ...
158
155
protected function setDefaultOptions(OptionsResolverInterface $resolver)
159
156
{
160
157
// ...
161
158
162
- $resolver->setOptional(array('lastName '));
159
+ $resolver->setOptional(array('password '));
163
160
}
164
161
165
162
Set Default Values
166
- ------------------
163
+ ~~~~~~~~~~~~~~~~~~
167
164
168
165
Most of the optional options have a default value. You can configure these
169
166
options by calling
@@ -175,14 +172,15 @@ options by calling
175
172
// ...
176
173
177
174
$resolver->setDefaults(array(
178
- 'age ' => 0 ,
175
+ 'username ' => 'root' ,
179
176
));
180
177
}
181
178
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.
186
184
187
185
The ``OptionsResolver `` component also has an
188
186
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::replaceDefaults `
@@ -196,8 +194,8 @@ that is passed has 2 parameters:
196
194
Default Values that depend on another Option
197
195
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198
196
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
201
199
Closure as the default value::
202
200
203
201
use Symfony\Component\OptionsResolver\Options;
@@ -208,12 +206,12 @@ Closure as the default value::
208
206
// ...
209
207
210
208
$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 ;
214
212
}
215
213
216
- return 'female' ;
214
+ return 25 ;
217
215
},
218
216
));
219
217
}
@@ -224,11 +222,11 @@ Closure as the default value::
224
222
otherwise it is considered as the value.
225
223
226
224
Configure allowed Values
227
- ------------------------
225
+ ~~~~~~~~~~~~~~~~~~~~~~~~
228
226
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
232
230
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setAllowedValues `::
233
231
234
232
// ...
@@ -237,20 +235,20 @@ values by calling
237
235
// ...
238
236
239
237
$resolver->setAllowedValues(array(
240
- 'gender ' => array('male ', 'female '),
238
+ 'transport ' => array('sendmail ', 'mail', 'smtp '),
241
239
));
242
240
}
243
241
244
242
There is also an
245
243
: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
247
245
set allowed values.
248
246
249
247
Configure allowed Types
250
248
~~~~~~~~~~~~~~~~~~~~~~~
251
249
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
254
252
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setAllowedTypes `::
255
253
256
254
// ...
@@ -259,27 +257,26 @@ be anything, but it must be a string. You can configure these types by calling
259
257
// ...
260
258
261
259
$resolver->setAllowedTypes(array(
262
- 'firstName ' => 'string ',
260
+ 'port ' => 'integer ',
263
261
));
264
262
}
265
263
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
267
265
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 ``.
270
267
271
268
There is also an
272
269
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedTypes `
273
270
method, which you can use to add an allowed type to the previous allowed types.
274
271
275
272
Normalize the Options
276
- ---------------------
273
+ ~~~~~~~~~~~~~~~~~~~~~
277
274
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
283
280
:method: `Symfony\\ Components\\ OptionsResolver\\ OptionsResolver::setNormalizers `::
284
281
285
282
// ...
@@ -288,13 +285,37 @@ calling
288
285
// ...
289
286
290
287
$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;
293
294
},
294
295
));
295
296
}
296
297
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
+ }
299
320
300
321
.. _Packagist : https://packagist.org/packages/symfony/options-resolver
0 commit comments