Request::onlyEnv()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PlugHttp;
4
5
use PlugHttp\Body\Content;
6
use PlugHttp\Globals\Cookie;
7
use PlugHttp\Globals\Env;
8
use PlugHttp\Globals\File;
9
use PlugHttp\Globals\Get;
10
use PlugHttp\Globals\Server;
11
use PlugHttp\Globals\Session;
12
use PlugHttp\Utils\ArrayUtil;
13
use stdClass;
14
15
/**
16
 * Class Request
17
 * @package PlugHttp
18
 */
19
class Request
20
{
21
    /**
22
     * @var Cookie
23
     */
24
    private Cookie $cookie;
25
26
    /**
27
     * @var Env
28
     */
29
    private Env $env;
30
31
    /**
32
     * @var File
33
     */
34
    private File $file;
35
36
    /**
37
     * @var Get
38
     */
39
    private Get $get;
40
41
    /**
42
     * @var Server
43
     */
44
    private Server $server;
45
46
    /**
47
     * @var Session
48
     */
49
    private Session $session;
50
51
    /**
52
     * @var Content
53
     */
54
    private Content $content;
55
56
    /**
57
     * Request constructor.
58
     */
59
    public function __construct()
60
    {
61
        $this->cookie = new Cookie();
62
        $this->env = new Env();
63
        $this->file = new File();
64
        $this->get = new Get();
65
        $this->server = new Server();
66
        $this->session = new Session();
67
        $this->content = new Content($this->server);
68
    }
69
70
    /**
71
     * @param string $key
72
     * @param $value
73
     */
74
    public function addCookie(string $key, $value, $expire = 0, $path = "", $domain = "", $secure = false, $httponly = false): void
75
    {
76
        $this->cookie->add($key, $value, $expire, $path, $domain, $secure, $httponly);
77
    }
78
79
    /**
80
     * @param string|null $key
81
     * @return array|mixed
82
     */
83
    public function cookies(?string $key = null)
84
    {
85
        if ($key) {
86
            return $this->cookie->get($key);
87
        }
88
89
        return $this->cookie->all();
90
    }
91
92
    /**
93
     * @param $key
94
     * @return bool
95
     */
96
    public function hasCookie($key): bool
97
    {
98
        return $this->cookie->has($key);
99
    }
100
101
    /**
102
     * @param $key
103
     */
104
    public function removeCookie($key): void
105
    {
106
        $this->cookie->remove($key);
107
    }
108
109
    /**
110
     * @param array $values
111
     * @return array
112
     */
113
    public function exceptCookie(array $values): array
114
    {
115
        return $this->cookie->except($values);
116
    }
117
118
    /**
119
     * @param array $values
120
     * @return array
121
     */
122
    public function onlyCookie(array $values): array
123
    {
124
        return $this->cookie->only($values);
125
    }
126
127
    /**
128
     * @param string|null $key
129
     * @return array|mixed
130
     */
131
    public function query(?string $key = null)
132
    {
133
        if ($key) {
134
            return $this->get->get($key);
135
        }
136
137
        return $this->get->all();
138
    }
139
140
    /**
141
     * @param string $key
142
     * @param $value
143
     */
144
    public function addQuery(string $key, $value): void
145
    {
146
        $this->get->add($key, $value);
147
    }
148
149
    /**
150
     * @param array $values
151
     * @return array
152
     */
153
    public function onlyQuery(array $values): array
154
    {
155
        return $this->get->only($values);
156
    }
157
158
    /**
159
     * @param array $values
160
     * @return array
161
     */
162
    public function exceptQuery(array $values): array
163
    {
164
        return $this->get->except($values);
165
    }
166
167
    /**
168
     * @param string $value
169
     * @return bool
170
     */
171
    public function hasQuery(string $value): bool
172
    {
173
        return $this->get->has($value);
174
    }
175
176
    /**
177
     * @param string $key
178
     */
179
    public function removeQuery(string $key): void
180
    {
181
        $this->get->remove($key);
182
    }
183
184
    /**
185
     * @param string|null $key
186
     * @return array|mixed
187
     */
188
    public function env(?string $key = null)
189
    {
190
        if ($key) {
191
            return $this->env->get($key);
192
        }
193
194
        return $this->env->all();
195
    }
196
197
    /**
198
     * @param string $key
199
     * @param $value
200
     */
201
    public function addEnv(string $key, $value): void
202
    {
203
        $this->env->add($key, $value);
204
    }
205
206
    /**
207
     * @param array $values
208
     * @return array
209
     */
210
    public function onlyEnv(array $values): array
211
    {
212
        return $this->env->only($values);
213
    }
214
215
    /**
216
     * @param array $values
217
     * @return array
218
     */
219
    public function exceptEnv(array $values): array
220
    {
221
        return $this->env->except($values);
222
    }
223
224
    /**
225
     * @param string $value
226
     * @return bool
227
     */
228
    public function hasEnv(string $value): bool
229
    {
230
        return $this->env->has($value);
231
    }
232
233
    /**
234
     * @param string $key
235
     */
236
    public function removeEnv(string $key): void
237
    {
238
        $this->env->remove($key);
239
    }
240
241
    /**
242
     * @param string|null $key
243
     * @return array
244
     */
245
    public function file(?string $key = null)
246
    {
247
        if ($key) {
248
            return $this->file->get($key);
249
        }
250
251
        return $this->file->all();
252
    }
253
254
    /**
255
     * @param $key
256
     * @return bool
257
     */
258
    public function hasFile($key): bool
259
    {
260
        return $this->file->has($key);
261
    }
262
263
    /**
264
     * @param $key
265
     */
266
    public function removeFile($key): void
267
    {
268
        $this->file->remove($key);
269
    }
270
271
    /**
272
     * @param array $values
273
     * @return array
274
     */
275
    public function exceptFile(array $values): array
276
    {
277
        return $this->file->except($values);
278
    }
279
280
    /**
281
     * @param array $values
282
     * @return array
283
     */
284
    public function onlyFile(array $values): array
285
    {
286
        return $this->file->only($values);
287
    }
288
289
    /**
290
     * @param string $key
291
     * @param $value
292
     */
293
    public function addHeader(string $key, $value): void
294
    {
295
        $this->server->add($key, $value);
296
    }
297
298
    /**
299
     * @param string $key
300
     * @return array|mixed
301
     */
302
    public function header(string $key)
303
    {
304
        return $this->server->get($key);
305
    }
306
307
    /**
308
     * @param $key
309
     * @return bool
310
     */
311
    public function hasheader($key): bool
312
    {
313
        return $this->server->has($key);
314
    }
315
316
    /**
317
     * @param $key
318
     */
319
    public function removeHeader($key): void
320
    {
321
        $this->server->remove($key);
322
    }
323
324
    /**
325
     * @param array $values
326
     * @return array
327
     */
328
    public function exceptHeaders(array $values): array
329
    {
330
        return $this->server->except($values);
331
    }
332
333
    /**
334
     * @param array $values
335
     * @return array
336
     */
337
    public function onlyHeaders(array $values): array
338
    {
339
        return $this->server->only($values);
340
    }
341
342
    /**
343
     * @return string
344
     */
345
    public function method(): string
346
    {
347
        return $this->server->method();
348
    }
349
350
    /**
351
     * @return string
352
     */
353
    public function getUrl(): string
354
    {
355
        return $this->server->getUrl();
356
    }
357
358
    /**
359
     * @param string $method
360
     * @return bool
361
     */
362
    public function isMethod(string $method): bool
363
    {
364
        return $this->method() === strtoupper($method);
365
    }
366
367
    /**
368
     * @return array
369
     */
370
    public function headers(): array
371
    {
372
        return $this->server->all();
373
    }
374
375
    /**
376
     * @param string $path
377
     * @param int $code
378
     * @return bool
379
     */
380
    public function redirect(string $path, int $code = 301)
381
    {
382
        header("HTTP/1.0 {$code}");
383
        header("Location: {$path}");
384
385
        return true;
386
    }
387
388
    /**
389
     * @param string $key
390
     * @param $value
391
     */
392
    public function addSession(string $key, $value): void
393
    {
394
        $this->session->add($key, $value);
395
    }
396
397
    /**
398
     * @param string|null $key
399
     * @return array|mixed
400
     */
401
    public function session(?string $key = null)
402
    {
403
        if ($key) {
404
            return $this->session->get($key);
405
        }
406
407
        return $this->session->all();
408
    }
409
410
    /**
411
     * @param $key
412
     * @return bool
413
     */
414
    public function hasSession($key): bool
415
    {
416
        return $this->session->has($key);
417
    }
418
419
    /**
420
     * @param $key
421
     */
422
    public function removeSession($key): void
423
    {
424
        $this->session->remove($key);
425
    }
426
427
    /**
428
     * @param array $values
429
     * @return array
430
     */
431
    public function exceptSession(array $values): array
432
    {
433
        return $this->session->except($values);
434
    }
435
436
    /**
437
     * @param array $values
438
     * @return array
439
     */
440
    public function onlySession(array $values): array
441
    {
442
        return $this->session->only($values);
443
    }
444
445
    /**
446
     * @param array $values
447
     * @return array
448
     */
449
    public function except(array $values)
450
    {
451
        return $this->content->except($values);
452
    }
453
454
    /**
455
     * @param array $values
456
     * @return array
457
     */
458
    public function only(array $values)
459
    {
460
        return $this->content->only($values);
461
    }
462
463
    /**
464
     * @param string $key
465
     * @return bool
466
     */
467
    public function has(string $key): bool
468
    {
469
        return $this->content->has($key);
470
    }
471
472
    /**
473
     * @param string $key
474
     */
475
    public function remove(string $key): void
476
    {
477
        $this->content->remove($key);
478
    }
479
480
    /**
481
     * @param $key
482
     * @param $value
483
     */
484
    public function add($key, $value)
485
    {
486
        $this->content->add($key, $value);
487
    }
488
489
    /**
490
     * @return array
491
     */
492
    public function all(): array
493
    {
494
        return $this->content->all();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->content->all() could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
495
    }
496
497
    /**
498
     * Return all body data as stdClass object
499
     *
500
     * @return stdClass
501
     */
502
    public function bodyAsObject(): stdClass
503
    {
504
        $data = $this->content->all();
505
506
        if (is_array($data)) {
507
            return ArrayUtil::converToObject($this->content->all());
0 ignored issues
show
Bug introduced by
It seems like $this->content->all() can also be of type string; however, parameter $data of PlugHttp\Utils\ArrayUtil::converToObject() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

507
            return ArrayUtil::converToObject(/** @scrutinizer ignore-type */ $this->content->all());
Loading history...
508
        }
509
510
        throw new \Exception("It wasn't possible convert to object");
511
    }
512
513
    /**
514
     * @param string $value
515
     * @return mixed
516
     */
517
    public function get(string $value)
518
    {
519
        return $this->content->get($value);
520
    }
521
522
    /**
523
     * @param $name
524
     */
525
    public function __get($name)
526
    {
527
        return $this->content->$name;
528
    }
529
}