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
|
|
|
* @deprecated Deprecated in v1.7.x and scheduled for removal in v2.0.x |
19
|
|
|
*/ |
20
|
|
|
class OptionsResolver |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $required = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $defaults = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $allowedValues = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $allowedTypes = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $normalizers = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $option |
49
|
|
|
* @param mixed $value |
50
|
|
|
*/ |
51
|
|
|
public function setDefault($option, $value) |
52
|
|
|
{ |
53
|
|
|
$this->defaults[$option] = $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $options |
58
|
|
|
*/ |
59
|
|
|
public function setRequired(array $options) |
60
|
|
|
{ |
61
|
|
|
$this->required = $options; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $option |
66
|
|
|
* @param mixed[] $values |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setAllowedValues($option, array $values) |
71
|
|
|
{ |
72
|
|
|
$this->allowedValues[$option] = $values; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $option |
79
|
|
|
* @param mixed[] $types |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setAllowedTypes($option, array $types) |
84
|
|
|
{ |
85
|
|
|
$this->allowedTypes[$option] = $types; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $option |
92
|
|
|
* @param \Closure $normalizer |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setNormalizer($option, \Closure $normalizer) |
97
|
|
|
{ |
98
|
|
|
$this->normalizers[$option] = $normalizer; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $options |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function resolve(array $options) |
109
|
|
|
{ |
110
|
|
|
$resolver = new BaseOptionsResolver(); |
111
|
|
|
$resolver->setDefaults($this->defaults); |
112
|
|
|
$resolver->setRequired($this->required); |
113
|
|
|
|
114
|
|
|
if (SymfonyFramework::isKernelGreaterThanOrEqualTo(2, 7)) { |
115
|
|
|
$this->setupResolver($resolver); |
116
|
|
|
} else { |
117
|
|
|
$this->setupResolverLegacy($resolver); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $resolver->resolve($options); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param BaseOptionsResolver $resolver |
125
|
|
|
*/ |
126
|
|
|
private function setupResolver(BaseOptionsResolver $resolver) |
127
|
|
|
{ |
128
|
|
|
foreach ($this->allowedValues as $option => $values) { |
129
|
|
|
$resolver->setAllowedValues($option, $values); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
foreach ($this->allowedTypes as $option => $types) { |
133
|
|
|
$resolver->setAllowedTypes($option, $types); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($this->normalizers as $option => $normalizer) { |
137
|
|
|
$resolver->setNormalizer($option, $normalizer); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param BaseOptionsResolver $resolver |
143
|
|
|
*/ |
144
|
|
|
private function setupResolverLegacy(BaseOptionsResolver $resolver) |
145
|
|
|
{ |
146
|
|
|
$resolver->setAllowedValues($this->allowedValues); |
|
|
|
|
147
|
|
|
$resolver->setAllowedTypes($this->allowedTypes); |
|
|
|
|
148
|
|
|
$resolver->setNormalizers($this->normalizers); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
This check looks for function calls that miss required arguments.