Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.58% |
43 / 48 |
|
90.00% |
18 / 20 |
CRAP | |
0.00% |
0 / 1 |
Argument | |
89.58% |
43 / 48 |
|
90.00% |
18 / 20 |
31.02 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
set | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
setQuery | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
isValid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRootPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRootPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPathOffset | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPath | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getPathOffset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRoute | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
getQuery | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getPathElement | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getPathKey | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getPathElements | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getQueryArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBase | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAuthority | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUserInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Uri |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Uri; |
16 | |
17 | /** |
18 | * Cli argument class. |
19 | * |
20 | * Considers arguments used in ca CLI as uri |
21 | * |
22 | * @package phpOMS\Uri |
23 | * @license OMS License 2.0 |
24 | * @link https://jingga.app |
25 | * @since 1.0.0 |
26 | * |
27 | * @SuppressWarnings(PHPMD.Superglobals) |
28 | */ |
29 | final class Argument implements UriInterface |
30 | { |
31 | /** |
32 | * Root path. |
33 | * |
34 | * @var string |
35 | * @since 1.0.0 |
36 | */ |
37 | private string $rootPath = '/'; |
38 | |
39 | /** |
40 | * Path offset. |
41 | * |
42 | * @var int |
43 | * @since 1.0.0 |
44 | */ |
45 | private int $pathOffset = 0; |
46 | |
47 | /** |
48 | * Path elements. |
49 | * |
50 | * @var string[] |
51 | * @since 1.0.0 |
52 | */ |
53 | private array $pathElements = []; |
54 | |
55 | /** |
56 | * Uri. |
57 | * |
58 | * @var string |
59 | * @since 1.0.0 |
60 | */ |
61 | private string $uri = ''; |
62 | |
63 | /** |
64 | * Uri scheme. |
65 | * |
66 | * @var string |
67 | * @since 1.0.0 |
68 | */ |
69 | public string $scheme = ''; |
70 | |
71 | /** |
72 | * Uri host. |
73 | * |
74 | * @var string |
75 | * @since 1.0.0 |
76 | */ |
77 | public string $host = ''; |
78 | |
79 | /** |
80 | * Uri port. |
81 | * |
82 | * @var int |
83 | * @since 1.0.0 |
84 | */ |
85 | public int $port = 0; |
86 | |
87 | /** |
88 | * Uri user. |
89 | * |
90 | * @var string |
91 | * @since 1.0.0 |
92 | */ |
93 | public string $user = ''; |
94 | |
95 | /** |
96 | * Uri password. |
97 | * |
98 | * @var string |
99 | * @since 1.0.0 |
100 | */ |
101 | public string $pass = ''; |
102 | |
103 | /** |
104 | * Uri path. |
105 | * |
106 | * @var string |
107 | * @since 1.0.0 |
108 | */ |
109 | private string $path = ''; |
110 | |
111 | /** |
112 | * Uri path with offset. |
113 | * |
114 | * @var string |
115 | * @since 1.0.0 |
116 | */ |
117 | private string $offsetPath = ''; |
118 | |
119 | /** |
120 | * Uri query. |
121 | * |
122 | * @var array<int, string> |
123 | * @since 1.0.0 |
124 | */ |
125 | private array $query = []; |
126 | |
127 | /** |
128 | * Uri query. |
129 | * |
130 | * @var string |
131 | * @since 1.0.0 |
132 | */ |
133 | private string $queryString = ''; |
134 | |
135 | /** |
136 | * Uri fragment. |
137 | * |
138 | * @var string |
139 | * @since 1.0.0 |
140 | */ |
141 | public string $fragment = ''; |
142 | |
143 | /** |
144 | * Uri base. |
145 | * |
146 | * @var string |
147 | * @since 1.0.0 |
148 | */ |
149 | private string $base = ''; |
150 | |
151 | /** |
152 | * Constructor. |
153 | * |
154 | * @param string $uri Uri |
155 | * |
156 | * @since 1.0.0 |
157 | */ |
158 | public function __construct(string $uri = '') |
159 | { |
160 | $this->set($uri); |
161 | } |
162 | |
163 | /** |
164 | * {@inheritdoc} |
165 | */ |
166 | public function set(string $uri) : void |
167 | { |
168 | $this->uri = $uri; |
169 | |
170 | $uriParts = \explode(' ', $uri); |
171 | |
172 | // Handle no path information only data |
173 | $uriParts = \str_starts_with($uriParts[0], '-') ? ['/', $uriParts[0]] : $uriParts; |
174 | |
175 | $this->path = empty($uriParts) ? '' : \array_shift($uriParts); |
176 | $this->pathElements = \explode('/', \ltrim($this->path, '/')); |
177 | |
178 | $path = \array_slice($this->pathElements, $this->pathOffset); |
179 | $this->offsetPath = '/' . \implode('/', $path); |
180 | |
181 | $this->setQuery(\implode(' ', $uriParts)); |
182 | } |
183 | |
184 | /** |
185 | * {@inheritdoc} |
186 | */ |
187 | public function setQuery(string $uri) : void |
188 | { |
189 | if ($uri === '') { |
190 | $this->query = []; |
191 | $this->queryString = $uri; |
192 | |
193 | return; |
194 | } |
195 | |
196 | $result = \explode(' ', $uri); |
197 | if ($result === false) { |
198 | return; |
199 | } |
200 | |
201 | $this->query = $result; |
202 | $this->queryString = $uri; |
203 | } |
204 | |
205 | /** |
206 | * Set fragment from uri. |
207 | * |
208 | * @param string $uri Uri to parse |
209 | * |
210 | * @return void |
211 | * |
212 | * @since 1.0.0 |
213 | */ |
214 | /* |
215 | private function setInternalFragment(string $uri) : void |
216 | { |
217 | $result = \preg_match('/#([a-zA-Z0-9]*)/', $uri, $matches); |
218 | |
219 | if ($result === 1) { |
220 | $this->fragment = $matches[1] ?? ''; |
221 | } |
222 | } |
223 | */ |
224 | |
225 | /** |
226 | * {@inheritdoc} |
227 | */ |
228 | public static function isValid(string $uri) : bool |
229 | { |
230 | return true; |
231 | } |
232 | |
233 | /** |
234 | * {@inheritdoc} |
235 | */ |
236 | public function getRootPath() : string |
237 | { |
238 | return $this->rootPath; |
239 | } |
240 | |
241 | /** |
242 | * {@inheritdoc} |
243 | */ |
244 | public function setRootPath(string $root) : void |
245 | { |
246 | $this->rootPath = $root; |
247 | } |
248 | |
249 | /** |
250 | * {@inheritdoc} |
251 | */ |
252 | public function setPathOffset(int $offset = 0) : void |
253 | { |
254 | $this->pathOffset = $offset; |
255 | |
256 | $path = \array_slice($this->pathElements, $this->pathOffset); |
257 | $this->offsetPath = '/' . \implode('/', $path); |
258 | } |
259 | |
260 | /** |
261 | * {@inheritdoc} |
262 | */ |
263 | public function getPath() : string |
264 | { |
265 | return $this->path; |
266 | } |
267 | |
268 | /** |
269 | * {@inheritdoc} |
270 | */ |
271 | public function setPath(string $path) : void |
272 | { |
273 | $this->path = $path; |
274 | $this->pathElements = \explode('/', \ltrim($this->path, '/')); |
275 | |
276 | $path = \array_slice($this->pathElements, $this->pathOffset); |
277 | $this->offsetPath = '/' . \implode('/', $path); |
278 | } |
279 | |
280 | /** |
281 | * Get path offset. |
282 | * |
283 | * @return int |
284 | * |
285 | * @since 1.0.0 |
286 | */ |
287 | public function getPathOffset() : int |
288 | { |
289 | return $this->pathOffset; |
290 | } |
291 | |
292 | /** |
293 | * {@inheritdoc} |
294 | */ |
295 | public function getRoute(bool $ignoreOffset = false) : string |
296 | { |
297 | $path = $ignoreOffset ? $this->path : $this->offsetPath; |
298 | |
299 | $query = $this->getQuery(); |
300 | return $path . (empty($query) ? '' : ' ' . $this->getQuery()); |
301 | } |
302 | |
303 | /** |
304 | * {@inheritdoc} |
305 | */ |
306 | public function getQuery(string $key = null) : string |
307 | { |
308 | if ($key !== null) { |
309 | $key = (int) \strtolower($key); |
310 | |
311 | return $this->query[$key] ?? ''; |
312 | } |
313 | |
314 | return $this->queryString; |
315 | } |
316 | |
317 | /** |
318 | * {@inheritdoc} |
319 | */ |
320 | public function getPathElement(int $pos = 0, bool $useOffset = true) : string |
321 | { |
322 | return $this->pathElements[$pos + ($useOffset ? $this->pathOffset : 0)] ?? ''; |
323 | } |
324 | |
325 | /** |
326 | * {@inheritdoc} |
327 | */ |
328 | public function getPathKey(string $key) : string |
329 | { |
330 | foreach ($this->pathElements as $index => $element) { |
331 | if ($element === $key) { |
332 | return $this->pathElements[$index + 1] ?? ''; |
333 | } |
334 | } |
335 | |
336 | return ''; |
337 | } |
338 | |
339 | /** |
340 | * {@inheritdoc} |
341 | */ |
342 | public function getPathElements() : array |
343 | { |
344 | return $this->pathElements; |
345 | } |
346 | |
347 | /** |
348 | * {@inheritdoc} |
349 | */ |
350 | public function getQueryArray() : array |
351 | { |
352 | return $this->query; |
353 | } |
354 | |
355 | /** |
356 | * {@inheritdoc} |
357 | */ |
358 | public function getBase() : string |
359 | { |
360 | return $this->base; |
361 | } |
362 | |
363 | /** |
364 | * {@inheritdoc} |
365 | */ |
366 | public function __toString() |
367 | { |
368 | return $this->uri; |
369 | } |
370 | |
371 | /** |
372 | * {@inheritdoc} |
373 | */ |
374 | public function getAuthority() : string |
375 | { |
376 | return ''; |
377 | } |
378 | |
379 | /** |
380 | * {@inheritdoc} |
381 | */ |
382 | public function getUserInfo() : string |
383 | { |
384 | return ''; |
385 | } |
386 | } |