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

Request::withUrlParams()   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 9.4285
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_TEXT_XML,
94
            Message::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
            Message::CONTENT_JSON,
108
            Message::CONTENT_XML,
109
            Message::CONTENT_TEXT_XML,
110
        ];
111
        foreach ($known_accept_types as $t) {
112
            if (strpos($accept_type, $t) !== false) {
113
                $this->accept_type = $t;
114
                return;
115
            }
116
        }
117
    }
118
119
    public function acceptType(): string
120
    {
121
        return $this->accept_type;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getServerParam(): array
128
    {
129
        return $this->server;
130
    }
131
132
    /**
133
     * @param array $post
134
     * @return Request
135
     */
136
    public function withPostData(array $post): Request
137
    {
138
        $new = clone $this;
139
        $new->post = $post;
140
        return $new;
141
    }
142
143
    /**
144
     * @return array
145
     */
146
    public function getPostData(): array
147
    {
148
        return $this->post;
149
    }
150
151
    /**
152
     * @param array $get
153
     * @return Request
154
     */
155
    public function withGetData(array $get): Request
156
    {
157
        $new = clone $this;
158
        $new->get = $get;
159
        return $new;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function getGetData(): array
166
    {
167
        return $this->get;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function query(): array
174
    {
175
        return $this->get;
176
    }
177
178
    /**
179
     * @param array $parameters
180
     * @return Request
181
     */
182
    public function withUrlParams(array $parameters): Request
183
    {
184
        $new = clone $this;
185
        $new->parameters = $parameters;
186
        return $new;
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    public function getUrlParams(): array
193
    {
194
        return $this->parameters;
195
    }
196
197
    /**
198
     * @param interfaces\Route $route
199
     * @return Request
200
     */
201
    public function withRoute(interfaces\Route $route): Request
202
    {
203
        $new = clone $this;
204
        $new->route = $route;
205
        $new->parameters = $route->parameters();
206
        return $new;
207
    }
208
209
    /**
210
     * @return interfaces\Route|null
211
     */
212
    public function route(): ?interfaces\Route
213
    {
214
        return $this->route;
215
    }
216
}
217