Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.28% covered (warning)
51.28%
40 / 78
55.56% covered (warning)
55.56%
10 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
CliRequest
51.28% covered (warning)
51.28%
40 / 78
55.56% covered (warning)
55.56%
10 / 18
256.80
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getData
26.67% covered (danger)
26.67%
4 / 15
0.00% covered (danger)
0.00%
0 / 1
40.94
 getDataString
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getDataInt
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getDataFloat
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getDataBool
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getDataDateTime
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 hasData
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setData
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 init
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createRequestHashs
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
 setMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOS
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setOS
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOrigin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteVerb
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Message\Cli
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Message\Cli;
16
17use phpOMS\Localization\Localization;
18use phpOMS\Message\Http\RequestMethod;
19use phpOMS\Message\RequestAbstract;
20use phpOMS\Router\RouteVerb;
21use phpOMS\Uri\Argument;
22use phpOMS\Uri\UriInterface;
23use phpOMS\Utils\ArrayUtils;
24
25/**
26 * Request class.
27 *
28 * @package phpOMS\Message\Cli
29 * @license OMS License 2.0
30 * @link    https://jingga.app
31 * @since   1.0.0
32 *
33 * @SuppressWarnings(PHPMD.Superglobals)
34 */
35final class CliRequest extends RequestAbstract
36{
37    /**
38     * Uri.
39     *
40     * @var UriInterface
41     * @since 1.0.0
42     */
43    public UriInterface $uri;
44
45    /**
46     * Request method.
47     *
48     * @var string
49     * @since 1.0.0
50     */
51    protected string $method = RequestMethod::GET;
52
53    /**
54     * OS type.
55     *
56     * @var string
57     * @since 1.0.0
58     */
59    private string $os;
60
61    /**
62     * Constructor.
63     *
64     * @param UriInterface $uri  Uri
65     * @param Localization $l11n Localization
66     *
67     * @since 1.0.0
68     */
69    public function __construct(UriInterface $uri = null, Localization $l11n = null)
70    {
71        $this->header       = new CliHeader();
72        $this->header->l11n = $l11n ?? new Localization();
73
74        $this->uri = $uri ?? new Argument();
75        $this->init();
76    }
77
78    /**
79     * Get data.
80     *
81     * @param string $key  Data key
82     * @param string $type Return type
83     *
84     * @return mixed
85     *
86     * @since 1.0.0
87     */
88    public function getData(string $key = null, string $type = null) : mixed
89    {
90        if ($key === null) {
91            return $this->data;
92        }
93
94        $key = \mb_strtolower($key);
95
96        switch ($type) {
97            case null:
98                /* @phpstan-ignore-next-line */
99                return ArrayUtils::getArg($key, $this->data);
100            case 'int':
101                /* @phpstan-ignore-next-line */
102                return (int) ArrayUtils::getArg($key, $this->data);
103            case 'string':
104                /* @phpstan-ignore-next-line */
105                return (string) ArrayUtils::getArg($key, $this->data);
106            case 'float':
107                /* @phpstan-ignore-next-line */
108                return (float) ArrayUtils::getArg($key, $this->data);
109            case 'bool':
110                /* @phpstan-ignore-next-line */
111                return (bool) ArrayUtils::getArg($key, $this->data);
112            case 'DateTime':
113                return new \DateTime((string) ArrayUtils::getArg($key, $this->data));
114            default:
115                /* @phpstan-ignore-next-line */
116                return ArrayUtils::getArg($key, $this->data);
117        }
118    }
119
120    /**
121     * Get data.
122     *
123     * @param string $key Data key
124     *
125     * @return null|string
126     *
127     * @since 1.0.0
128     */
129    public function getDataString(string $key) : ?string
130    {
131        $key = \mb_strtolower($key);
132
133        if (ArrayUtils::hasArg($key, $this->data) === -1) {
134            return null;
135        }
136
137        return (string) ArrayUtils::getArg($key, $this->data);
138    }
139
140    /**
141     * Get data.
142     *
143     * @param string $key Data key
144     *
145     * @return null|int
146     *
147     * @since 1.0.0
148     */
149    public function getDataInt(string $key) : ?int
150    {
151        $key = \mb_strtolower($key);
152
153        if (ArrayUtils::hasArg($key, $this->data) === -1) {
154            return null;
155        }
156
157        return (int) ArrayUtils::getArg($key, $this->data);
158    }
159
160    /**
161     * Get data.
162     *
163     * @param string $key Data key
164     *
165     * @return null|float
166     *
167     * @since 1.0.0
168     */
169    public function getDataFloat(string $key) : ?float
170    {
171        $key = \mb_strtolower($key);
172
173        if (ArrayUtils::hasArg($key, $this->data) === -1) {
174            return null;
175        }
176
177        return (float) ArrayUtils::getArg($key, $this->data);
178    }
179
180    /**
181     * Get data.
182     *
183     * @param string $key Data key
184     *
185     * @return null|bool
186     *
187     * @since 1.0.0
188     */
189    public function getDataBool(string $key) : ?bool
190    {
191        $key = \mb_strtolower($key);
192
193        if (ArrayUtils::hasArg($key, $this->data) === -1) {
194            return null;
195        }
196
197        return (bool) ArrayUtils::getArg($key, $this->data);
198    }
199
200    /**
201     * Get data.
202     *
203     * @param string $key Data key
204     *
205     * @return null|\DateTime
206     *
207     * @since 1.0.0
208     */
209    public function getDataDateTime(string $key) : ?\DateTime
210    {
211        $key = \mb_strtolower($key);
212
213        return empty($this->data[$key] ?? null)
214            ? null
215            : new \DateTime((string) ArrayUtils::getArg($key, $this->data));
216    }
217
218    /**
219     * {@inheritdoc}
220     */
221    public function hasData(string $key) : bool
222    {
223        $key = \mb_strtolower($key);
224
225        /* @phpstan-ignore-next-line */
226        return ArrayUtils::hasArg($key, $this->data) > -1;
227    }
228
229    /**
230     * Set request data.
231     *
232     * @param string $key       Data key
233     * @param mixed  $value     Value
234     * @param bool   $overwrite Overwrite data
235     *
236     * @return bool
237     *
238     * @since 1.0.0
239     */
240    public function setData(string $key, mixed $value, bool $overwrite = false) : bool
241    {
242        $key = \mb_strtolower($key);
243        $pos = -1;
244
245        /* @phpstan-ignore-next-line */
246        if (($pos = ArrayUtils::hasArg($key, $this->data)) === -1 || $overwrite) {
247            if ($pos === -1) {
248                $this->data[] = $key;
249                $this->data[] = $value;
250            } else {
251                $this->data[$pos]     = $key;
252                $this->data[$pos + 1] = $value;
253            }
254
255            $this->uri->setQuery(\implode(' ', $this->data));
256
257            return true;
258        }
259
260        return false;
261    }
262
263    /**
264     * Init request.
265     *
266     * This is used in order to either initialize the current http request or a batch of GET requests
267     *
268     * @return void
269     *
270     * @since 1.0.0
271     */
272    private function init() : void
273    {
274        $this->header->l11n->language = 'en';
275        $this->data                   = $this->uri->getQueryArray();
276    }
277
278    /**
279     * Create request hashs of current request
280     *
281     * The hashes are based on the request path and can be used as unique id.
282     *
283     * @param int $start Start hash from n-th path element
284     *
285     * @return void
286     *
287     * @since 1.0.0
288     */
289    public function createRequestHashs(int $start = 0) : void
290    {
291        $this->hash = [\sha1('')];
292        $pathArray  = $this->uri->getPathElements();
293        $pathLength = \count($pathArray);
294
295        for ($i = $start; $i < $pathLength; ++$i) {
296            if ($pathArray[$i] === '') {
297                continue;
298            }
299
300            $paths = [];
301            for ($j = $start; $j <= $i; ++$j) {
302                $paths[] = $pathArray[$j];
303            }
304
305            $this->hash[] = \sha1(\implode('', $paths));
306        }
307    }
308
309    /**
310     * Set request method.
311     *
312     * @param string $method Request method
313     *
314     * @return void
315     *
316     * @since 1.0.0
317     */
318    public function setMethod(string $method) : void
319    {
320        $this->method = $method;
321    }
322
323    /**
324     * Get request method.
325     *
326     * @return string
327     *
328     * @since 1.0.0
329     */
330    public function getMethod() : string
331    {
332        return $this->method;
333    }
334
335    /**
336     * Determine request OS.
337     *
338     * @return string
339     *
340     * @since 1.0.0
341     */
342    public function getOS() : string
343    {
344        if (!isset($this->os)) {
345            $this->os = \strtolower(\PHP_OS);
346        }
347
348        return $this->os;
349    }
350
351    /**
352     * Set OS type
353     *
354     * @param string $os OS type
355     *
356     * @return void
357     *
358     * @since 1.0.0
359     */
360    public function setOS(string $os) : void
361    {
362        $this->os = $os;
363    }
364
365    /**
366     * {@inheritdoc}
367     */
368    public function getOrigin() : string
369    {
370        return '127.0.0.1';
371    }
372
373    /**
374     * {@inheritdoc}
375     */
376    public function getBody() : string
377    {
378        return '';
379    }
380
381    /**
382     * Get route verb.
383     *
384     * @return int
385     *
386     * @throws \Exception
387     *
388     * @since 1.0.0
389     */
390    public function getRouteVerb() : int
391    {
392        switch ($this->getMethod()) {
393            case RequestMethod::GET:
394                return RouteVerb::GET;
395            case RequestMethod::PUT:
396                return RouteVerb::PUT;
397            case RequestMethod::POST:
398                return RouteVerb::SET;
399            case RequestMethod::DELETE:
400                return RouteVerb::DELETE;
401            default:
402                throw new \Exception();
403        }
404    }
405}