Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
56.45% |
35 / 62 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
OptionContainer | |
56.45% |
35 / 62 |
|
50.00% |
4 / 8 |
157.26 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isFlag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
extract | |
64.29% |
18 / 28 |
|
0.00% |
0 / 1 |
35.44 | |||
searchOptionIndex | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
cast | |
16.67% |
3 / 18 |
|
0.00% |
0 / 1 |
45.04 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | namespace oat\oatbox\extension\script; |
23 | |
24 | /** |
25 | * Option Container Class |
26 | * |
27 | * This class implements a container for options provided through CLI scripts. |
28 | */ |
29 | class OptionContainer |
30 | { |
31 | /** |
32 | * @var array |
33 | */ |
34 | protected $options; |
35 | |
36 | /** |
37 | * @var array |
38 | */ |
39 | protected $data; |
40 | |
41 | /** |
42 | * Constructor |
43 | * |
44 | * Create a new OptionContainer object. |
45 | * |
46 | * @param array $options |
47 | * @param array $values |
48 | */ |
49 | public function __construct(array $options, array $values) |
50 | { |
51 | $this->data = self::extract($options, $values); |
52 | $this->options = $options; |
53 | } |
54 | |
55 | /** |
56 | * Has Option |
57 | * |
58 | * Wheter an option with name $optionName is extracted. |
59 | * |
60 | * @param string $optionName |
61 | */ |
62 | public function has($optionName) |
63 | { |
64 | return isset($this->data[$optionName]); |
65 | } |
66 | |
67 | /** |
68 | * Get Option |
69 | * |
70 | * Returns the value of option with name $optionName. In case of |
71 | * such a value does not exist, null is returned. |
72 | * |
73 | * @return mixed |
74 | */ |
75 | public function get($optionName) |
76 | { |
77 | return ($this->has($optionName)) ? $this->data[$optionName] : null; |
78 | } |
79 | |
80 | /** |
81 | * Get Options |
82 | * |
83 | * Get all options. |
84 | * |
85 | * @return array |
86 | */ |
87 | public function getOptions() |
88 | { |
89 | return $this->options; |
90 | } |
91 | |
92 | /** |
93 | * Is Flag |
94 | * |
95 | * Wheter an option with name $optionName is a flag. |
96 | */ |
97 | public function isFlag($optionName) |
98 | { |
99 | return isset($this->options[$optionName]) && !empty($this->options[$optionName]['flag']); |
100 | } |
101 | |
102 | private static function extract(array $options, array $values) |
103 | { |
104 | $returnValue = []; |
105 | |
106 | foreach ($options as $optionName => $optionParams) { |
107 | // Ignore non string-indexed options. |
108 | if (is_string($optionName)) { |
109 | $prefix = empty($optionParams['prefix']) ? '' : $optionParams['prefix']; |
110 | $longPrefix = empty($optionParams['longPrefix']) ? '' : $optionParams['longPrefix']; |
111 | |
112 | if (empty($prefix) && empty($longPrefix)) { |
113 | throw new \LogicException("Argument with name '${optionName}' has no prefix, nor long prefix."); |
114 | } |
115 | |
116 | if (!empty($optionParams['flag'])) { |
117 | // It's a flag! |
118 | if (is_int(self::searchOptionIndex($prefix, $longPrefix, $values))) { |
119 | $returnValue[$optionName] = true; |
120 | } |
121 | } else { |
122 | // It's a regular option! |
123 | $required = empty($optionParams['required']) ? false : true; |
124 | $castTo = empty($optionParams['cast']) ? null : $optionParams['cast']; |
125 | $optionIndex = self::searchOptionIndex($prefix, $longPrefix, $values); |
126 | |
127 | if ($required && $optionIndex === false) { |
128 | throw new MissingOptionException("Required argument '${optionName}' is missing.", $optionName); |
129 | } |
130 | |
131 | if ($optionIndex === false && isset($optionParams['defaultValue'])) { |
132 | $returnValue[$optionName] = self::cast($optionParams['defaultValue'], $castTo); |
133 | } else { |
134 | $valueIndex = $optionIndex + 1; |
135 | if ($optionIndex !== false && isset($values[$valueIndex])) { |
136 | $returnValue[$optionName] = self::cast($values[$valueIndex], $castTo); |
137 | } else { |
138 | // Edge case. Option found, but it is the last value of the $value array. |
139 | if ($required) { |
140 | throw new MissingOptionException( |
141 | "No value given for required argument '${optionName}'.", |
142 | $optionName |
143 | ); |
144 | } elseif (isset($optionParams['defaultValue'])) { |
145 | $returnValue[$optionName] = self::cast($optionParams['defaultValue'], $castTo); |
146 | } |
147 | } |
148 | } |
149 | } |
150 | } |
151 | } |
152 | |
153 | return $returnValue; |
154 | } |
155 | |
156 | private static function searchOptionIndex($prefix, $longPrefix, array $values) |
157 | { |
158 | $optionIndex = false; |
159 | $prefixes = [$prefix, $longPrefix]; |
160 | |
161 | for ($i = 0; $i < count($prefixes); $i++) { |
162 | $dashes = str_repeat('-', $i + 1); |
163 | $p = $prefixes[$i]; |
164 | if (!empty($p)) { |
165 | if (($search = array_search("${dashes}${p}", $values)) !== false) { |
166 | $optionIndex = $search; |
167 | break; |
168 | } |
169 | } |
170 | } |
171 | |
172 | return $optionIndex; |
173 | } |
174 | |
175 | private static function cast($value, $to) |
176 | { |
177 | $casted = $value; |
178 | |
179 | if (is_string($to)) { |
180 | switch (strtolower($to)) { |
181 | case 'integer': |
182 | case 'int': |
183 | $casted = @intval($value); |
184 | break; |
185 | |
186 | case 'float': |
187 | $casted = @floatval($value); |
188 | break; |
189 | |
190 | case 'string': |
191 | $casted = @strval($value); |
192 | break; |
193 | |
194 | case 'boolean': |
195 | case 'bool': |
196 | $casted = @boolval($value); |
197 | break; |
198 | } |
199 | } |
200 | |
201 | return $casted; |
202 | } |
203 | } |