Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.77% |
30 / 31 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
Query | |
96.77% |
30 / 31 |
|
88.89% |
8 / 9 |
20 | |
0.00% |
0 / 1 |
setInputOption | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getInputOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOutputOption | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getOutputOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
dirty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
reset | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isOptionSet | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
notWith | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
prepareOptions | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 |
1 | <?php |
2 | |
3 | /** |
4 | * Karla ImageMagick wrapper library |
5 | * |
6 | * PHP Version 8.0< |
7 | * |
8 | * @category Utility |
9 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
10 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
11 | * @link https://github.com/localgod/karla Karla |
12 | * @since 2013-05-26 |
13 | */ |
14 | |
15 | declare(strict_types=1); |
16 | |
17 | namespace Karla; |
18 | |
19 | /** |
20 | * Class for maintaining query info |
21 | * |
22 | * @category Utility |
23 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
24 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
25 | * @link https://github.com/localgod/karla Karla |
26 | */ |
27 | class Query |
28 | { |
29 | /** |
30 | * This argument is to be considered for the input image |
31 | * |
32 | * @var interger |
33 | */ |
34 | public const ARGUMENT_TYPE_INPUT = 0; |
35 | |
36 | /** |
37 | * This argument is to be considered for the output image |
38 | * |
39 | * @var integer |
40 | */ |
41 | public const ARGUMENT_TYPE_OUTPUT = 1; |
42 | |
43 | /** |
44 | * Is the object dirty (has any arguments been set) |
45 | * |
46 | * @var boolean |
47 | */ |
48 | private bool $dirty; |
49 | |
50 | /** |
51 | * Input option |
52 | * |
53 | * @var Array |
54 | */ |
55 | protected array $inputOptions; |
56 | |
57 | /** |
58 | * Output option |
59 | * |
60 | * @var Array |
61 | */ |
62 | protected array $outputOptions; |
63 | |
64 | /** |
65 | * Set input option |
66 | * |
67 | * @param string $option |
68 | * Option to set |
69 | * |
70 | * @return void |
71 | */ |
72 | public function setInputOption($option) |
73 | { |
74 | if ($option != "") { |
75 | $this->inputOptions[] = $option; |
76 | $this->dirty(); |
77 | } |
78 | } |
79 | |
80 | /** |
81 | * Get input option |
82 | * |
83 | * @return string[] |
84 | */ |
85 | public function getInputOptions() |
86 | { |
87 | return $this->inputOptions; |
88 | } |
89 | |
90 | /** |
91 | * Set output option |
92 | * |
93 | * @param string $option |
94 | * Option to set |
95 | * |
96 | * @return void |
97 | */ |
98 | public function setOutputOption($option) |
99 | { |
100 | if ($option != "") { |
101 | $this->outputOptions[] = $option; |
102 | $this->dirty(); |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * Get output options |
108 | * |
109 | * @return string[] |
110 | */ |
111 | public function getOutputOptions() |
112 | { |
113 | return $this->outputOptions; |
114 | } |
115 | |
116 | /** |
117 | * Set the object as beeing dirty |
118 | * |
119 | * (Arguments has been set) |
120 | * |
121 | * @return void |
122 | */ |
123 | public function dirty() |
124 | { |
125 | $this->dirty = true; |
126 | } |
127 | |
128 | /** |
129 | * Reset the command |
130 | * |
131 | * @return void |
132 | */ |
133 | public function reset() |
134 | { |
135 | $this->inputOptions = array(); |
136 | $this->outputOptions = array(); |
137 | $this->dirty = false; |
138 | } |
139 | |
140 | /** |
141 | * Check if an option is already set |
142 | * |
143 | * @param string $lookop |
144 | * Option to look up |
145 | * @param array $optionList |
146 | * Optionlist to look in |
147 | * |
148 | * @return boolean |
149 | */ |
150 | final public function isOptionSet($lookop, array $optionList) |
151 | { |
152 | foreach ($optionList as $option) { |
153 | if (strstr($option, trim($lookop))) { |
154 | return true; |
155 | } |
156 | } |
157 | |
158 | return false; |
159 | } |
160 | |
161 | /** |
162 | * Raise an error if a method is called in a invalid context |
163 | * |
164 | * @param string $method Method to check |
165 | * @param integer $argumentType Is it an input or an output argument |
166 | * |
167 | * @throws \BadMethodCallException |
168 | */ |
169 | public function notWith(string $method, int $argumentType): void |
170 | { |
171 | if ($argumentType == Query::ARGUMENT_TYPE_INPUT) { |
172 | if ($this->isOptionSet($method, $this->inputOptions)) { |
173 | $message = "'" . $method . "()' can only be called once as in input argument.."; |
174 | throw new \BadMethodCallException($message); |
175 | } |
176 | } elseif ($argumentType == Query::ARGUMENT_TYPE_OUTPUT) { |
177 | if ($this->isOptionSet($method, $this->outputOptions)) { |
178 | $message = "'" . $method . "()' can only be called once as in output argument.."; |
179 | throw new \BadMethodCallException($message); |
180 | } |
181 | } |
182 | } |
183 | |
184 | /** |
185 | * Prepare option collection |
186 | * |
187 | * @param array $options |
188 | * Options |
189 | * |
190 | * @return string |
191 | */ |
192 | final public function prepareOptions(array $options): string |
193 | { |
194 | foreach ($options as $option) { |
195 | if ($option == '') { |
196 | unset($option); |
197 | } |
198 | } |
199 | $options = implode(' ', $options); |
200 | if (trim($options) == '') { |
201 | return ''; |
202 | } |
203 | |
204 | return trim($options); |
205 | } |
206 | } |