Completed
Push — master ( 30766b...13771c )
by Berend
02:56 queued 10s
created
src/ServerRequest.php 1 patch
Indentation   +407 added lines, -407 removed lines patch added patch discarded remove patch
@@ -21,411 +21,411 @@
 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
-	 * @throws \JsonException
313
-	 */
314
-	public function getParsedBody()
315
-	{
316
-		if ($this->parsedBody !== null) {
317
-			return $this->parsedBody;
318
-		}
319
-		if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) {
320
-			return $this->postParams;
321
-		}
322
-		if ($this->hasContentType('application/json') || $this->hasContentType('text/plain')) {
323
-			return json_decode((string) $this->getBody(), true, 512, \JSON_THROW_ON_ERROR);
324
-		}
325
-		return null;
326
-	}
327
-
328
-
329
-	/**
330
-	 * Checks if a content type header exists with the given content type.
331
-	 *
332
-	 * @param string $contentType
333
-	 * @return bool true if a content type header exists with the given content type.
334
-	 */
335
-	private function hasContentType($contentType)
336
-	{
337
-		foreach ($this->getHeader('Content-Type') as $key => $value) {
338
-			if (mb_substr($value, 0, strlen($contentType)) == $contentType) {
339
-				return true;
340
-			}
341
-		}
342
-
343
-		return false;
344
-	}
345
-
346
-	/**
347
-	 * Set the parsed body.
348
-	 *
349
-	 * @param null|array|object $parsedBody
350
-	 * @return $this
351
-	 */
352
-	private function setParsedBody($parsedBody)
353
-	{
354
-		$this->parsedBody = $parsedBody;
355
-
356
-		return $this;
357
-	}
358
-
359
-	/**
360
-	 * {@inheritdoc}
361
-	 */
362
-	public function withParsedBody($parsedBody)
363
-	{
364
-		$result = clone $this;
365
-
366
-		return $result->setParsedBody($parsedBody);
367
-	}
368
-
369
-	/**
370
-	 * {@inheritdoc}
371
-	 */
372
-	public function getAttributes()
373
-	{
374
-		return $this->attributes;
375
-	}
376
-
377
-	/**
378
-	 * {@inheritdoc}
379
-	 */
380
-	public function getAttribute($name, $default = null)
381
-	{
382
-		return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
383
-	}
384
-
385
-	/**
386
-	 * Set the attribute.
387
-	 *
388
-	 * @param string $name
389
-	 * @param mixed $value
390
-	 * @return $this
391
-	 */
392
-	private function setAttribute($name, $value)
393
-	{
394
-		$this->attributes[$name] = $value;
395
-
396
-		return $this;
397
-	}
398
-
399
-	/**
400
-	 * {@inheritdoc}
401
-	 */
402
-	public function withAttribute($name, $value)
403
-	{
404
-		$result = clone $this;
405
-
406
-		return $result->setAttribute($name, $value);
407
-	}
408
-
409
-	/**
410
-	 * Remove the attribute.
411
-	 *
412
-	 * @param string $name
413
-	 * @return $this
414
-	 */
415
-	private function removeAttribute($name)
416
-	{
417
-		unset($this->attributes[$name]);
418
-
419
-		return $this;
420
-	}
421
-
422
-	/**
423
-	 * {@inheritdoc}
424
-	 */
425
-	public function withoutAttribute($name)
426
-	{
427
-		$result = clone $this;
428
-
429
-		return $result->removeAttribute($name);
430
-	}
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
+     * @throws \JsonException
313
+     */
314
+    public function getParsedBody()
315
+    {
316
+        if ($this->parsedBody !== null) {
317
+            return $this->parsedBody;
318
+        }
319
+        if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) {
320
+            return $this->postParams;
321
+        }
322
+        if ($this->hasContentType('application/json') || $this->hasContentType('text/plain')) {
323
+            return json_decode((string) $this->getBody(), true, 512, \JSON_THROW_ON_ERROR);
324
+        }
325
+        return null;
326
+    }
327
+
328
+
329
+    /**
330
+     * Checks if a content type header exists with the given content type.
331
+     *
332
+     * @param string $contentType
333
+     * @return bool true if a content type header exists with the given content type.
334
+     */
335
+    private function hasContentType($contentType)
336
+    {
337
+        foreach ($this->getHeader('Content-Type') as $key => $value) {
338
+            if (mb_substr($value, 0, strlen($contentType)) == $contentType) {
339
+                return true;
340
+            }
341
+        }
342
+
343
+        return false;
344
+    }
345
+
346
+    /**
347
+     * Set the parsed body.
348
+     *
349
+     * @param null|array|object $parsedBody
350
+     * @return $this
351
+     */
352
+    private function setParsedBody($parsedBody)
353
+    {
354
+        $this->parsedBody = $parsedBody;
355
+
356
+        return $this;
357
+    }
358
+
359
+    /**
360
+     * {@inheritdoc}
361
+     */
362
+    public function withParsedBody($parsedBody)
363
+    {
364
+        $result = clone $this;
365
+
366
+        return $result->setParsedBody($parsedBody);
367
+    }
368
+
369
+    /**
370
+     * {@inheritdoc}
371
+     */
372
+    public function getAttributes()
373
+    {
374
+        return $this->attributes;
375
+    }
376
+
377
+    /**
378
+     * {@inheritdoc}
379
+     */
380
+    public function getAttribute($name, $default = null)
381
+    {
382
+        return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
383
+    }
384
+
385
+    /**
386
+     * Set the attribute.
387
+     *
388
+     * @param string $name
389
+     * @param mixed $value
390
+     * @return $this
391
+     */
392
+    private function setAttribute($name, $value)
393
+    {
394
+        $this->attributes[$name] = $value;
395
+
396
+        return $this;
397
+    }
398
+
399
+    /**
400
+     * {@inheritdoc}
401
+     */
402
+    public function withAttribute($name, $value)
403
+    {
404
+        $result = clone $this;
405
+
406
+        return $result->setAttribute($name, $value);
407
+    }
408
+
409
+    /**
410
+     * Remove the attribute.
411
+     *
412
+     * @param string $name
413
+     * @return $this
414
+     */
415
+    private function removeAttribute($name)
416
+    {
417
+        unset($this->attributes[$name]);
418
+
419
+        return $this;
420
+    }
421
+
422
+    /**
423
+     * {@inheritdoc}
424
+     */
425
+    public function withoutAttribute($name)
426
+    {
427
+        $result = clone $this;
428
+
429
+        return $result->removeAttribute($name);
430
+    }
431 431
 }
Please login to merge, or discard this patch.