Debian Codegraph @

lib/3rdParty/composer/nyholm/psr7/src/ServerRequest.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Nyholm\Psr7;
6
7 use Psr\Http\Message\{ServerRequestInterface, StreamInterface, UploadedFileInterface, UriInterface};
8
9 /**
10 * @author Michael Dowling and contributors to guzzlehttp/psr7
11 * @author Tobias Nyholm <[email protected]>
12 * @author Martijn van der Ven <[email protected]>
13 *
14 * @final This class should never be extended. See https://github.com/Nyholm/psr7/blob/master/doc/final.md
15 */
16 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 array|object|null */
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|resource|StreamInterface|null $body Request body
44 * @param string $version Protocol version
45 * @param array $serverParams Typically the $_SERVER superglobal
46 */
47 public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = [])
48 {
49 $this->serverParams = $serverParams;
50
51 if (!($uri instanceof UriInterface)) {
52 $uri = new Uri($uri);
53 }
54
55 $this->method = $method;
56 $this->uri = $uri;
57 $this->setHeaders($headers);
58 $this->protocol = $version;
59 \parse_str($uri->getQuery(), $this->queryParams);
60
61 if (!$this->hasHeader('Host')) {
62 $this->updateHostFromUri();
63 }
64
65 // If we got no body, defer initialization of the stream until ServerRequest::getBody()
66 if ('' !== $body && null !== $body) {
67 $this->stream = Stream::create($body);
68 }
69 }
70
71 public function getServerParams(): array
72 {
73 return $this->serverParams;
74 }
75
76 public function getUploadedFiles(): array
77 {
78 return $this->uploadedFiles;
79 }
80
81 /**
82 * @return static
83 */
84 public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
85 {
86 $new = clone $this;
87 $new->uploadedFiles = $uploadedFiles;
88
89 return $new;
90 }
91
92 public function getCookieParams(): array
93 {
94 return $this->cookieParams;
95 }
96
97 /**
98 * @return static
99 */
100 public function withCookieParams(array $cookies): ServerRequestInterface
101 {
102 $new = clone $this;
103 $new->cookieParams = $cookies;
104
105 return $new;
106 }
107
108 public function getQueryParams(): array
109 {
110 return $this->queryParams;
111 }
112
113 /**
114 * @return static
115 */
116 public function withQueryParams(array $query): ServerRequestInterface
117 {
118 $new = clone $this;
119 $new->queryParams = $query;
120
121 return $new;
122 }
123
124 /**
125 * @return array|object|null
126 */
127 public function getParsedBody()
128 {
129 return $this->parsedBody;
130 }
131
132 /**
133 * @return static
134 */
135 public function withParsedBody($data): ServerRequestInterface
136 {
137 if (!\is_array($data) && !\is_object($data) && null !== $data) {
138 throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
139 }
140
141 $new = clone $this;
142 $new->parsedBody = $data;
143
144 return $new;
145 }
146
147 public function getAttributes(): array
148 {
149 return $this->attributes;
150 }
151
152 /**
153 * @return mixed
154 */
155 public function getAttribute($attribute, $default = null)
156 {
157 if (!\is_string($attribute)) {
158 throw new \InvalidArgumentException('Attribute name must be a string');
159 }
160
161 if (false === \array_key_exists($attribute, $this->attributes)) {
162 return $default;
163 }
164
165 return $this->attributes[$attribute];
166 }
167
168 /**
169 * @return static
170 */
171 public function withAttribute($attribute, $value): ServerRequestInterface
172 {
173 if (!\is_string($attribute)) {
174 throw new \InvalidArgumentException('Attribute name must be a string');
175 }
176
177 $new = clone $this;
178 $new->attributes[$attribute] = $value;
179
180 return $new;
181 }
182
183 /**
184 * @return static
185 */
186 public function withoutAttribute($attribute): ServerRequestInterface
187 {
188 if (!\is_string($attribute)) {
189 throw new \InvalidArgumentException('Attribute name must be a string');
190 }
191
192 if (false === \array_key_exists($attribute, $this->attributes)) {
193 return $this;
194 }
195
196 $new = clone $this;
197 unset($new->attributes[$attribute]);
198
199 return $new;
200 }
201 }