Completed
Push — master ( 988ea9...868c80 )
by smiley
07:26
created

ServerRequest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 19
lcom 5
cbo 1
1
<?php
2
/**
3
 * Class ServerRequest
4
 *
5
 * @filesource   ServerRequest.php
6
 * @created      11.08.2018
7
 * @package      chillerlan\HTTP\Psr7
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\HTTP\Psr7;
14
15
use InvalidArgumentException;
16
use Psr\Http\Message\ServerRequestInterface;
17
18
use function array_key_exists, is_array, is_object;
19
20
final class ServerRequest extends Request implements ServerRequestInterface{
21
22
	/**
23
	 * @var array
24
	 */
25
	private array $serverParams;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
26
27
	/**
28
	 * @var array
29
	 */
30
	private array $cookieParams = [];
31
32
	/**
33
	 * @var array
34
	 */
35
	private array $queryParams = [];
36
37
	/**
38
	 * @var null|array|object
39
	 */
40
	private $parsedBody;
41
42
	/**
43
	 * @var array
44
	 */
45
	private array $attributes = [];
46
47
	/**
48
	 * @var array
49
	 */
50
	private array $uploadedFiles = [];
51
52
	/**
53
	 * ServerRequest constructor.
54
	 *
55
	 * @param string                                                 $method
56
	 * @param string|\Psr\Http\Message\UriInterface                  $uri
57
	 * @param array|null                                             $headers
58
	 * @param null|string|resource|\Psr\Http\Message\StreamInterface $body
59
	 * @param string|null                                            $version
60
	 * @param array|null                                             $serverParams
61
	 */
62
	public function __construct(string $method, $uri, array $headers = null, $body = null, string $version = null, array $serverParams = null){
63
		parent::__construct($method, $uri, $headers, $body, $version);
64
65
		$this->serverParams = $serverParams ?? [];
66
	}
67
68
	/**
69
	 * @inheritDoc
70
	 */
71
	public function getServerParams():array{
72
		return $this->serverParams;
73
	}
74
75
	/**
76
	 * @inheritDoc
77
	 */
78
	public function getCookieParams():array{
79
		return $this->cookieParams;
80
	}
81
82
	/**
83
	 * @inheritDoc
84
	 */
85
	public function withCookieParams(array $cookies):ServerRequestInterface{
86
		$clone               = clone $this;
87
		$clone->cookieParams = $cookies;
88
89
		return $clone;
90
	}
91
92
	/**
93
	 * @inheritDoc
94
	 */
95
	public function getQueryParams():array{
96
		return $this->queryParams;
97
	}
98
99
	/**
100
	 * @inheritDoc
101
	 */
102
	public function withQueryParams(array $query):ServerRequestInterface{
103
		$clone              = clone $this;
104
		$clone->queryParams = $query;
105
106
		return $clone;
107
	}
108
109
	/**
110
	 * @inheritDoc
111
	 */
112
	public function getUploadedFiles():array{
113
		return $this->uploadedFiles;
114
	}
115
116
	/**
117
	 * @inheritDoc
118
	 */
119
	public function withUploadedFiles(array $uploadedFiles):ServerRequestInterface{
120
		$clone                = clone $this;
121
		$clone->uploadedFiles = $uploadedFiles;
122
123
		return $clone;
124
	}
125
126
	/**
127
	 * @inheritDoc
128
	 */
129
	public function getParsedBody(){
130
		return $this->parsedBody;
131
	}
132
133
	/**
134
	 * @inheritDoc
135
	 */
136
	public function withParsedBody($data):ServerRequestInterface{
137
138
		if($data !== null && !is_object($data) && !is_array($data)){
139
			throw new InvalidArgumentException('parsed body value must be an array, object or null');
140
		}
141
142
		$clone             = clone $this;
143
		$clone->parsedBody = $data;
144
145
		return $clone;
146
	}
147
148
	/**
149
	 * @inheritDoc
150
	 */
151
	public function getAttributes():array{
152
		return $this->attributes;
153
	}
154
155
	/**
156
	 * @inheritDoc
157
	 */
158
	public function getAttribute($name, $default = null){
159
160
		if(array_key_exists($name, $this->attributes)){
161
			return $this->attributes[$name];
162
		}
163
164
		return $default;
165
	}
166
167
	/**
168
	 * @inheritDoc
169
	 */
170
	public function withAttribute($name, $value):ServerRequestInterface{
171
		$clone                    = clone $this;
172
		$clone->attributes[$name] = $value;
173
174
		return $clone;
175
	}
176
177
	/**
178
	 * @inheritDoc
179
	 */
180
	public function withoutAttribute($name):ServerRequestInterface{
181
182
		if(array_key_exists($name, $this->attributes)){
183
			$clone = clone $this;
184
			unset($clone->attributes[$name]);
185
186
			return $clone;
187
		}
188
189
		return $this;
190
	}
191
192
}
193