ServerRequest::files()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Http;
6
7
use HttpSoft\ServerRequest\ServerNormalizerInterface;
8
use HttpSoft\ServerRequest\ServerRequestCreator;
9
10
/**
11
 * Class ServerRequest
12
 *
13
 * @author Enjoys
14
 */
15
class ServerRequest implements ServerRequestInterface
16
{
17
18
    protected \Psr\Http\Message\ServerRequestInterface $request;
19
    private array $query = [];
20
21
    /**
22
     *
23
     * @param \Psr\Http\Message\ServerRequestInterface|null $request
24
     * @param ServerNormalizerInterface|null $normalizer
25
     */
26
    public function __construct(
27
        ?\Psr\Http\Message\ServerRequestInterface $request = null,
28
        ?ServerNormalizerInterface $normalizer = null
29
    ) {
30
        $this->request = $request ?? ServerRequestCreator::create($normalizer);
31
        $this->query = $this->request->getQueryParams();
32
    }
33
34
    /**
35
     *
36
     * @return \Psr\Http\Message\ServerRequestInterface
37
     */
38
    public function getRequest(): \Psr\Http\Message\ServerRequestInterface
39
    {
40
        return $this->request;
41
    }
42
43
    /**
44
     *
45
     * @param string|null $key
46
     * @param mixed $default
47
     * @return mixed
48
     */
49
    public function get(?string $key = null, $default = null)
50
    {
51
        if ($key === null) {
52
            return $this->query;
53
        }
54
        if (array_key_exists($key, $this->query)) {
55
            return $this->query[$key];
56
        }
57
        return $default;
58
    }
59
60
    /**
61
     *
62
     * @param array $params
63
     * @return void
64
     */
65
    public function addQuery(array $params = []): void
66
    {
67
        foreach ($params as $key => $value) {
68
            $this->query[$key] = $value;
69
        }
70
    }
71
72
    /**
73
     *
74
     * @param string|null $key
75
     * @param mixed $default
76
     * @return mixed
77
     */
78
    public function post(?string $key = null, $default = null)
79
    {
80
        if ($key === null) {
81
            return $this->request->getParsedBody();
82
        }
83
        if (array_key_exists($key, $this->request->getParsedBody())) {
84
            return $this->request->getParsedBody()[$key];
85
        }
86
        return $default;
87
    }
88
89
    /**
90
     *
91
     * @param string|null $key
92
     * @return mixed
93
     */
94
    public function server(?string $key = null)
95
    {
96
        if ($key === null) {
97
            return $this->request->getServerParams();
98
        }
99
        if (array_key_exists($key, $this->request->getServerParams())) {
100
            return $this->request->getServerParams()[$key];
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     *
108
     * @param string|null $key
109
     * @return mixed
110
     */
111
    public function files(?string $key = null)
112
    {
113
        if ($key === null) {
114
            return $this->request->getUploadedFiles();
115
        }
116
        if (array_key_exists($key, $this->request->getUploadedFiles())) {
117
            return $this->request->getUploadedFiles()[$key];
118
        }
119
        return null;
120
    }
121
122
    /**
123
     *
124
     * @return string
125
     */
126
    public function getMethod(): string
127
    {
128
        return $this->request->getMethod();
129
    }
130
}
131