1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Utility\OptionsResolver; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver as BaseOptionsResolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides a compatible interface for Symfony's newest OptionsResolver implementation, while internally supporting |
19
|
|
|
* legacy variants of the interface. This allows for a clean migration plan in our 2.x branch where any classes |
20
|
|
|
* implementing this must simple "use" the real "Symfony\Component\OptionsResolver\OptionsResolver" class without any |
21
|
|
|
* changes to usage within the code (once support for Symfony <2.7 is dropped). |
22
|
|
|
* |
23
|
|
|
* @deprecated Deprecated in v1.7 and scheduled for removal in v2.0.x |
24
|
|
|
*/ |
25
|
|
|
class OptionsResolver |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $defined = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $required = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $defaults = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $allowedValues = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $allowedTypes = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $normalizers = array(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $defined |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setDefined(array $defined) |
63
|
|
|
{ |
64
|
|
|
$this->defined = $defined; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $option |
71
|
|
|
* @param mixed $value |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function setDefault($option, $value) |
76
|
|
|
{ |
77
|
|
|
$this->defaults[$option] = $value; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $options |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function setRequired(array $options) |
88
|
|
|
{ |
89
|
|
|
$this->required = $options; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $option |
96
|
|
|
* @param mixed[] $values |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setAllowedValues($option, array $values) |
101
|
|
|
{ |
102
|
|
|
$this->allowedValues[$option] = $values; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $option |
109
|
|
|
* @param mixed[] $types |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setAllowedTypes($option, array $types) |
114
|
|
|
{ |
115
|
|
|
$this->allowedTypes[$option] = $types; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $option |
122
|
|
|
* @param \Closure $normalizer |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setNormalizer($option, \Closure $normalizer) |
127
|
|
|
{ |
128
|
|
|
$this->normalizers[$option] = $normalizer; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $options |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function resolve(array $options) |
139
|
|
|
{ |
140
|
|
|
$resolver = new BaseOptionsResolver(); |
141
|
|
|
$resolver->setDefaults($this->defaults); |
142
|
|
|
$resolver->setRequired($this->required); |
143
|
|
|
|
144
|
|
|
if (SymfonyFramework::isKernelGreaterThanOrEqualTo(2, 7)) { |
145
|
|
|
$this->setupResolver($resolver); |
146
|
|
|
} else { |
147
|
|
|
$this->setupResolverLegacy($resolver); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $resolver->resolve($options); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param BaseOptionsResolver $resolver |
155
|
|
|
*/ |
156
|
|
|
private function setupResolver(BaseOptionsResolver $resolver) |
157
|
|
|
{ |
158
|
|
|
$resolver->setDefined($this->defined); |
159
|
|
|
|
160
|
|
|
foreach ($this->allowedValues as $option => $values) { |
161
|
|
|
$resolver->setAllowedValues($option, $values); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
foreach ($this->allowedTypes as $option => $types) { |
165
|
|
|
$resolver->setAllowedTypes($option, $types); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
foreach ($this->normalizers as $option => $normalizer) { |
169
|
|
|
$resolver->setNormalizer($option, $normalizer); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param BaseOptionsResolver $resolver |
175
|
|
|
*/ |
176
|
|
|
private function setupResolverLegacy(BaseOptionsResolver $resolver) |
177
|
|
|
{ |
178
|
|
|
$resolver->setOptional($this->defined); |
|
|
|
|
179
|
|
|
$resolver->setAllowedValues($this->allowedValues); |
|
|
|
|
180
|
|
|
$resolver->setAllowedTypes($this->allowedTypes); |
|
|
|
|
181
|
|
|
$resolver->setNormalizers($this->normalizers); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.