Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
53.85% |
21 / 39 |
|
46.15% |
6 / 13 |
CRAP | |
0.00% |
0 / 1 |
Options | |
53.85% |
21 / 39 |
|
46.15% |
6 / 13 |
92.46 | |
0.00% |
0 / 1 |
addOption | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addOptions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setOption | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
setOrCreateOption | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getOption | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getOptionById | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 | |||
getOptionValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getOptionsMap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOptions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
check | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
setHelpTexts | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
setUI | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getDefinitions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace WebPConvert\Options; |
4 | |
5 | use WebPConvert\Options\Option; |
6 | use WebPConvert\Options\Exceptions\OptionNotFoundException; |
7 | |
8 | /** |
9 | * Handles a collection of options. |
10 | * |
11 | * @package WebPConvert |
12 | * @author Bjørn Rosell <it@rosell.dk> |
13 | * @since Class available since Release 2.0.0 |
14 | */ |
15 | class Options |
16 | { |
17 | |
18 | /** @var array A map of options, keyed by their id */ |
19 | private $options = []; |
20 | |
21 | /** |
22 | * Add option. |
23 | * |
24 | * @param Option $option The option object to add to collection. |
25 | * @return void |
26 | */ |
27 | public function addOption($option) |
28 | { |
29 | $this->options[$option->getId()] = $option; |
30 | } |
31 | |
32 | /** |
33 | * Add options. |
34 | * |
35 | * Conveniently add several options in one call. |
36 | * |
37 | * @return void |
38 | */ |
39 | public function addOptions() |
40 | { |
41 | $options = func_get_args(); |
42 | foreach ($options as $option) { |
43 | $this->addOption($option); |
44 | } |
45 | } |
46 | |
47 | /* |
48 | In some years, we can use the splat instead (requires PHP 5.6): |
49 | @param Option[] ...$options Array of options objects to add |
50 | public function addOptions(...$options) |
51 | { |
52 | foreach ($options as $option) { |
53 | $this->addOption($option); |
54 | } |
55 | }*/ |
56 | |
57 | /** |
58 | * Set the value of an option. |
59 | * |
60 | * @param string $id Id of the option |
61 | * @param mixed $value Value of the option |
62 | * @return void |
63 | */ |
64 | public function setOption($id, $value) |
65 | { |
66 | if (!isset($this->options[$id])) { |
67 | throw new OptionNotFoundException( |
68 | 'Could not set option. There is no option called "' . $id . '" in the collection.' |
69 | ); |
70 | } |
71 | $option = $this->options[$id]; |
72 | $option->setValue($value); |
73 | } |
74 | |
75 | /** |
76 | * Set option, or create a new, if no such option exists. |
77 | * |
78 | * @param string $id Id of option to set/create |
79 | * @param mixed $value Value of option |
80 | * @return void |
81 | */ |
82 | public function setOrCreateOption($id, $value) |
83 | { |
84 | if (!isset($this->options[$id])) { |
85 | $newOption = new GhostOption($id, null); |
86 | $newOption->setValue($value); |
87 | //$newOption = new Option($id, $value); |
88 | $this->addOption($newOption); |
89 | } else { |
90 | $this->setOption($id, $value); |
91 | } |
92 | } |
93 | |
94 | /** |
95 | * Get the value of an option in the collection - by id. |
96 | * |
97 | * @deprecated Use getOptionValue() instead |
98 | * @param string $id Id of the option to get |
99 | * @throws OptionNotFoundException if the option is not in the collection |
100 | * @return mixed The value of the option |
101 | */ |
102 | public function getOption($id) |
103 | { |
104 | return $this->getOptionValue($id); |
105 | } |
106 | |
107 | /** |
108 | * Get the Option in the collection by id. |
109 | * |
110 | * @param string $id Id of the option to get |
111 | * @throws OptionNotFoundException if the option is not in the collection |
112 | * @return mixed The value of the option |
113 | */ |
114 | public function getOptionById($id) |
115 | { |
116 | if (!isset($this->options[$id])) { |
117 | throw new OptionNotFoundException( |
118 | 'There is no option called "' . $id . '" in the collection.' |
119 | ); |
120 | } |
121 | return $this->options[$id]; |
122 | } |
123 | |
124 | /** |
125 | * Get the value of an option in the collection - by id. |
126 | * |
127 | * @param string $id Id of the option to get |
128 | * @throws OptionNotFoundException if the option is not in the collection |
129 | * @return mixed The value of the option |
130 | */ |
131 | public function getOptionValue($id) |
132 | { |
133 | $option = $this->getOptionById($id); |
134 | return $option->getValue(); |
135 | } |
136 | |
137 | /** |
138 | * Return map of Option objects. |
139 | * |
140 | * @return array map of option objects |
141 | */ |
142 | public function getOptionsMap() |
143 | { |
144 | return $this->options; |
145 | } |
146 | |
147 | /** |
148 | * Return flat associative array of options (simple objects). |
149 | * |
150 | * @return array associative array of options |
151 | */ |
152 | public function getOptions() |
153 | { |
154 | $values = []; |
155 | foreach ($this->options as $id => $option) { |
156 | $values[$id] = $option->getValue(); |
157 | } |
158 | return $values; |
159 | } |
160 | |
161 | /** |
162 | * Check all options in the collection. |
163 | */ |
164 | public function check() |
165 | { |
166 | foreach ($this->options as $id => $option) { |
167 | $option->check(); |
168 | } |
169 | } |
170 | |
171 | /** |
172 | * Set help texts on multiple options |
173 | * |
174 | * @param array $helpTexts Hash of helptexts indexed by option id |
175 | */ |
176 | public function setHelpTexts($helpTexts) |
177 | { |
178 | foreach ($this->options as $option) { |
179 | if (array_key_exists($option->getId(), $helpTexts)) { |
180 | $option->setHelpText($helpTexts[$option->getId()]); |
181 | } |
182 | } |
183 | } |
184 | |
185 | /** |
186 | * Set ui definitions on multiple options |
187 | * |
188 | * @param array $uis Hash of ui definitions indexed by option id |
189 | */ |
190 | public function setUI($uis) |
191 | { |
192 | foreach ($this->options as $option) { |
193 | if (array_key_exists($option->getId(), $uis)) { |
194 | $option->setUI($uis[$option->getId()]); |
195 | } |
196 | } |
197 | } |
198 | |
199 | public function getDefinitions($deprecatedToo = false) |
200 | { |
201 | $defs = []; |
202 | foreach ($this->options as $option) { |
203 | if ($deprecatedToo || !($option->isDeprecated())) { |
204 | $defs[] = $option->getDefinition(); |
205 | } |
206 | } |
207 | return $defs; |
208 | } |
209 | } |