1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Furious\Psr7; |
6
|
|
|
|
7
|
|
|
use Furious\Psr7\Exception\InvalidArgumentException; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Psr\Http\Message\StreamInterface; |
10
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
11
|
|
|
use function array_key_exists; |
12
|
|
|
use function is_array; |
13
|
|
|
|
14
|
|
|
class ServerRequest extends Request implements ServerRequestInterface |
15
|
|
|
{ |
16
|
|
|
private array $queryParams = []; |
|
|
|
|
17
|
|
|
private array $cookieParams = []; |
18
|
|
|
/** @var array|UploadedFileInterface[] */ |
19
|
|
|
private array $uploadedFiles = []; |
20
|
|
|
private array $attributes = []; |
21
|
|
|
private array $serverParams; |
22
|
|
|
/** @var array|object|null */ |
23
|
|
|
private $parsedBody; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
string $method, $uri, string $version = '1.1', array $headers = [], |
27
|
|
|
array $queryParams = [], $body = null, |
28
|
|
|
array $serverParams = [], array $cookieParams = [], |
29
|
|
|
array $files = [], array $attributes = [] |
30
|
|
|
) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($method, $uri, $headers, $body, $version); |
33
|
|
|
if (is_string($body)) { |
34
|
|
|
$body = Stream::new($body); |
35
|
|
|
} |
36
|
|
|
$this->queryParams = $queryParams; |
37
|
|
|
$this->validateBody($body); |
38
|
|
|
$this->parsedBody = $body; |
39
|
|
|
$this->serverParams = $serverParams; |
40
|
|
|
$this->cookieParams = $cookieParams; |
41
|
|
|
$this->uploadedFiles = $files; |
42
|
|
|
$this->attributes = $attributes; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Get |
46
|
|
|
|
47
|
|
|
public function getServerParams(): array |
48
|
|
|
{ |
49
|
|
|
return $this->serverParams; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getUploadedFiles(): array |
53
|
|
|
{ |
54
|
|
|
return $this->uploadedFiles; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getCookieParams(): array |
58
|
|
|
{ |
59
|
|
|
return $this->cookieParams; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getQueryParams(): array |
63
|
|
|
{ |
64
|
|
|
return $this->queryParams; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getAttributes(): array |
68
|
|
|
{ |
69
|
|
|
return $this->attributes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getParsedBody() |
73
|
|
|
{ |
74
|
|
|
return $this->parsedBody; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getAttribute($attribute, $default = null) |
78
|
|
|
{ |
79
|
|
|
if (!array_key_exists($attribute, $this->attributes)) { |
80
|
|
|
return $default; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->attributes[$attribute]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// With |
87
|
|
|
|
88
|
|
|
public function withUploadedFiles(array $uploadedFiles) |
89
|
|
|
{ |
90
|
|
|
$serverRequest = clone $this; |
91
|
|
|
$serverRequest->uploadedFiles = $uploadedFiles; |
92
|
|
|
return $serverRequest; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function withCookieParams(array $cookies) |
96
|
|
|
{ |
97
|
|
|
$serverRequest = clone $this; |
98
|
|
|
$serverRequest->cookieParams = $cookies; |
99
|
|
|
return $serverRequest; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function withQueryParams(array $query) |
103
|
|
|
{ |
104
|
|
|
$serverRequest = clone $this; |
105
|
|
|
$serverRequest->queryParams = $query; |
106
|
|
|
return $serverRequest; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function withAttribute($attribute, $value): self |
110
|
|
|
{ |
111
|
|
|
$serverRequest = clone $this; |
112
|
|
|
$serverRequest->attributes[$attribute] = $value; |
113
|
|
|
return $serverRequest; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function withoutAttribute($attribute): self |
117
|
|
|
{ |
118
|
|
|
if (!array_key_exists($attribute, $this->attributes)) { |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$serverRequest = clone $this; |
123
|
|
|
unset($serverRequest->attributes[$attribute]); |
124
|
|
|
return $serverRequest; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function withParsedBody($data) |
128
|
|
|
{ |
129
|
|
|
if (is_string($data)) { |
130
|
|
|
$data = Stream::new($data); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->validateBody($data); |
134
|
|
|
|
135
|
|
|
$serverRequest = clone $this; |
136
|
|
|
$serverRequest->parsedBody = $data; |
137
|
|
|
return $serverRequest; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function validateBody($body): void |
141
|
|
|
{ |
142
|
|
|
if (null !== $body and !is_array($body) and !$body instanceof StreamInterface) { |
143
|
|
|
throw new InvalidArgumentException('Body must be array, instance of StreamInterface or null'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |