2012-05-10 15:37:03 +01:00
|
|
|
OptionsResolver Component
|
2012-05-14 18:34:05 +01:00
|
|
|
=========================
|
2012-04-17 17:14:09 +01:00
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
OptionsResolver helps at configuring objects with option arrays.
|
2012-04-17 17:14:09 +01:00
|
|
|
|
|
|
|
It supports default values on different levels of your class hierarchy,
|
2012-05-10 15:37:03 +01:00
|
|
|
option constraints (required vs. optional, allowed values) and lazy options
|
|
|
|
whose default value depends on the value of another option.
|
2012-04-17 17:14:09 +01:00
|
|
|
|
|
|
|
The following example demonstrates a Person class with two required options
|
|
|
|
"firstName" and "lastName" and two optional options "age" and "gender", where
|
|
|
|
the default value of "gender" is derived from the passed first name, if
|
2012-05-10 15:37:03 +01:00
|
|
|
possible, and may only be one of "male" and "female".
|
2012-04-17 17:14:09 +01:00
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
use Symfony\Component\OptionsResolver\Options;
|
2012-04-17 17:14:09 +01:00
|
|
|
|
|
|
|
class Person
|
|
|
|
{
|
|
|
|
protected $options;
|
|
|
|
|
|
|
|
public function __construct(array $options = array())
|
|
|
|
{
|
2012-05-10 15:37:03 +01:00
|
|
|
$resolver = new OptionsResolver();
|
2012-05-23 18:41:48 +01:00
|
|
|
$this->setDefaultOptions($resolver);
|
2012-04-17 17:14:09 +01:00
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
$this->options = $resolver->resolve($options);
|
2012-04-17 17:14:09 +01:00
|
|
|
}
|
|
|
|
|
2012-05-23 18:41:48 +01:00
|
|
|
protected function setDefaultOptions(OptionsResolver $resolver)
|
2012-04-17 17:14:09 +01:00
|
|
|
{
|
2012-05-10 15:37:03 +01:00
|
|
|
$resolver->setRequired(array(
|
2012-04-17 17:14:09 +01:00
|
|
|
'firstName',
|
|
|
|
'lastName',
|
|
|
|
));
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
$resolver->setDefaults(array(
|
2012-04-17 17:14:09 +01:00
|
|
|
'age' => null,
|
|
|
|
'gender' => function (Options $options) {
|
|
|
|
if (self::isKnownMaleName($options['firstName'])) {
|
|
|
|
return 'male';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'female';
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
$resolver->setAllowedValues(array(
|
2012-04-17 17:14:09 +01:00
|
|
|
'gender' => array('male', 'female'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
We can now easily instantiate a Person object:
|
|
|
|
|
2012-05-10 18:26:05 +01:00
|
|
|
// 'gender' is implicitly set to 'female'
|
2012-04-17 17:14:09 +01:00
|
|
|
$person = new Person(array(
|
|
|
|
'firstName' => 'Jane',
|
|
|
|
'lastName' => 'Doe',
|
|
|
|
));
|
|
|
|
|
|
|
|
We can also override the default values of the optional options:
|
|
|
|
|
|
|
|
$person = new Person(array(
|
|
|
|
'firstName' => 'Abdullah',
|
|
|
|
'lastName' => 'Mogashi',
|
|
|
|
'gender' => 'male',
|
|
|
|
'age' => 30,
|
|
|
|
));
|
|
|
|
|
2012-05-23 18:41:48 +01:00
|
|
|
Options can be added or changed in subclasses by overriding the `setDefaultOptions`
|
2012-04-17 17:14:09 +01:00
|
|
|
method:
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
use Symfony\Component\OptionsResolver\Options;
|
2012-04-17 17:14:09 +01:00
|
|
|
|
|
|
|
class Employee extends Person
|
|
|
|
{
|
2012-05-23 18:41:48 +01:00
|
|
|
protected function setDefaultOptions(OptionsResolver $resolver)
|
2012-04-17 17:14:09 +01:00
|
|
|
{
|
2012-05-23 18:41:48 +01:00
|
|
|
parent::setDefaultOptions($resolver);
|
2012-04-17 17:14:09 +01:00
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
$resolver->setRequired(array(
|
2012-04-17 17:14:09 +01:00
|
|
|
'birthDate',
|
|
|
|
));
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
$resolver->setDefaults(array(
|
2012-05-23 18:41:48 +01:00
|
|
|
// $previousValue contains the default value setDefaultOptionsd in the
|
2012-04-17 17:14:09 +01:00
|
|
|
// parent class
|
|
|
|
'age' => function (Options $options, $previousValue) {
|
2012-05-10 15:37:03 +01:00
|
|
|
return self::calculateAge($options['birthDate']);
|
2012-04-17 17:14:09 +01:00
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
|
|
|
|
|
2012-04-17 17:14:09 +01:00
|
|
|
Resources
|
|
|
|
---------
|
|
|
|
|
|
|
|
You can run the unit tests with the following command:
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
phpunit
|