Completed
Push — master ( 396557...287168 )
by Alexander
01:58
created

Request::withPageVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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