Completed
Push — master ( b63eb5...213c02 )
by Mark
01:54
created

Request::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Air\HTTP\Request;
4
5
class Request implements RequestInterface
6
{
7
    /**
8
     * @param string $uri The request URI.
9
     */
10
    protected $uri;
11
12
13
    /**
14
     * @param array $uriComponents An array of components that make up the URI.
15
     */
16
    protected $uriComponents;
17
18
19
    /**
20
     * @param string $method The request method.
21
     */
22
    protected $method;
23
24
25
    /**
26
     * @var array $requestData An array of request data (from POST, PUT, etc).
27
     */
28
    protected $requestData;
29
30
31
    /**
32
     * @var array $queryData An array of query data (i.e. a GET request).
33
     */
34
    protected $queryData;
35
36
37
    /**
38
     * @var array $serverData An array of server data (i.e. HTTP_REFERER, DOC_ROOT etc).
39
     */
40
    protected $serverData;
41
42
43
    /**
44
     * @var array $cookies An array of cookies the current request holds
45
     */
46
    protected $cookies;
47
48
49
    /**
50
     * @var array $fileData An array of file data.
51
     */
52
    protected $fileData;
53
54
55
    /**
56
     * Class constructor.
57
     *
58
     * @param string $uri The request URI.
59
     * @param string $method The request method.
60
     * @param array $requestData The request data.
61
     * @param array $queryData The query data.
62
     * @param array $serverData The server data.
63
     * @param array $cookies The cookies for the current request
64
     * @param array $fileData An array of file data.
65
     * @throws \InvalidArgumentException
66
     */
67
    public function __construct(
68
        $uri,
69
        $method = self::METHOD_GET,
70
        array $requestData = [],
71
        array $queryData = [],
72
        array $serverData = [],
73
        array $cookies = [],
74
        array $fileData = []
75
    ) {
76
        $this->uri = $uri;
77
        $this->method = $method;
78
        $this->requestData = $requestData;
79
        $this->queryData = $queryData;
80
        $this->serverData = $serverData;
81
        $this->cookies = $cookies;
82
        $this->fileData = $fileData;
83
84
        // Parse the URI.
85
        $parsed_uri = parse_url($uri);
86
87
        // Ensure the URI is valid, and set it.
88
        if ($parsed_uri) {
89
            $this->uriComponents = $parsed_uri;
90
        } else {
91
            throw new \InvalidArgumentException('The URI provided was malformed.');
92
        }
93
    }
94
95
96
    /**
97
     * Get the request method.
98
     *
99
     * @return string The request method.
100
     */
101
    public function getMethod()
102
    {
103
        return $this->method;
104
    }
105
106
107
    /**
108
     * Get the URI.
109
     *
110
     * @return string The URI.
111
     */
112
    public function getUri()
113
    {
114
        return $this->uri;
115
    }
116
117
118
    /**
119
     * Get the URI path.
120
     *
121
     * @return string The URI path.
122
     */
123
    public function getUriPath()
124
    {
125
        return $this->uriComponents['path'];
126
    }
127
128
129
    /**
130
     * @param mixed $key The data key.
131
     * @param mixed $default The value to return if the key is not found.
132
     *
133
     * @return mixed The request data.
134
     */
135
    public function getRequestData($key = null, $default = null)
136
    {
137
        if (!is_null($key)) {
138
            if (isset($this->requestData[$key])) {
139
                return $this->requestData[$key];
140
            } else {
141
                return $default;
142
            }
143
        } else {
144
            return $this->requestData;
145
        }
146
    }
147
148
149
    /**
150
     * Get the query data.
151
     *
152
     * @return array The query data.
153
     */
154
    public function getQueryData()
155
    {
156
        return $this->queryData;
157
    }
158
159
160
    /**
161
     * Get the server data.
162
     *
163
     * @return array The server data.
164
     */
165
    public function getServerData()
166
    {
167
        return $this->serverData;
168
    }
169
170
171
    /**
172
     * Get the HTTP REFERER from the server data.
173
     *
174
     * @return string|null The referer or null if not found.
175
     */
176
    public function getReferer()
177
    {
178
        $referer = null;
179
180
        if (array_key_exists(self::REFERER_KEY, $this->serverData)) {
181
            $referer = $this->serverData[self::REFERER_KEY];
182
        }
183
184
        return $referer;
185
    }
186
187
188
    /**
189
     * Get file data.
190
     *
191
     * @return array File data.
192
     */
193
    public function getFileData()
194
    {
195
        return $this->fileData;
196
    }
197
198
199
    /**
200
     * Add data to the request.
201
     *
202
     * @param array $data An array of data.
203
     */
204
    public function addRequestData(array $data)
205
    {
206
        $this->requestData = array_merge($this->requestData, $data);
207
    }
208
209
210
    /**
211
     * @return bool Whether the method is post or not.
212
     */
213
    public function isPost()
214
    {
215
        return $this->method === self::METHOD_POST;
216
    }
217
218
219
    /**
220
     * @return bool Whether the method is get or not.
221
     */
222
    public function isGet()
223
    {
224
        return $this->method === self::METHOD_GET;
225
    }
226
227
228
    /**
229
     * Get cookie value by name
230
     *
231
     * @param string $name
232
     *
233
     * @return string|null returns the cookie value for the index name provided
234
     */
235
    public function getCookie($name)
236
    {
237
        return (isset($this->cookies[$name])) ? $this->cookies[$name] : null;
238
    }
239
240
241
    /**
242
     * Returns true if the cookie is set for the name provided else false
243
     *
244
     * @param string @name
245
     *
246
     * @return boolean
247
     */
248
    public function issetCookie($name)
249
    {
250
        return ($this->getCookie($name) !== null);
251
    }
252
253
254
    /**
255
     * Returns the content of the request.
256
     *
257
     * @return string
258
     */
259
    public function getContent()
260
    {
261
        return file_get_contents('php://input');
262
    }
263
}
264