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

Request::setRouteFromRouter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
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
        $content_type = $server['HTTP_CONTENT_TYPE'] ?? false;
82
        if ($content_type && $content_type !== '*/*') {
83
            if (strpos($content_type, 'application/json') !== false) {
84
                $new->content_type = Message::CONTENT_JSON;
85
            } elseif (strpos($content_type, 'application/xml') !== false) {
86
                $new->content_type = Message::CONTENT_XML;
87
            } elseif (strpos($content_type, 'application/x-www-form-urlencoded') !== false) {
88
                $new->content_type = Message::CONTENT_FORM;
89
            }
90
        }
91
        $accept_header = $server['HTTP_ACCEPT'] ?? false;
92
        if ($accept_header && $accept_header !== '*/*') {
93
            if (strpos($accept_header, 'application/json') !== false) {
94
                $new->accept_type = Message::CONTENT_JSON;
95
            } elseif (strpos($accept_header, 'application/xml') !== false) {
96
                $new->accept_type = Message::CONTENT_XML;
97
            }
98
        }
99
        $new->method = $server['REQUEST_METHOD'] ?? Request::GET;
100
        $new->headers = Util::getRequestHeadersFromServerArray($server);
101
        return $new;
102
    }
103
104
    public function acceptType(): string
105
    {
106
        return $this->accept_type;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getServerParam(): array
113
    {
114
        return $this->server;
115
    }
116
117
    /**
118
     * @param array $post
119
     * @return Request
120
     */
121
    public function withPostData(array $post): Request
122
    {
123
        $new = clone $this;
124
        $new->post = $post;
125
        return $new;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function getPostData(): array
132
    {
133
        return $this->post;
134
    }
135
136
    /**
137
     * @param array $get
138
     * @return Request
139
     */
140
    public function withGetData(array $get): Request
141
    {
142
        $new = clone $this;
143
        $new->get = $get;
144
        return $new;
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function getGetData(): array
151
    {
152
        return $this->get;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function query(): array
159
    {
160
        return $this->get;
161
    }
162
163
    /**
164
     * @param array $parameters
165
     * @return Request
166
     */
167
    public function withUrlParams(array $parameters): Request
168
    {
169
        $new = clone $this;
170
        $new->parameters = $parameters;
171
        return $new;
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function getUrlParams(): array
178
    {
179
        return $this->parameters;
180
    }
181
182
    /**
183
     * @param interfaces\Route $route
184
     * @return Request
185
     */
186
    public function withRoute(interfaces\Route $route): Request
187
    {
188
        $new = clone $this;
189
        $new->route = $route;
190
        $new->parameters = $route->parameters();
191
        return $new;
192
    }
193
194
    /**
195
     * @return interfaces\Route|null
196
     */
197
    public function route(): ?interfaces\Route
198
    {
199
        return $this->route;
200
    }
201
}
202