1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\Token; |
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
7
|
|
|
|
8
|
|
|
class Configuration |
9
|
|
|
{ |
10
|
|
|
const CLEAN_STRATEGY_NONE = 'none'; |
11
|
|
|
const CLEAN_STRATEGY_STANDARD = 'standard'; |
12
|
|
|
const CLEAN_STRATEGY_AGGRESSIVE = 'aggressive'; |
13
|
|
|
|
14
|
|
|
const ERROR_STRATEGY_NONE = 'none'; |
15
|
|
|
const ERROR_STRATEGY_THROW = 'throw'; |
16
|
|
|
const ERROR_STRATEGY_FIX = 'fix'; |
17
|
|
|
|
18
|
|
|
const OUTPUT_COMPACT = 'compact'; |
19
|
|
|
const OUTPUT_PRETTY = 'pretty'; |
20
|
|
|
|
21
|
|
|
const REMOVE_TYPES_NONE = 'none'; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
private $options; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
*/ |
29
|
53 |
|
public function __construct(array $options = array()) |
30
|
|
|
{ |
31
|
53 |
|
$resolver = new OptionsResolver(); |
32
|
53 |
|
$this->configureOptions($resolver); |
33
|
|
|
|
34
|
53 |
|
$this->options = $resolver->resolve($options); |
35
|
43 |
|
$this->setDependentOptions(); |
36
|
43 |
|
} |
37
|
|
|
|
38
|
27 |
|
public function has($key) |
39
|
|
|
{ |
40
|
27 |
|
return array_key_exists($key, $this->options); |
41
|
|
|
} |
42
|
|
|
|
43
|
27 |
|
public function get($key) |
44
|
|
|
{ |
45
|
27 |
|
if (!$this->has($key)) { |
46
|
1 |
|
throw new \InvalidArgumentException('Invalid configuration key: ' . $key); |
47
|
|
|
} |
48
|
|
|
|
49
|
27 |
|
return $this->options[$key]; |
50
|
|
|
} |
51
|
|
|
|
52
|
30 |
|
public function isAllowedType($type) |
53
|
|
|
{ |
54
|
30 |
|
$disallowedTypeArray = explode(',', $this->options['remove-types']); |
55
|
30 |
|
foreach ($disallowedTypeArray as $disallowedType) { |
56
|
30 |
|
if (strtolower(trim($disallowedType)) == $type) { |
57
|
5 |
|
return false; |
58
|
|
|
} |
59
|
26 |
|
} |
60
|
|
|
|
61
|
26 |
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
53 |
|
protected function configureOptions(OptionsResolver $resolver) |
65
|
|
|
{ |
66
|
|
|
// Set default options. |
67
|
53 |
|
$resolver->setDefaults(array( |
68
|
53 |
|
'clean-strategy' => self::CLEAN_STRATEGY_STANDARD, |
69
|
53 |
|
'error-strategy' => self::ERROR_STRATEGY_FIX, |
70
|
53 |
|
'indent-spaces' => 4, |
71
|
53 |
|
'output' => self::OUTPUT_COMPACT, |
72
|
53 |
|
'remove-types' => Token::CDATA . ',' . Token::COMMENT |
73
|
53 |
|
)); |
74
|
|
|
|
75
|
|
|
// Validation |
76
|
|
|
|
77
|
|
|
// clean-strategy |
78
|
53 |
|
$resolver->setAllowedTypes('clean-strategy', 'string'); |
79
|
53 |
|
$resolver->setAllowedValues( |
80
|
53 |
|
'clean-strategy', |
81
|
|
|
array( |
82
|
53 |
|
self::CLEAN_STRATEGY_NONE, |
83
|
53 |
|
self::CLEAN_STRATEGY_STANDARD, |
84
|
|
|
self::CLEAN_STRATEGY_AGGRESSIVE |
85
|
53 |
|
) |
86
|
53 |
|
); |
87
|
|
|
|
88
|
|
|
// error-strategy |
89
|
53 |
|
$resolver->setAllowedTypes('error-strategy', 'string'); |
90
|
53 |
|
$resolver->setAllowedValues( |
91
|
53 |
|
'error-strategy', |
92
|
|
|
array( |
93
|
53 |
|
self::ERROR_STRATEGY_NONE, |
94
|
53 |
|
self::ERROR_STRATEGY_THROW, |
95
|
|
|
self::ERROR_STRATEGY_FIX |
96
|
53 |
|
) |
97
|
53 |
|
); |
98
|
|
|
|
99
|
|
|
// indent-spaces |
100
|
53 |
|
$resolver->setAllowedTypes('indent-spaces', 'int'); |
101
|
|
|
$resolver->setAllowedValues('indent-spaces', function ($value) { |
102
|
48 |
|
return $value >= 0; |
103
|
|
|
} |
104
|
53 |
|
); |
105
|
|
|
|
106
|
|
|
// output |
107
|
53 |
|
$resolver->setAllowedTypes('output', 'string'); |
108
|
53 |
|
$resolver->setAllowedValues( |
109
|
53 |
|
'output', |
110
|
53 |
|
array(self::OUTPUT_COMPACT, self::OUTPUT_PRETTY) |
111
|
53 |
|
); |
112
|
|
|
|
113
|
|
|
// remove-types |
114
|
53 |
|
$resolver->setAllowedTypes('remove-types', 'string'); |
115
|
53 |
|
$resolver->setAllowedValues( |
116
|
53 |
|
'remove-types', |
117
|
44 |
|
function ($value) { |
118
|
44 |
|
if ($value == self::REMOVE_TYPES_NONE) { |
119
|
19 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$acceptedValues = array( |
123
|
25 |
|
Token::CDATA, |
124
|
25 |
|
Token::COMMENT, |
125
|
25 |
|
Token::DOCTYPE, |
126
|
25 |
|
Token::ELEMENT, |
127
|
|
|
Token::TEXT |
128
|
25 |
|
); |
129
|
25 |
|
$valueArray = explode(',', $value); |
130
|
25 |
|
foreach ($valueArray as $val) { |
131
|
25 |
|
if (array_search(trim(strtolower($val)), $acceptedValues) === false) { |
132
|
1 |
|
return false; |
133
|
|
|
} |
134
|
24 |
|
} |
135
|
|
|
|
136
|
24 |
|
return true; |
137
|
|
|
} |
138
|
53 |
|
); |
139
|
53 |
|
} |
140
|
|
|
|
141
|
43 |
|
protected function setDependentOptions() |
142
|
|
|
{ |
143
|
43 |
|
if ($this->options['output'] == self::OUTPUT_COMPACT) { |
144
|
40 |
|
$this->options['indent-spaces'] = 0; |
145
|
40 |
|
} |
146
|
43 |
|
} |
147
|
|
|
} |
148
|
|
|
|