Completed
Pull Request — master (#20)
by Alexander
01:39
created

Request::withServerParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Class Request
7
 *
8
 * @TODO : $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
9
 * @package alkemann\h2l
10
 */
11
class Request extends Message
12
{
13
    const GET = 'GET';
14
    const HEAD = 'HEAD';
15
    const POST = 'POST';
16
    const PUT = 'PUT';
17
    const DELETE = 'DELETE';
18
    const CONNECT = 'CONNECT';
19
    const OPTIONS = 'OPTIONS';
20
    const TRACE = 'TRACE';
21
    const PATCH = 'PATCH';
22
23
    protected $parameters = [];
24
    protected $request = [];
25
    protected $server = [];
26
    protected $get = [];
27
    protected $post = [];
28
    protected $route = null;
29
30
    protected $accept_type = Message::CONTENT_HTML;
31
32
    /**
33
     * Get request parameters from url as url params, get queries or post, in that order
34
     *
35
     * @param string $name the name of the parameter
36
     * @return mixed|null the value or null if not set
37
     */
38
    public function param(string $name)
39
    {
40
        if (isset($this->parameters[$name])) {
41
            return $this->parameters[$name];
42
        }
43
        if (isset($this->get[$name])) {
44
            return $this->get[$name];
45
        }
46
        if (isset($this->post[$name])) {
47
            return $this->post[$name];
48
        }
49
        return null;
50
    }
51
52
    /**
53
     * @param array $request
54
     * @return Request
55
     */
56
    public function withRequestParams(array $request): Request
57
    {
58
        $new = clone $this;
59
        $new->url = $request['url'] ?? '/';
60
        unset($request['url']);
61
        $new->request = $request;
62
        return $new;
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    public function getRequestParams(): array
69
    {
70
        return $this->request;
71
    }
72
73
    /**
74
     * @param array $server
75
     * @return Request
76
     */
77
    public function withServerParams(array $server): Request
78
    {
79
        $new = clone $this;
80
        $new->server = $server;
81
        $new->setContentTypeFromServerParams($server['HTTP_CONTENT_TYPE'] ?? '');
82
        $new->setAcceptTypeFromServerParams($server['HTTP_ACCEPT'] ?? '');
83
        $new->method = $server['REQUEST_METHOD'] ?? Request::GET;
84
        $new->headers = Util::getRequestHeadersFromServerArray($server);
85
        return $new;
86
    }
87
88
    private function setContentTypeFromServerParams(string $content_type): void
89
    {
90
        $known_content_types = [
91
            Message::CONTENT_JSON,
92
            Message::CONTENT_XML,
93
            Message::CONTENT_FORM
94
        ];
95
        foreach ($known_content_types as $t) {
96
            if (strpos($content_type, $t) !== false) {
97
                $this->content_type = $t;
98
                return;
99
            }
100
        }
101
    }
102
103
    private function setAcceptTypeFromServerParams(string $accept_type): void
104
    {
105
        $known_accept_types = [
106
            Message::CONTENT_JSON,
107
            Message::CONTENT_XML,
108
        ];
109
        foreach ($known_accept_types as $t) {
110
            if (strpos($accept_type, $t) !== false) {
111
                $this->accept_type = $t;
112
                return;
113
            }
114
        }
115
    }
116
117
    public function acceptType(): string
118
    {
119
        return $this->accept_type;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function getServerParam(): array
126
    {
127
        return $this->server;
128
    }
129
130
    /**
131
     * @param array $post
132
     * @return Request
133
     */
134
    public function withPostData(array $post): Request
135
    {
136
        $new = clone $this;
137
        $new->post = $post;
138
        return $new;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getPostData(): array
145
    {
146
        return $this->post;
147
    }
148
149
    /**
150
     * @param array $get
151
     * @return Request
152
     */
153
    public function withGetData(array $get): Request
154
    {
155
        $new = clone $this;
156
        $new->get = $get;
157
        return $new;
158
    }
159
160
    /**
161
     * @return array
162
     */
163
    public function getGetData(): array
164
    {
165
        return $this->get;
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public function query(): array
172
    {
173
        return $this->get;
174
    }
175
176
    /**
177
     * @param array $parameters
178
     * @return Request
179
     */
180
    public function withUrlParams(array $parameters): Request
181
    {
182
        $new = clone $this;
183
        $new->parameters = $parameters;
184
        return $new;
185
    }
186
187
    /**
188
     * @return array
189
     */
190
    public function getUrlParams(): array
191
    {
192
        return $this->parameters;
193
    }
194
195
    /**
196
     * @param interfaces\Route $route
197
     * @return Request
198
     */
199
    public function withRoute(interfaces\Route $route): Request
200
    {
201
        $new = clone $this;
202
        $new->route = $route;
203
        $new->parameters = $route->parameters();
204
        return $new;
205
    }
206
207
    /**
208
     * @return interfaces\Route|null
209
     */
210
    public function route(): ?interfaces\Route
211
    {
212
        return $this->route;
213
    }
214
}
215