Completed
Pull Request — master (#25)
by Alexander
01:37
created

Request::fullUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
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 $parameters = [];
16
    protected $request = [];
17
    protected $server = [];
18
    protected $get = [];
19
    protected $post = [];
20
    protected $route = null;
21
    protected $content_type = '';
22
    protected $accept_type = Http::CONTENT_HTML;
23
24
    /**
25
     * Get request parameters from url as url params, get queries or post, in that order
26
     *
27
     * @param string $name the name of the parameter
28
     * @return mixed|null the value or null if not set
29
     */
30
    public function param(string $name)
31
    {
32
        if (isset($this->parameters[$name])) {
33
            return $this->parameters[$name];
34
        }
35
        if (isset($this->get[$name])) {
36
            return $this->get[$name];
37
        }
38
        if (isset($this->post[$name])) {
39
            return $this->post[$name];
40
        }
41
        return null;
42
    }
43
44
    public function fullUrl(?string $path = null): string
45
    {
46
        $path = $path ?? $this->url();
47
        return
48
            $this->getServerParam('REQUEST_SCHEME') . '://' .
49
            $this->getServerParam('HTTP_HOST') . $path;
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'] ?? Http::GET;
84
        $new->headers = Http::getRequestHeadersFromServerArray($server);
85
        return $new;
86
    }
87
88
    private function setContentTypeFromServerParams(string $content_type): void
89
    {
90
        $known_content_types = [
91
            Http::CONTENT_JSON,
92
            Http::CONTENT_XML,
93
            Http::CONTENT_TEXT_XML,
94
            Http::CONTENT_FORM
95
        ];
96
        foreach ($known_content_types as $t) {
97
            if (strpos($content_type, $t) !== false) {
98
                $this->content_type = $t;
99
                return;
100
            }
101
        }
102
    }
103
104
    private function setAcceptTypeFromServerParams(string $accept_type): void
105
    {
106
        $known_accept_types = [
107
            Http::CONTENT_JSON,
108
            Http::CONTENT_HTML,
109
            Http::CONTENT_XML,
110
            Http::CONTENT_TEXT_XML,
111
        ];
112
        foreach ($known_accept_types as $t) {
113
            if (strpos($accept_type, $t) !== false) {
114
                $this->accept_type = $t;
115
                return;
116
            }
117
        }
118
    }
119
120
    public function acceptType(): string
121
    {
122
        return $this->accept_type;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getServerParams(): array
129
    {
130
        return $this->server;
131
    }
132
133
    public function getServerParam(string $name): ?string
134
    {
135
        return $this->server[$name] ?? null;
136
    }
137
138
    /**
139
     * @param array $post
140
     * @return Request
141
     */
142
    public function withPostData(array $post): Request
143
    {
144
        $new = clone $this;
145
        $new->post = $post;
146
        return $new;
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getPostData(): array
153
    {
154
        return $this->post;
155
    }
156
157
    /**
158
     * @param array $get
159
     * @return Request
160
     */
161
    public function withGetData(array $get): Request
162
    {
163
        $new = clone $this;
164
        $new->get = $get;
165
        return $new;
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public function getGetData(): array
172
    {
173
        return $this->get;
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function query(): array
180
    {
181
        return $this->get;
182
    }
183
184
    /**
185
     * @param array $parameters
186
     * @return Request
187
     */
188
    public function withUrlParams(array $parameters): Request
189
    {
190
        $new = clone $this;
191
        $new->parameters = $parameters;
192
        return $new;
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function getUrlParams(): array
199
    {
200
        return $this->parameters;
201
    }
202
203
    /**
204
     * @param interfaces\Route $route
205
     * @return Request
206
     */
207
    public function withRoute(interfaces\Route $route): Request
208
    {
209
        $new = clone $this;
210
        $new->route = $route;
211
        $new->parameters = $route->parameters();
212
        return $new;
213
    }
214
215
    /**
216
     * @return interfaces\Route|null
217
     */
218
    public function route(): ?interfaces\Route
219
    {
220
        return $this->route;
221
    }
222
}
223