@@ -12,88 +12,96 @@ The following example demonstrates a Person class with two required options
12
12
the default value of "gender" is derived from the passed first name, if
13
13
possible, and may only be one of "male" and "female".
14
14
15
- use Symfony\Component\OptionsResolver\OptionsResolver;
16
- use Symfony\Component\OptionsResolver\OptionsResolverInterface;
17
- use Symfony\Component\OptionsResolver\Options;
15
+ ``` php
16
+ use Symfony\Component\OptionsResolver\OptionsResolver;
17
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
18
+ use Symfony\Component\OptionsResolver\Options;
18
19
19
- class Person
20
+ class Person
21
+ {
22
+ protected $options;
23
+
24
+ public function __construct(array $options = array())
20
25
{
21
- protected $options;
22
-
23
- public function __construct(array $options = array())
24
- {
25
- $resolver = new OptionsResolver();
26
- $this->setDefaultOptions($resolver);
27
-
28
- $this->options = $resolver->resolve($options);
29
- }
30
-
31
- protected function setDefaultOptions(OptionsResolverInterface $resolver)
32
- {
33
- $resolver->setRequired(array(
34
- 'firstName',
35
- 'lastName',
36
- ));
37
-
38
- $resolver->setDefaults(array(
39
- 'age' => null,
40
- 'gender' => function (Options $options) {
41
- if (self::isKnownMaleName($options['firstName'])) {
42
- return 'male';
43
- }
44
-
45
- return 'female';
46
- },
47
- ));
48
-
49
- $resolver->setAllowedValues(array(
50
- 'gender' => array('male', 'female'),
51
- ));
52
- }
26
+ $resolver = new OptionsResolver();
27
+ $this->setDefaultOptions($resolver);
28
+
29
+ $this->options = $resolver->resolve($options);
53
30
}
54
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
+
55
57
We can now easily instantiate a Person object:
56
58
57
- // 'gender' is implicitly set to 'female'
58
- $person = new Person(array(
59
- 'firstName' => 'Jane',
60
- 'lastName' => 'Doe',
61
- ));
59
+ ``` php
60
+ // 'gender' is implicitly set to 'female'
61
+ $person = new Person(array(
62
+ 'firstName' => 'Jane',
63
+ 'lastName' => 'Doe',
64
+ ));
65
+ ```
62
66
63
67
We can also override the default values of the optional options:
64
68
65
- $person = new Person(array(
66
- 'firstName' => 'Abdullah',
67
- 'lastName' => 'Mogashi',
68
- 'gender' => 'male',
69
- 'age' => 30,
70
- ));
69
+ ``` php
70
+ $person = new Person(array(
71
+ 'firstName' => 'Abdullah',
72
+ 'lastName' => 'Mogashi',
73
+ 'gender' => 'male',
74
+ 'age' => 30,
75
+ ));
76
+ ```
71
77
72
78
Options can be added or changed in subclasses by overriding the ` setDefaultOptions `
73
79
method:
74
80
75
- use Symfony\Component\OptionsResolver\OptionsResolver;
76
- use Symfony\Component\OptionsResolver\Options;
81
+ ``` php
82
+ use Symfony\Component\OptionsResolver\OptionsResolver;
83
+ use Symfony\Component\OptionsResolver\Options;
77
84
78
- class Employee extends Person
85
+ class Employee extends Person
86
+ {
87
+ protected function setDefaultOptions(OptionsResolverInterface $resolver)
79
88
{
80
- protected function setDefaultOptions(OptionsResolverInterface $resolver)
81
- {
82
- parent::setDefaultOptions($resolver);
83
-
84
- $resolver->setRequired(array(
85
- 'birthDate',
86
- ));
87
-
88
- $resolver->setDefaults(array(
89
- // $previousValue contains the default value configured in the
90
- // parent class
91
- 'age' => function (Options $options, $previousValue) {
92
- return self::calculateAge($options['birthDate']);
93
- }
94
- ));
95
- }
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
+ ));
96
102
}
103
+ }
104
+ ```
97
105
98
106
99
107
0 commit comments