Completed
Push — master ( 3b3e78...78cfc1 )
by Alexander
03:21
created

Request::setAcceptTypeFromServerParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
use alkemann\h2l\util\Http;
6
7
/**
8
 * Class Request
9
 *
10
 * @TODO : $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
11
 * @package alkemann\h2l
12
 */
13
class Request extends Message
14
{
15
    protected $session = null;
16
    protected $parameters = [];
17
    protected $request = [];
18
    protected $server = [];
19
    protected $get = [];
20
    protected $post = [];
21
    protected $route = null;
22
    protected $content_type = '';
23
    protected $accept_type = Http::CONTENT_HTML;
24
25
    /**
26
     * Get request parameters from url as url params, get queries or post, in that order
27
     *
28
     * @param string $name the name of the parameter
29
     * @return mixed|null the value or null if not set
30
     */
31
    public function param(string $name)
32
    {
33
        if (isset($this->parameters[$name])) {
34
            return $this->parameters[$name];
35
        }
36
        if (isset($this->get[$name])) {
37
            return $this->get[$name];
38
        }
39
        if (isset($this->post[$name])) {
40
            return $this->post[$name];
41
        }
42
        return null;
43
    }
44
45
    public function fullUrl(?string $path = null): string
46
    {
47
        $path = $path ?? $this->url();
48
        return
49
            ($this->getServerParam('REQUEST_SCHEME') ?? 'http') . '://' .
50
            $this->getServerParam('HTTP_HOST') . $path;
51
    }
52
53
    /**
54
     * @param array $request
55
     * @return Request
56
     */
57
    public function withRequestParams(array $request): Request
58
    {
59
        $new = clone $this;
60
        $new->url = $request['url'] ?? '/';
61
        unset($request['url']);
62
        $new->request = $request;
63
        return $new;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getRequestParams(): array
70
    {
71
        return $this->request;
72
    }
73
74
    /**
75
     * @param array $server
76
     * @return Request
77
     */
78
    public function withServerParams(array $server): Request
79
    {
80
        $new = clone $this;
81
        $new->server = $server;
82
        $new->setContentTypeFromServerParams($server['HTTP_CONTENT_TYPE'] ?? '');
83
        $new->setAcceptTypeFromServerParams($server['HTTP_ACCEPT'] ?? '');
84
        $new->method = $server['REQUEST_METHOD'] ?? Http::GET;
85
        $new->headers = Http::getRequestHeadersFromServerArray($server);
86
        return $new;
87
    }
88
89
    private function setContentTypeFromServerParams(string $content_type): void
90
    {
91
        $known_content_types = [
92
            Http::CONTENT_JSON,
93
            Http::CONTENT_XML,
94
            Http::CONTENT_TEXT_XML,
95
            Http::CONTENT_FORM
96
        ];
97
        foreach ($known_content_types as $t) {
98
            if (strpos($content_type, $t) !== false) {
99
                $this->content_type = $t;
100
                return;
101
            }
102
        }
103
    }
104
105
    private function setAcceptTypeFromServerParams(string $accept_type): void
106
    {
107
        $known_accept_types = [
108
            Http::CONTENT_JSON,
109
            Http::CONTENT_HTML,
110
            Http::CONTENT_XML,
111
            Http::CONTENT_TEXT_XML,
112
        ];
113
        foreach ($known_accept_types as $t) {
114
            if (strpos($accept_type, $t) !== false) {
115
                $this->accept_type = $t;
116
                return;
117
            }
118
        }
119
    }
120
121
    public function acceptType(): string
122
    {
123
        return $this->accept_type;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getServerParams(): array
130
    {
131
        return $this->server;
132
    }
133
134
    public function getServerParam(string $name): ?string
135
    {
136
        return $this->server[$name] ?? null;
137
    }
138
139
    /**
140
     * @param array $post
141
     * @return Request
142
     */
143
    public function withPostData(array $post): Request
144
    {
145
        $new = clone $this;
146
        $new->post = $post;
147
        return $new;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    public function getPostData(): array
154
    {
155
        return $this->post;
156
    }
157
158
    /**
159
     * @param array $get
160
     * @return Request
161
     */
162
    public function withGetData(array $get): Request
163
    {
164
        $new = clone $this;
165
        $new->get = $get;
166
        return $new;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getGetData(): array
173
    {
174
        return $this->get;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function query(): array
181
    {
182
        return $this->get;
183
    }
184
185
    /**
186
     * @param array $parameters
187
     * @return Request
188
     */
189
    public function withUrlParams(array $parameters): Request
190
    {
191
        $new = clone $this;
192
        $new->parameters = $parameters;
193
        return $new;
194
    }
195
196
    /**
197
     * @return array
198
     */
199
    public function getUrlParams(): array
200
    {
201
        return $this->parameters;
202
    }
203
204
    /**
205
     * @param interfaces\Route $route
206
     * @return Request
207
     */
208
    public function withRoute(interfaces\Route $route): Request
209
    {
210
        $new = clone $this;
211
        $new->route = $route;
212
        $new->parameters = $route->parameters();
213
        return $new;
214
    }
215
216
    /**
217
     * @return interfaces\Route|null
218
     */
219
    public function route(): ?interfaces\Route
220
    {
221
        return $this->route;
222
    }
223
224
    public function withSession(interfaces\Session $session): Request
225
    {
226
        $new = clone $this;
227
        $new->session = $session;
228
        return $new;
229
    }
230
    /**
231
     * Returns the session var at $key or the Session object
232
     *
233
     * First call to this method will initiate the session
234
     *
235
     * @param string $key Dot notation for deeper values, i.e. `user.email`
236
     * @return mixed|interfaces\Session
237
     */
238
    public function session(?string $key = null)
239
    {
240
        if (is_null($key)) {
241
            $this->session->startIfNotStarted();
242
            return $this->session;
243
        }
244
        return $this->session->get($key);
245
    }
246
247
    /**
248
     * Redirect NOW the request to $url
249
     *
250
     * @codeCoverageIgnore
251
     * @param $url
252
     */
253
    public function redirect($url)
254
    {
255
        // @TODO add support for reverse route match
256
        header("Location: " . $url);
257
        exit;
258
    }
259
}
260