@@ -50,17 +50,17 @@ The advantages of doing this will become more obvious as you continue::
50
50
$this->options = $resolver->resolve($options);
51
51
}
52
52
53
- The options property now is a well defined array with all resolved options readily available::
53
+ The options property now is a well defined array with all resolved options
54
+ readily available::
54
55
55
56
// ...
56
- public function getHost( )
57
+ public function sendMail($from, $to )
57
58
{
58
- return $this->options['host'];
59
- }
60
-
61
- public function getPassword()
62
- {
63
- return $this->options['password'];
59
+ $mail = ...;
60
+ $mail->setHost($this->options['host']);
61
+ $mail->setUsername($this->options['username']);
62
+ $mail->setPassword($this->options['password']);
63
+ // ...
64
64
}
65
65
66
66
Configuring the OptionsResolver
@@ -70,6 +70,7 @@ Now, try to actually use the class::
70
70
71
71
$mailer = new Mailer(array(
72
72
'host' => 'smtp.example.org',
73
+ 'username' => 'user',
73
74
'password' => 'pa$$word',
74
75
));
75
76
@@ -86,7 +87,7 @@ knows which options should be resolved.
86
87
function.
87
88
88
89
A best practice is to put the configuration in a method (e.g.
89
- ``setDefaultOptions ``). You call this method in the constructor to configure
90
+ ``configureOptions ``). You call this method in the constructor to configure
90
91
the ``OptionsResolver `` class::
91
92
92
93
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -99,17 +100,38 @@ the ``OptionsResolver`` class::
99
100
public function __construct(array $options = array())
100
101
{
101
102
$resolver = new OptionsResolver();
102
- $this->setDefaultOptions ($resolver);
103
+ $this->configureOptions ($resolver);
103
104
104
105
$this->options = $resolver->resolve($options);
105
106
}
106
107
107
- protected function setDefaultOptions (OptionsResolverInterface $resolver)
108
+ protected function configureOptions (OptionsResolverInterface $resolver)
108
109
{
109
110
// ... configure the resolver, you will learn this in the sections below
110
111
}
111
112
}
112
113
114
+ Set Default Values
115
+ ~~~~~~~~~~~~~~~~~~
116
+
117
+ Most of the options have a default value. You can configure these options by
118
+ calling :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setDefaults `::
119
+
120
+ // ...
121
+ protected function setDefaultOptions(OptionsResolverInterface $resolver)
122
+ {
123
+ // ...
124
+
125
+ $resolver->setDefaults(array(
126
+ 'username' => 'root',
127
+ ));
128
+ }
129
+
130
+ This would add an option - ``username `` - and give it a default value of
131
+ ``root ``. If the user passes in a ``username `` option, that value will
132
+ override this default. You don't need to configure ``username `` as an optional
133
+ option.
134
+
113
135
Required Options
114
136
~~~~~~~~~~~~~~~~
115
137
@@ -135,15 +157,18 @@ If you don't pass a required option, a
135
157
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ MissingOptionsException `
136
158
will be thrown.
137
159
138
- To determine if an option is required, you can use the
139
- :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isRequired `
140
- method.
160
+ .. tip ::
161
+
162
+ To determine if an option is required, you can use the
163
+ :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::isRequired `
164
+ method.
141
165
142
166
Optional Options
143
167
~~~~~~~~~~~~~~~~
144
168
145
169
Sometimes, an option can be optional (e.g. the ``password `` option in the
146
- ``Mailer `` class). You can configure these options by calling
170
+ ``Mailer `` class), but it doesn't have a default value. You can configure
171
+ these options by calling
147
172
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setOptional `::
148
173
149
174
// ...
@@ -154,34 +179,23 @@ Sometimes, an option can be optional (e.g. the ``password`` option in the
154
179
$resolver->setOptional(array('password'));
155
180
}
156
181
157
- Set Default Values
158
- ~~~~~~~~~~~~~~~~~~
159
-
160
- Most of the optional options have a default value. You can configure these
161
- options by calling
162
- :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setDefaults `::
182
+ Options with defaults are already marked as optional.
163
183
164
- // ...
165
- protected function setDefaultOptions(OptionsResolverInterface $resolver)
166
- {
167
- // ...
184
+ .. tip ::
168
185
169
- $resolver->setDefaults(array(
170
- 'username' => 'root',
171
- ));
172
- }
186
+ When setting an option as optional, you can't be sure if it's in the array
187
+ or not. You have to check if the option exists before using it.
173
188
174
- This would add a third option - ``username `` - and give it a default value
175
- of ``root ``. If the user passes in a ``username `` option, that value will
176
- override this default. You don't need to configure ``username `` as an optional
177
- option. The ``OptionsResolver `` already knows that options with a default
178
- value are optional.
189
+ To avoid checking if it exists everytime, you can also set a default of
190
+ ``null `` to an option using the ``setDefaults() `` method (see `Set Default Values `_),
191
+ this means the element always exists in the array, but with a default of
192
+ ``null ``.
179
193
180
- Default Values that depend on another Option
194
+ Default Values that Depend on another Option
181
195
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
182
196
183
197
Suppose you add a ``port `` option to the ``Mailer `` class, whose default
184
- value you guess based on the host . You can do that easily by using a
198
+ value you guess based on the encryption . You can do that easily by using a
185
199
closure as the default value::
186
200
187
201
use Symfony\Component\OptionsResolver\Options;
@@ -193,16 +207,21 @@ closure as the default value::
193
207
// ...
194
208
195
209
$resolver->setDefaults(array(
210
+ 'encryption' => null,
196
211
'port' => function (Options $options) {
197
- if (in_array($options['host'], array('127.0.0.1', 'localhost')) ) {
198
- return 80 ;
212
+ if ('ssl' === $options['encryption'] ) {
213
+ return 465 ;
199
214
}
200
215
201
216
return 25;
202
217
},
203
218
));
204
219
}
205
220
221
+ The :class: `Symfony\\ Component\\ OptionsResolver\\ Options ` class implements
222
+ :phpclass: `ArrayAccess `, :phpclass: `Iterator ` and :phpclass: `Countable `. That
223
+ means you can handle it just like a normal array containing the options.
224
+
206
225
.. caution ::
207
226
208
227
The first argument of the closure must be typehinted as ``Options ``,
@@ -296,16 +315,16 @@ a ``transport`` option, it can only be one of ``sendmail``, ``mail`` or
296
315
// ...
297
316
298
317
$resolver->setAllowedValues(array(
299
- 'transport ' => array('sendmail' , 'mail ', 'smtp '),
318
+ 'encryption ' => array(null , 'ssl ', 'tls '),
300
319
));
301
320
}
302
321
303
322
There is also an
304
323
:method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::addAllowedValues `
305
324
method, which you can use if you want to add an allowed value to the previously
306
- set allowed values.
325
+ configured allowed values.
307
326
308
- Configure allowed Types
327
+ Configure Allowed Types
309
328
~~~~~~~~~~~~~~~~~~~~~~~
310
329
311
330
You can also specify allowed types. For instance, the ``port `` option can
0 commit comments