Skip to content

Commit 32835af

Browse files
lologhifabpot
authored andcommitted
[Doc] Use Markdown syntax highlighting
1 parent f49b4f0 commit 32835af

File tree

1 file changed

+74
-66
lines changed

1 file changed

+74
-66
lines changed

README.md

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,96 @@ The following example demonstrates a Person class with two required options
1212
the default value of "gender" is derived from the passed first name, if
1313
possible, and may only be one of "male" and "female".
1414

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;
1819

19-
class Person
20+
class Person
21+
{
22+
protected $options;
23+
24+
public function __construct(array $options = array())
2025
{
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);
5330
}
5431

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+
5557
We can now easily instantiate a Person object:
5658

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+
```
6266

6367
We can also override the default values of the optional options:
6468

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+
```
7177

7278
Options can be added or changed in subclasses by overriding the `setDefaultOptions`
7379
method:
7480

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;
7784

78-
class Employee extends Person
85+
class Employee extends Person
86+
{
87+
protected function setDefaultOptions(OptionsResolverInterface $resolver)
7988
{
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+
));
96102
}
103+
}
104+
```
97105

98106

99107

0 commit comments

Comments
 (0)