Completed
Push — develop ( f46e51...0711aa )
by Berend
03:25
created
src/ServerRequest.php 1 patch
Indentation   +408 added lines, -408 removed lines patch added patch discarded remove patch
@@ -21,412 +21,412 @@
 block discarded – undo
21 21
  */
22 22
 class ServerRequest extends Request implements ServerRequestInterface
23 23
 {
24
-	/** @var array The server parameters. */
25
-	private $serverParams;
26
-
27
-	/** @var array The cookie parameters. */
28
-	private $cookieParams;
29
-
30
-	/** @var array The query parameters. */
31
-	private $queryParams;
32
-
33
-	/** @var array The post parameters. */
34
-	private $postParams;
35
-
36
-	/** @var array The files parameters. */
37
-	private $filesParams;
38
-
39
-	/** @var array The uploaded files. */
40
-	private $uploadedFiles;
41
-
42
-	/** @var null|array|object The parsed body. */
43
-	private $parsedBody;
44
-
45
-	/** @var array The attributes. */
46
-	private $attributes;
47
-
48
-	/**
49
-	 * Construct a Request object with the given method, uri, version, headers & body.
50
-	 *
51
-	 * @global array $_SERVER The server parameters.
52
-	 * @global array $_COOKIE The cookie parameters.
53
-	 * @global array $_GET The query parameters.
54
-	 * @global array $_POST The post parameters.
55
-	 * @global array $_FILES The files parameters.
56
-	 *
57
-	 * @param string $method = ''
58
-	 * @param UriInterface|null $uri = null
59
-	 * @param string $version = self::DEFAULT_VERSION
60
-	 * @param array $headers = []
61
-	 * @param StreamInterface|null $body = null
62
-	 */
63
-	public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null)
64
-	{
65
-		if ($body === null) {
66
-			$body = new Stream(fopen('php://input', 'r'));
67
-		}
68
-
69
-		$this->serverParams = $_SERVER;
70
-		$this->cookieParams = $_COOKIE;
71
-		$this->queryParams = $this->initQueryParams($this->serverParams);
72
-		$this->postParams = $_POST;
73
-		$this->filesParams = $_FILES;
74
-		$this->uploadedFiles = $this->initUploadedFiles($this->filesParams);
75
-		$this->attributes = [];
76
-
77
-		parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body);
78
-	}
79
-
80
-	/**
81
-	 * Initialize the method.
82
-	 *
83
-	 * @param string $method
84
-	 * @return string the method.
85
-	 */
86
-	private function initMethod($method)
87
-	{
88
-		return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method;
89
-	}
90
-
91
-	/**
92
-	 * Initialize the URI.
93
-	 *
94
-	 * @param UriInterface|null $uri
95
-	 * @return UriInterface the URI.
96
-	 */
97
-	private function initUri($uri)
98
-	{
99
-		if ($uri !== null) {
100
-			return $uri;
101
-		}
102
-
103
-		$scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://';
104
-		$host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : '';
105
-		$path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : '';
106
-
107
-		return new URI($host . $path);
108
-	}
109
-
110
-	/**
111
-	 * Initialize the headers.
112
-	 *
113
-	 * @param array $headers
114
-	 * @return array the headers.
115
-	 */
116
-	private function initHeaders($headers)
117
-	{
118
-		return $headers ?: getallheaders();
119
-	}
120
-
121
-	/**
122
-	 * Initialize the headers.
123
-	 *
124
-	 * @param string $serverParams
125
-	 * @return array the headers.
126
-	 */
127
-	private function initQueryParams($serverParams)
128
-	{
129
-		$result = [];
130
-
131
-		if (isset($serverParams['REQUEST_URI']) && ($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) {
132
-			parse_str($query, $result);
133
-		}
134
-
135
-		return $result ?? [];
136
-	}
137
-
138
-	/**
139
-	 * Initialize the uploaded files.
140
-	 *
141
-	 * @param array $files
142
-	 * @return array the uploaded files.
143
-	 */
144
-	private function initUploadedFiles(array $files)
145
-	{
146
-		$result = [];
147
-
148
-		foreach ($files as $key => $value) {
149
-			$result[$key] = $this->parseUploadedFiles($value);
150
-		}
151
-
152
-		return $result;
153
-	}
154
-
155
-	/**
156
-	 * Parse uploaded files.
157
-	 *
158
-	 * @param array $files
159
-	 * @return UploadedFile|array uploaded files.
160
-	 */
161
-	private function parseUploadedFiles($files)
162
-	{
163
-		// Empty
164
-		$first = reset($files);
165
-
166
-		// Single
167
-		if (!is_array($first)) {
168
-			return $this->parseSingleUploadedFiles($files);
169
-		}
170
-
171
-		// Multiple
172
-		if (count(array_filter(array_keys($first), 'is_string')) === 0) {
173
-			return $this->parseMultipleUploadedFiles($files);
174
-		}
175
-
176
-		// Namespace
177
-		return $this->initUploadedFiles($files);
178
-	}
179
-
180
-	/**
181
-	 * Parse single uploaded file.
182
-	 *
183
-	 * @param array $file
184
-	 * @return UploadedFile single uploaded file.
185
-	 */
186
-	private function parseSingleUploadedFiles(array $file)
187
-	{
188
-		return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']);
189
-	}
190
-
191
-	/**
192
-	 * Parse multiple uploaded files.
193
-	 *
194
-	 * @param array $files
195
-	 * @return UploadedFiles[] multiple uploaded files.
196
-	 */
197
-	private function parseMultipleUploadedFiles(array $files)
198
-	{
199
-		$count = count($files['name']);
200
-		$result = [];
201
-
202
-		for ($i = 0; $i < $count; $i++) {
203
-			$result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]);
204
-		}
205
-
206
-		return $result;
207
-	}
208
-
209
-	/**
210
-	 * {@inheritdoc}
211
-	 */
212
-	public function getServerParams()
213
-	{
214
-		return $this->serverParams;
215
-	}
216
-
217
-	/**
218
-	 * {@inheritdoc}
219
-	 */
220
-	public function getCookieParams()
221
-	{
222
-		return $this->cookieParams;
223
-	}
224
-
225
-	/**
226
-	 * Set the cookie params.
227
-	 *
228
-	 * @param array $cookieParams
229
-	 * @return $this
230
-	 */
231
-	private function setCookieParams(array $cookieParams)
232
-	{
233
-		$this->cookieParams = $cookieParams;
234
-
235
-		return $this;
236
-	}
237
-
238
-	/**
239
-	 * {@inheritdoc}
240
-	 */
241
-	public function withCookieParams(array $cookieParams)
242
-	{
243
-		$result = clone $this;
244
-
245
-		return $result->setCookieParams($cookieParams);
246
-	}
247
-
248
-	/**
249
-	 * {@inheritdoc}
250
-	 */
251
-	public function getQueryParams()
252
-	{
253
-		return $this->queryParams;
254
-	}
255
-
256
-	/**
257
-	 * Set the query params.
258
-	 *
259
-	 * @param array $queryParams
260
-	 * @return $this
261
-	 */
262
-	private function setQueryParams(array $queryParams)
263
-	{
264
-		$this->queryParams = $queryParams;
265
-
266
-		return $this;
267
-	}
268
-
269
-	/**
270
-	 * {@inheritdoc}
271
-	 */
272
-	public function withQueryParams(array $queryParams)
273
-	{
274
-		$result = clone $this;
275
-
276
-		return $result->setQueryParams($queryParams);
277
-	}
278
-
279
-	/**
280
-	 * {@inheritdoc}
281
-	 */
282
-	public function getUploadedFiles()
283
-	{
284
-		return $this->uploadedFiles;
285
-	}
286
-
287
-	/**
288
-	 * Set the uploaded files.
289
-	 *
290
-	 * @param array $uploadedFiles
291
-	 * @return $this
292
-	 */
293
-	private function setUploadedFiles(array $uploadedFiles)
294
-	{
295
-		$this->uploadedFiles = $uploadedFiles;
296
-
297
-		return $this;
298
-	}
299
-
300
-	/**
301
-	 * {@inheritdoc}
302
-	 */
303
-	public function withUploadedFiles(array $uploadedFiles)
304
-	{
305
-		$result = clone $this;
306
-
307
-		return $result->setUploadedFiles($uploadedFiles);
308
-	}
309
-
310
-	/**
311
-	 * {@inheritdoc}
312
-	 */
313
-	public function getParsedBody()
314
-	{
315
-		if ($this->parsedBody !== null) {
316
-			return $this->parsedBody;
317
-		}
318
-
319
-		if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) {
320
-			return $this->postParams;
321
-		}
322
-
323
-		if ($this->hasContentType('application/json')) {
324
-			return json_decode((string) $this->getBody(), true);
325
-		}
326
-
327
-		return null;
328
-	}
329
-
330
-	/**
331
-	 * Checks if a content type header exists with the given content type.
332
-	 *
333
-	 * @param string $contentType
334
-	 * @return bool true if a content type header exists with the given content type.
335
-	 */
336
-	private function hasContentType($contentType)
337
-	{
338
-		foreach ($this->getHeader('Content-Type') as $key => $value) {
339
-			if (mb_substr($value, 0, strlen($contentType)) == $contentType) {
340
-				return true;
341
-			}
342
-		}
343
-
344
-		return false;
345
-	}
346
-
347
-	/**
348
-	 * Set the parsed body.
349
-	 *
350
-	 * @param null|array|object $parsedBody
351
-	 * @return $this
352
-	 */
353
-	private function setParsedBody($parsedBody)
354
-	{
355
-		$this->parsedBody = $parsedBody;
356
-
357
-		return $this;
358
-	}
359
-
360
-	/**
361
-	 * {@inheritdoc}
362
-	 */
363
-	public function withParsedBody($parsedBody)
364
-	{
365
-		$result = clone $this;
366
-
367
-		return $result->setParsedBody($parsedBody);
368
-	}
369
-
370
-	/**
371
-	 * {@inheritdoc}
372
-	 */
373
-	public function getAttributes()
374
-	{
375
-		return $this->attributes;
376
-	}
377
-
378
-	/**
379
-	 * {@inheritdoc}
380
-	 */
381
-	public function getAttribute($name, $default = null)
382
-	{
383
-		return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
384
-	}
385
-
386
-	/**
387
-	 * Set the attribute.
388
-	 *
389
-	 * @param string $name
390
-	 * @param mixed $value
391
-	 * @return $this
392
-	 */
393
-	private function setAttribute($name, $value)
394
-	{
395
-		$this->attributes[$name] = $value;
396
-
397
-		return $this;
398
-	}
399
-
400
-	/**
401
-	 * {@inheritdoc}
402
-	 */
403
-	public function withAttribute($name, $value)
404
-	{
405
-		$result = clone $this;
406
-
407
-		return $result->setAttribute($name, $value);
408
-	}
409
-
410
-	/**
411
-	 * Remove the attribute.
412
-	 *
413
-	 * @param string $name
414
-	 * @return $this
415
-	 */
416
-	private function removeAttribute($name)
417
-	{
418
-		unset($this->attributes[$name]);
419
-
420
-		return $this;
421
-	}
422
-
423
-	/**
424
-	 * {@inheritdoc}
425
-	 */
426
-	public function withoutAttribute($name)
427
-	{
428
-		$result = clone $this;
429
-
430
-		return $result->removeAttribute($name);
431
-	}
24
+    /** @var array The server parameters. */
25
+    private $serverParams;
26
+
27
+    /** @var array The cookie parameters. */
28
+    private $cookieParams;
29
+
30
+    /** @var array The query parameters. */
31
+    private $queryParams;
32
+
33
+    /** @var array The post parameters. */
34
+    private $postParams;
35
+
36
+    /** @var array The files parameters. */
37
+    private $filesParams;
38
+
39
+    /** @var array The uploaded files. */
40
+    private $uploadedFiles;
41
+
42
+    /** @var null|array|object The parsed body. */
43
+    private $parsedBody;
44
+
45
+    /** @var array The attributes. */
46
+    private $attributes;
47
+
48
+    /**
49
+     * Construct a Request object with the given method, uri, version, headers & body.
50
+     *
51
+     * @global array $_SERVER The server parameters.
52
+     * @global array $_COOKIE The cookie parameters.
53
+     * @global array $_GET The query parameters.
54
+     * @global array $_POST The post parameters.
55
+     * @global array $_FILES The files parameters.
56
+     *
57
+     * @param string $method = ''
58
+     * @param UriInterface|null $uri = null
59
+     * @param string $version = self::DEFAULT_VERSION
60
+     * @param array $headers = []
61
+     * @param StreamInterface|null $body = null
62
+     */
63
+    public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null)
64
+    {
65
+        if ($body === null) {
66
+            $body = new Stream(fopen('php://input', 'r'));
67
+        }
68
+
69
+        $this->serverParams = $_SERVER;
70
+        $this->cookieParams = $_COOKIE;
71
+        $this->queryParams = $this->initQueryParams($this->serverParams);
72
+        $this->postParams = $_POST;
73
+        $this->filesParams = $_FILES;
74
+        $this->uploadedFiles = $this->initUploadedFiles($this->filesParams);
75
+        $this->attributes = [];
76
+
77
+        parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body);
78
+    }
79
+
80
+    /**
81
+     * Initialize the method.
82
+     *
83
+     * @param string $method
84
+     * @return string the method.
85
+     */
86
+    private function initMethod($method)
87
+    {
88
+        return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method;
89
+    }
90
+
91
+    /**
92
+     * Initialize the URI.
93
+     *
94
+     * @param UriInterface|null $uri
95
+     * @return UriInterface the URI.
96
+     */
97
+    private function initUri($uri)
98
+    {
99
+        if ($uri !== null) {
100
+            return $uri;
101
+        }
102
+
103
+        $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://';
104
+        $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : '';
105
+        $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : '';
106
+
107
+        return new URI($host . $path);
108
+    }
109
+
110
+    /**
111
+     * Initialize the headers.
112
+     *
113
+     * @param array $headers
114
+     * @return array the headers.
115
+     */
116
+    private function initHeaders($headers)
117
+    {
118
+        return $headers ?: getallheaders();
119
+    }
120
+
121
+    /**
122
+     * Initialize the headers.
123
+     *
124
+     * @param string $serverParams
125
+     * @return array the headers.
126
+     */
127
+    private function initQueryParams($serverParams)
128
+    {
129
+        $result = [];
130
+
131
+        if (isset($serverParams['REQUEST_URI']) && ($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) {
132
+            parse_str($query, $result);
133
+        }
134
+
135
+        return $result ?? [];
136
+    }
137
+
138
+    /**
139
+     * Initialize the uploaded files.
140
+     *
141
+     * @param array $files
142
+     * @return array the uploaded files.
143
+     */
144
+    private function initUploadedFiles(array $files)
145
+    {
146
+        $result = [];
147
+
148
+        foreach ($files as $key => $value) {
149
+            $result[$key] = $this->parseUploadedFiles($value);
150
+        }
151
+
152
+        return $result;
153
+    }
154
+
155
+    /**
156
+     * Parse uploaded files.
157
+     *
158
+     * @param array $files
159
+     * @return UploadedFile|array uploaded files.
160
+     */
161
+    private function parseUploadedFiles($files)
162
+    {
163
+        // Empty
164
+        $first = reset($files);
165
+
166
+        // Single
167
+        if (!is_array($first)) {
168
+            return $this->parseSingleUploadedFiles($files);
169
+        }
170
+
171
+        // Multiple
172
+        if (count(array_filter(array_keys($first), 'is_string')) === 0) {
173
+            return $this->parseMultipleUploadedFiles($files);
174
+        }
175
+
176
+        // Namespace
177
+        return $this->initUploadedFiles($files);
178
+    }
179
+
180
+    /**
181
+     * Parse single uploaded file.
182
+     *
183
+     * @param array $file
184
+     * @return UploadedFile single uploaded file.
185
+     */
186
+    private function parseSingleUploadedFiles(array $file)
187
+    {
188
+        return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']);
189
+    }
190
+
191
+    /**
192
+     * Parse multiple uploaded files.
193
+     *
194
+     * @param array $files
195
+     * @return UploadedFiles[] multiple uploaded files.
196
+     */
197
+    private function parseMultipleUploadedFiles(array $files)
198
+    {
199
+        $count = count($files['name']);
200
+        $result = [];
201
+
202
+        for ($i = 0; $i < $count; $i++) {
203
+            $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]);
204
+        }
205
+
206
+        return $result;
207
+    }
208
+
209
+    /**
210
+     * {@inheritdoc}
211
+     */
212
+    public function getServerParams()
213
+    {
214
+        return $this->serverParams;
215
+    }
216
+
217
+    /**
218
+     * {@inheritdoc}
219
+     */
220
+    public function getCookieParams()
221
+    {
222
+        return $this->cookieParams;
223
+    }
224
+
225
+    /**
226
+     * Set the cookie params.
227
+     *
228
+     * @param array $cookieParams
229
+     * @return $this
230
+     */
231
+    private function setCookieParams(array $cookieParams)
232
+    {
233
+        $this->cookieParams = $cookieParams;
234
+
235
+        return $this;
236
+    }
237
+
238
+    /**
239
+     * {@inheritdoc}
240
+     */
241
+    public function withCookieParams(array $cookieParams)
242
+    {
243
+        $result = clone $this;
244
+
245
+        return $result->setCookieParams($cookieParams);
246
+    }
247
+
248
+    /**
249
+     * {@inheritdoc}
250
+     */
251
+    public function getQueryParams()
252
+    {
253
+        return $this->queryParams;
254
+    }
255
+
256
+    /**
257
+     * Set the query params.
258
+     *
259
+     * @param array $queryParams
260
+     * @return $this
261
+     */
262
+    private function setQueryParams(array $queryParams)
263
+    {
264
+        $this->queryParams = $queryParams;
265
+
266
+        return $this;
267
+    }
268
+
269
+    /**
270
+     * {@inheritdoc}
271
+     */
272
+    public function withQueryParams(array $queryParams)
273
+    {
274
+        $result = clone $this;
275
+
276
+        return $result->setQueryParams($queryParams);
277
+    }
278
+
279
+    /**
280
+     * {@inheritdoc}
281
+     */
282
+    public function getUploadedFiles()
283
+    {
284
+        return $this->uploadedFiles;
285
+    }
286
+
287
+    /**
288
+     * Set the uploaded files.
289
+     *
290
+     * @param array $uploadedFiles
291
+     * @return $this
292
+     */
293
+    private function setUploadedFiles(array $uploadedFiles)
294
+    {
295
+        $this->uploadedFiles = $uploadedFiles;
296
+
297
+        return $this;
298
+    }
299
+
300
+    /**
301
+     * {@inheritdoc}
302
+     */
303
+    public function withUploadedFiles(array $uploadedFiles)
304
+    {
305
+        $result = clone $this;
306
+
307
+        return $result->setUploadedFiles($uploadedFiles);
308
+    }
309
+
310
+    /**
311
+     * {@inheritdoc}
312
+     */
313
+    public function getParsedBody()
314
+    {
315
+        if ($this->parsedBody !== null) {
316
+            return $this->parsedBody;
317
+        }
318
+
319
+        if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) {
320
+            return $this->postParams;
321
+        }
322
+
323
+        if ($this->hasContentType('application/json')) {
324
+            return json_decode((string) $this->getBody(), true);
325
+        }
326
+
327
+        return null;
328
+    }
329
+
330
+    /**
331
+     * Checks if a content type header exists with the given content type.
332
+     *
333
+     * @param string $contentType
334
+     * @return bool true if a content type header exists with the given content type.
335
+     */
336
+    private function hasContentType($contentType)
337
+    {
338
+        foreach ($this->getHeader('Content-Type') as $key => $value) {
339
+            if (mb_substr($value, 0, strlen($contentType)) == $contentType) {
340
+                return true;
341
+            }
342
+        }
343
+
344
+        return false;
345
+    }
346
+
347
+    /**
348
+     * Set the parsed body.
349
+     *
350
+     * @param null|array|object $parsedBody
351
+     * @return $this
352
+     */
353
+    private function setParsedBody($parsedBody)
354
+    {
355
+        $this->parsedBody = $parsedBody;
356
+
357
+        return $this;
358
+    }
359
+
360
+    /**
361
+     * {@inheritdoc}
362
+     */
363
+    public function withParsedBody($parsedBody)
364
+    {
365
+        $result = clone $this;
366
+
367
+        return $result->setParsedBody($parsedBody);
368
+    }
369
+
370
+    /**
371
+     * {@inheritdoc}
372
+     */
373
+    public function getAttributes()
374
+    {
375
+        return $this->attributes;
376
+    }
377
+
378
+    /**
379
+     * {@inheritdoc}
380
+     */
381
+    public function getAttribute($name, $default = null)
382
+    {
383
+        return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
384
+    }
385
+
386
+    /**
387
+     * Set the attribute.
388
+     *
389
+     * @param string $name
390
+     * @param mixed $value
391
+     * @return $this
392
+     */
393
+    private function setAttribute($name, $value)
394
+    {
395
+        $this->attributes[$name] = $value;
396
+
397
+        return $this;
398
+    }
399
+
400
+    /**
401
+     * {@inheritdoc}
402
+     */
403
+    public function withAttribute($name, $value)
404
+    {
405
+        $result = clone $this;
406
+
407
+        return $result->setAttribute($name, $value);
408
+    }
409
+
410
+    /**
411
+     * Remove the attribute.
412
+     *
413
+     * @param string $name
414
+     * @return $this
415
+     */
416
+    private function removeAttribute($name)
417
+    {
418
+        unset($this->attributes[$name]);
419
+
420
+        return $this;
421
+    }
422
+
423
+    /**
424
+     * {@inheritdoc}
425
+     */
426
+    public function withoutAttribute($name)
427
+    {
428
+        $result = clone $this;
429
+
430
+        return $result->removeAttribute($name);
431
+    }
432 432
 }
Please login to merge, or discard this patch.