Skip to content

Commit 9068d80

Browse files
committed
Add ServerRequest
1 parent 6bf58be commit 9068d80

File tree

2 files changed

+415
-0
lines changed

2 files changed

+415
-0
lines changed

src/ServerRequest.php

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HttpSoft\Request;
6+
7+
use Fig\Http\Message\RequestMethodInterface;
8+
use HttpSoft\Uri\UriData;
9+
use InvalidArgumentException;
10+
use Psr\Http\Message\ServerRequestInterface;
11+
use Psr\Http\Message\StreamInterface;
12+
use Psr\Http\Message\UploadedFileInterface;
13+
use Psr\Http\Message\UriInterface;
14+
15+
use function array_key_exists;
16+
use function gettype;
17+
use function get_class;
18+
use function is_array;
19+
use function is_object;
20+
use function sprintf;
21+
22+
final class ServerRequest implements ServerRequestInterface, RequestMethodInterface
23+
{
24+
use RequestTrait;
25+
26+
/**
27+
* @var array
28+
*/
29+
private array $attributes = [];
30+
31+
/**
32+
* @var array
33+
*/
34+
private array $cookieParams;
35+
36+
/**
37+
* @var array|object|null
38+
*/
39+
private $parsedBody;
40+
41+
/**
42+
* @var array
43+
*/
44+
private array $queryParams;
45+
46+
/**
47+
* @var array
48+
*/
49+
private array $serverParams;
50+
51+
/**
52+
* @var array
53+
*/
54+
private array $uploadedFiles;
55+
56+
/**
57+
* @param array $serverParams
58+
* @param array $uploadedFiles
59+
* @param array $cookieParams
60+
* @param array $queryParams
61+
* @param array|object|null $parsedBody
62+
* @param string $method
63+
* @param UriInterface|string $uri
64+
* @param StreamInterface|string|resource $body
65+
* @param array $headers
66+
* @param string $protocol
67+
*/
68+
public function __construct(
69+
array $serverParams = [],
70+
array $uploadedFiles = [],
71+
array $cookieParams = [],
72+
array $queryParams = [],
73+
$parsedBody = null,
74+
string $method = self::METHOD_GET,
75+
$uri = UriData::EMPTY_STRING,
76+
$body = 'php://input',
77+
array $headers = [],
78+
string $protocol = '1.1'
79+
) {
80+
$this->validateUploadedFiles($uploadedFiles);
81+
$this->uploadedFiles = $uploadedFiles;
82+
$this->serverParams = $serverParams;
83+
$this->cookieParams = $cookieParams;
84+
$this->queryParams = $queryParams;
85+
$this->parsedBody = $parsedBody;
86+
$this->init($method, $uri, $body, $headers, $protocol);
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
public function getServerParams(): array
93+
{
94+
return $this->serverParams;
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public function getCookieParams(): array
101+
{
102+
return $this->cookieParams;
103+
}
104+
105+
/**
106+
* {@inheritDoc}
107+
*/
108+
public function withCookieParams(array $cookies): ServerRequestInterface
109+
{
110+
$new = clone $this;
111+
$new->cookieParams = $cookies;
112+
return $new;
113+
}
114+
115+
/**
116+
* {@inheritDoc}
117+
*/
118+
public function getQueryParams(): array
119+
{
120+
return $this->queryParams;
121+
}
122+
123+
/**
124+
* {@inheritDoc}
125+
*/
126+
public function withQueryParams(array $query): ServerRequestInterface
127+
{
128+
$new = clone $this;
129+
$new->queryParams = $query;
130+
return $new;
131+
}
132+
133+
/**
134+
* {@inheritDoc}
135+
*/
136+
public function getUploadedFiles(): array
137+
{
138+
return $this->uploadedFiles;
139+
}
140+
141+
/**
142+
* {@inheritDoc}
143+
*/
144+
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
145+
{
146+
$this->validateUploadedFiles($uploadedFiles);
147+
$new = clone $this;
148+
$new->uploadedFiles = $uploadedFiles;
149+
return $new;
150+
}
151+
152+
/**
153+
* {@inheritDoc}
154+
*/
155+
public function getParsedBody()
156+
{
157+
return $this->parsedBody;
158+
}
159+
160+
/**
161+
* {@inheritDoc}
162+
*/
163+
public function withParsedBody($data): ServerRequestInterface
164+
{
165+
if (!is_array($data) && !is_object($data) && $data !== null) {
166+
throw new InvalidArgumentException(sprintf(
167+
'`%s` is not valid Parsed Body. Must be `null` or a `array` or a `object`.',
168+
gettype($data)
169+
));
170+
}
171+
172+
$new = clone $this;
173+
$new->parsedBody = $data;
174+
return $new;
175+
}
176+
177+
/**
178+
* {@inheritDoc}
179+
*/
180+
public function getAttributes(): array
181+
{
182+
return $this->attributes;
183+
}
184+
185+
/**
186+
* {@inheritDoc}
187+
*/
188+
public function getAttribute($name, $default = null)
189+
{
190+
if (array_key_exists($name, $this->attributes)) {
191+
return $this->attributes[$name];
192+
}
193+
194+
return $default;
195+
}
196+
197+
/**
198+
* {@inheritDoc}
199+
*/
200+
public function withAttribute($name, $value): ServerRequestInterface
201+
{
202+
if (array_key_exists($name, $this->attributes) && $this->attributes[$name] === $value) {
203+
return $this;
204+
}
205+
206+
$new = clone $this;
207+
$new->attributes[$name] = $value;
208+
return $new;
209+
}
210+
211+
/**
212+
* {@inheritDoc}
213+
*/
214+
public function withoutAttribute($name): ServerRequestInterface
215+
{
216+
if (!array_key_exists($name, $this->attributes)) {
217+
return $this;
218+
}
219+
220+
$new = clone $this;
221+
unset($new->attributes[$name]);
222+
return $new;
223+
}
224+
225+
/**
226+
* @param array $uploadedFiles
227+
* @throws InvalidArgumentException
228+
*/
229+
private function validateUploadedFiles(array $uploadedFiles): void
230+
{
231+
foreach ($uploadedFiles as $file) {
232+
if (is_array($file)) {
233+
$this->validateUploadedFiles($file);
234+
continue;
235+
}
236+
237+
if (!$file instanceof UploadedFileInterface) {
238+
throw new InvalidArgumentException(sprintf(
239+
'Invalid item in uploaded files structure.'
240+
. '`%s` is not an instance of `Psr\Http\Message\UploadedFileInterface`.',
241+
(is_object($file) ? get_class($file) : gettype($file))
242+
));
243+
}
244+
}
245+
}
246+
}

0 commit comments

Comments
 (0)