Skip to content

Commit a34ec2b

Browse files
javiereguiluzfabpot
authored andcommitted
Updated all the README files
1 parent e262145 commit a34ec2b

File tree

1 file changed

+8
-108
lines changed

1 file changed

+8
-108
lines changed

README.md

Lines changed: 8 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,15 @@
11
OptionsResolver Component
22
=========================
33

4-
OptionsResolver helps at configuring objects with option arrays.
5-
6-
It supports default values on different levels of your class hierarchy,
7-
option constraints (required vs. optional, allowed values) and lazy options
8-
whose default value depends on the value of another option.
9-
10-
The following example demonstrates a Person class with two required options
11-
"firstName" and "lastName" and two optional options "age" and "gender", where
12-
the default value of "gender" is derived from the passed first name, if
13-
possible, and may only be one of "male" and "female".
14-
15-
```php
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
17-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
18-
use Symfony\Component\OptionsResolver\Options;
19-
20-
class Person
21-
{
22-
protected $options;
23-
24-
public function __construct(array $options = array())
25-
{
26-
$resolver = new OptionsResolver();
27-
$this->setDefaultOptions($resolver);
28-
29-
$this->options = $resolver->resolve($options);
30-
}
31-
32-
protected function setDefaultOptions(OptionsResolverInterface $resolver)
33-
{
34-
$resolver->setRequired(array(
35-
'firstName',
36-
'lastName',
37-
));
38-
39-
$resolver->setDefaults(array(
40-
'age' => null,
41-
'gender' => function (Options $options) {
42-
if (self::isKnownMaleName($options['firstName'])) {
43-
return 'male';
44-
}
45-
46-
return 'female';
47-
},
48-
));
49-
50-
$resolver->setAllowedValues(array(
51-
'gender' => array('male', 'female'),
52-
));
53-
}
54-
}
55-
```
56-
57-
We can now easily instantiate a Person object:
58-
59-
```php
60-
// 'gender' is implicitly set to 'female'
61-
$person = new Person(array(
62-
'firstName' => 'Jane',
63-
'lastName' => 'Doe',
64-
));
65-
```
66-
67-
We can also override the default values of the optional options:
68-
69-
```php
70-
$person = new Person(array(
71-
'firstName' => 'Abdullah',
72-
'lastName' => 'Mogashi',
73-
'gender' => 'male',
74-
'age' => 30,
75-
));
76-
```
77-
78-
Options can be added or changed in subclasses by overriding the `setDefaultOptions`
79-
method:
80-
81-
```php
82-
use Symfony\Component\OptionsResolver\OptionsResolver;
83-
use Symfony\Component\OptionsResolver\Options;
84-
85-
class Employee extends Person
86-
{
87-
protected function setDefaultOptions(OptionsResolverInterface $resolver)
88-
{
89-
parent::setDefaultOptions($resolver);
90-
91-
$resolver->setRequired(array(
92-
'birthDate',
93-
));
94-
95-
$resolver->setDefaults(array(
96-
// $previousValue contains the default value configured in the
97-
// parent class
98-
'age' => function (Options $options, $previousValue) {
99-
return self::calculateAge($options['birthDate']);
100-
}
101-
));
102-
}
103-
}
104-
```
105-
106-
4+
The OptionsResolver component is `array_replace on steroids. It allows you to
5+
create an options system with required options, defaults, validation (type,
6+
value), normalization and more.
1077

1088
Resources
1099
---------
11010

111-
You can run the unit tests with the following command:
112-
113-
$ cd path/to/Symfony/Component/OptionsResolver/
114-
$ composer install
115-
$ phpunit
11+
* [Documentation](https://symfony.com/doc/current/components/options_resolver.html)
12+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
13+
* [Report issues](https://github.com/symfony/symfony/issues) and
14+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
15+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)