1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\Element; |
6
|
|
|
use Groundskeeper\Tokens\Token; |
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
|
10
|
|
|
class Configuration |
11
|
|
|
{ |
12
|
|
|
const CLEAN_STRATEGY_NONE = 'none'; |
13
|
|
|
const CLEAN_STRATEGY_LENIENT = 'lenient'; |
14
|
|
|
const CLEAN_STRATEGY_STANDARD = 'standard'; |
15
|
|
|
const CLEAN_STRATEGY_AGGRESSIVE = 'aggressive'; |
16
|
|
|
|
17
|
|
|
const OUTPUT_COMPACT = 'compact'; |
18
|
|
|
const OUTPUT_PRETTY = 'pretty'; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
private $options; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
*/ |
26
|
215 |
|
public function __construct(array $options = array()) |
27
|
|
|
{ |
28
|
215 |
|
$resolver = new OptionsResolver(); |
29
|
215 |
|
$this->configureOptions($resolver); |
30
|
|
|
|
31
|
215 |
|
$this->options = $resolver->resolve($options); |
32
|
206 |
|
$this->setDependentOptions(); |
33
|
206 |
|
} |
34
|
|
|
|
35
|
174 |
|
public function has(string $key) : bool |
36
|
|
|
{ |
37
|
174 |
|
return array_key_exists($key, $this->options); |
38
|
|
|
} |
39
|
|
|
|
40
|
174 |
|
public function get(string $key) |
41
|
|
|
{ |
42
|
174 |
|
if (!$this->has($key)) { |
43
|
1 |
|
throw new \InvalidArgumentException('Invalid configuration key: ' . $key); |
44
|
|
|
} |
45
|
|
|
|
46
|
174 |
|
return $this->options[$key]; |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
View Code Duplication |
public function isAllowedElement($element) |
|
|
|
|
50
|
|
|
{ |
51
|
6 |
|
if ($element instanceof Element) { |
52
|
1 |
|
$element = $element->getName(); |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
$disallowedElementArray = explode(',', $this->options['element-blacklist']); |
56
|
|
|
|
57
|
6 |
|
return !in_array($element, $disallowedElementArray); |
58
|
|
|
} |
59
|
|
|
|
60
|
11 |
View Code Duplication |
public function isAllowedType($type) |
|
|
|
|
61
|
|
|
{ |
62
|
11 |
|
if ($type instanceof Token) { |
63
|
1 |
|
$type = $type->getType(); |
64
|
|
|
} |
65
|
|
|
|
66
|
11 |
|
$disallowedTypeArray = explode(',', $this->options['type-blacklist']); |
67
|
|
|
|
68
|
11 |
|
return !in_array($type, $disallowedTypeArray); |
69
|
|
|
} |
70
|
|
|
|
71
|
215 |
|
protected function configureOptions(OptionsResolver $resolver) |
72
|
|
|
{ |
73
|
|
|
// Set default options. |
74
|
215 |
|
$resolver->setDefaults(array( |
75
|
215 |
|
'clean-strategy' => self::CLEAN_STRATEGY_STANDARD, |
76
|
215 |
|
'element-blacklist' => '', |
77
|
215 |
|
'indent-spaces' => 4, |
78
|
215 |
|
'output' => self::OUTPUT_COMPACT, |
79
|
215 |
|
'type-blacklist' => Token::CDATA . ',' . Token::COMMENT |
80
|
|
|
)); |
81
|
|
|
|
82
|
|
|
// Validation |
83
|
|
|
|
84
|
|
|
// clean-strategy |
85
|
215 |
|
$resolver->setAllowedTypes('clean-strategy', 'string'); |
86
|
215 |
|
$resolver->setAllowedValues( |
87
|
215 |
|
'clean-strategy', |
88
|
|
|
array( |
89
|
215 |
|
self::CLEAN_STRATEGY_NONE, |
90
|
215 |
|
self::CLEAN_STRATEGY_LENIENT, |
91
|
215 |
|
self::CLEAN_STRATEGY_STANDARD, |
92
|
215 |
|
self::CLEAN_STRATEGY_AGGRESSIVE |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// element-blacklist |
97
|
215 |
|
$resolver->setAllowedTypes('element-blacklist', 'string'); |
98
|
215 |
|
$resolver->setNormalizer( |
99
|
215 |
|
'element-blacklist', |
100
|
215 |
View Code Duplication |
function (Options $options, $value) { |
|
|
|
|
101
|
212 |
|
$valueArray = explode(',', $value); |
102
|
212 |
|
$formattedValueArray = array(); |
103
|
212 |
|
foreach ($valueArray as $data) { |
104
|
212 |
|
$formattedValueArray[] = trim(strtolower($data)); |
105
|
|
|
} |
106
|
|
|
|
107
|
212 |
|
return implode(',', $formattedValueArray); |
108
|
215 |
|
} |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
// indent-spaces |
112
|
215 |
|
$resolver->setAllowedTypes('indent-spaces', 'int'); |
113
|
215 |
|
$resolver->setAllowedValues('indent-spaces', function ($value) { |
114
|
211 |
|
return $value >= 0; |
115
|
215 |
|
} |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
// output |
119
|
215 |
|
$resolver->setAllowedTypes('output', 'string'); |
120
|
215 |
|
$resolver->setAllowedValues( |
121
|
215 |
|
'output', |
122
|
215 |
|
array(self::OUTPUT_COMPACT, self::OUTPUT_PRETTY) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
// type-blacklist |
126
|
215 |
|
$resolver->setAllowedTypes('type-blacklist', 'string'); |
127
|
215 |
|
$resolver->setAllowedValues( |
128
|
215 |
|
'type-blacklist', |
129
|
215 |
|
function ($value) { |
130
|
207 |
|
if ($value == '') { |
131
|
143 |
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$acceptedValues = array( |
135
|
64 |
|
Token::CDATA, |
136
|
64 |
|
Token::COMMENT, |
137
|
64 |
|
Token::DOCTYPE, |
138
|
64 |
|
Token::ELEMENT, |
139
|
64 |
|
Token::PHP, |
140
|
64 |
|
Token::TEXT |
141
|
|
|
); |
142
|
64 |
|
$valueArray = explode(',', $value); |
143
|
64 |
|
foreach ($valueArray as $val) { |
144
|
64 |
|
if (array_search(trim(strtolower($val)), $acceptedValues) === false) { |
145
|
64 |
|
return false; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
63 |
|
return true; |
150
|
215 |
|
} |
151
|
|
|
); |
152
|
215 |
|
$resolver->setNormalizer( |
153
|
215 |
|
'type-blacklist', |
154
|
215 |
View Code Duplication |
function (Options $options, $value) { |
|
|
|
|
155
|
206 |
|
$valueArray = explode(',', $value); |
156
|
206 |
|
$formattedValueArray = array(); |
157
|
206 |
|
foreach ($valueArray as $data) { |
158
|
206 |
|
$formattedValueArray[] = trim(strtolower($data)); |
159
|
|
|
} |
160
|
|
|
|
161
|
206 |
|
return implode(',', $formattedValueArray); |
162
|
215 |
|
} |
163
|
|
|
); |
164
|
215 |
|
} |
165
|
|
|
|
166
|
206 |
|
protected function setDependentOptions() |
167
|
|
|
{ |
168
|
206 |
|
if ($this->options['output'] === self::OUTPUT_COMPACT) { |
169
|
203 |
|
$this->options['indent-spaces'] = 0; |
170
|
|
|
} |
171
|
206 |
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.