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