|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Nyholm\Psr7; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Message\StreamInterface; |
|
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
10
|
|
|
use Psr\Http\Message\UriInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Michael Dowling and contributors to guzzlehttp/psr7 |
|
14
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ServerRequest implements ServerRequestInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use MessageTrait; |
|
19
|
|
|
use RequestTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $attributes = []; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
private $cookieParams = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var null|array|object */ |
|
28
|
|
|
private $parsedBody; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private $queryParams = []; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
private $serverParams; |
|
35
|
|
|
|
|
36
|
|
|
/** @var UploadedFileInterface[] */ |
|
37
|
|
|
private $uploadedFiles = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $method HTTP method |
|
41
|
|
|
* @param string|UriInterface $uri URI |
|
42
|
|
|
* @param array $headers Request headers |
|
43
|
|
|
* @param string|null|resource|StreamInterface $body Request body |
|
44
|
|
|
* @param string $version Protocol version |
|
45
|
|
|
* @param array $serverParams Typically the $_SERVER superglobal |
|
46
|
|
|
*/ |
|
47
|
53 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
48
|
|
|
string $method, |
|
49
|
|
|
$uri, |
|
50
|
|
|
array $headers = [], |
|
51
|
|
|
$body = null, |
|
52
|
|
|
string $version = '1.1', |
|
53
|
|
|
array $serverParams = [] |
|
54
|
|
|
) { |
|
55
|
53 |
|
$this->serverParams = $serverParams; |
|
56
|
|
|
|
|
57
|
53 |
|
if (!($uri instanceof UriInterface)) { |
|
58
|
24 |
|
$uri = new Uri($uri); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
53 |
|
$this->method = $method; |
|
62
|
53 |
|
$this->uri = $uri; |
|
63
|
53 |
|
$this->setHeaders($headers); |
|
64
|
53 |
|
$this->protocol = $version; |
|
65
|
|
|
|
|
66
|
53 |
|
if (!$this->hasHeader('Host')) { |
|
67
|
53 |
|
$this->updateHostFromUri(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
53 |
|
if ('' !== $body && null !== $body) { |
|
71
|
|
|
$this->stream = (new StreamFactory())->createStream($body); |
|
72
|
|
|
} |
|
73
|
53 |
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
public function getServerParams(): array |
|
76
|
|
|
{ |
|
77
|
3 |
|
return $this->serverParams; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
10 |
|
public function getUploadedFiles(): array |
|
81
|
|
|
{ |
|
82
|
10 |
|
return $this->uploadedFiles; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
public function withUploadedFiles(array $uploadedFiles) |
|
86
|
|
|
{ |
|
87
|
9 |
|
$new = clone $this; |
|
88
|
9 |
|
$new->uploadedFiles = $uploadedFiles; |
|
89
|
|
|
|
|
90
|
9 |
|
return $new; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
public function getCookieParams(): array |
|
94
|
|
|
{ |
|
95
|
5 |
|
return $this->cookieParams; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
10 |
|
public function withCookieParams(array $cookies) |
|
99
|
|
|
{ |
|
100
|
10 |
|
$new = clone $this; |
|
101
|
10 |
|
$new->cookieParams = $cookies; |
|
102
|
|
|
|
|
103
|
10 |
|
return $new; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
public function getQueryParams(): array |
|
107
|
|
|
{ |
|
108
|
4 |
|
return $this->queryParams; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
10 |
|
public function withQueryParams(array $query) |
|
112
|
|
|
{ |
|
113
|
10 |
|
$new = clone $this; |
|
114
|
10 |
|
$new->queryParams = $query; |
|
115
|
|
|
|
|
116
|
10 |
|
return $new; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
public function getParsedBody() |
|
120
|
|
|
{ |
|
121
|
6 |
|
return $this->parsedBody; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
16 |
|
public function withParsedBody($data) |
|
125
|
|
|
{ |
|
126
|
16 |
|
if (!is_array($data) && !is_object($data) && null !== $data) { |
|
127
|
4 |
|
throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
12 |
|
$new = clone $this; |
|
131
|
12 |
|
$new->parsedBody = $data; |
|
132
|
|
|
|
|
133
|
12 |
|
return $new; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
public function getAttributes(): array |
|
137
|
|
|
{ |
|
138
|
3 |
|
return $this->attributes; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
4 |
View Code Duplication |
public function getAttribute($attribute, $default = null) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
4 |
|
if (false === array_key_exists($attribute, $this->attributes)) { |
|
144
|
4 |
|
return $default; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
4 |
|
return $this->attributes[$attribute]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
5 |
|
public function withAttribute($attribute, $value): self |
|
151
|
|
|
{ |
|
152
|
5 |
|
$new = clone $this; |
|
153
|
5 |
|
$new->attributes[$attribute] = $value; |
|
154
|
|
|
|
|
155
|
5 |
|
return $new; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
View Code Duplication |
public function withoutAttribute($attribute): self |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
3 |
|
if (false === array_key_exists($attribute, $this->attributes)) { |
|
161
|
1 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
3 |
|
$new = clone $this; |
|
165
|
3 |
|
unset($new->attributes[$attribute]); |
|
166
|
|
|
|
|
167
|
3 |
|
return $new; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.