ServerRequest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 140
rs 10
c 1
b 0
f 0
wmc 19

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getParsedBody() 0 2 1
A withoutAttribute() 0 10 2
A getAttribute() 0 7 2
A getCookieParams() 0 2 1
A withQueryParams() 0 5 1
A withCookieParams() 0 5 1
A getUploadedFiles() 0 2 1
A withParsedBody() 0 10 4
A getQueryParams() 0 2 1
A withAttribute() 0 5 1
A getServerParams() 0 2 1
A __construct() 0 4 1
A getAttributes() 0 2 1
A withUploadedFiles() 0 5 1
1
<?php
2
/**
3
 * Class ServerRequest
4
 *
5
 * @created      11.08.2018
6
 * @author       smiley <[email protected]>
7
 * @copyright    2018 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\HTTP\Psr7;
12
13
use InvalidArgumentException;
14
use Psr\Http\Message\{ServerRequestInterface, UriInterface};
15
16
use function array_key_exists, is_array, is_object;
17
18
class ServerRequest extends Request implements ServerRequestInterface{
19
20
	protected array             $serverParams;
21
	protected array             $cookieParams  = [];
22
	protected array             $queryParams   = [];
23
	protected array             $attributes    = [];
24
	protected array             $uploadedFiles = [];
25
	protected array|object|null $parsedBody    = null;
26
27
	/**
28
	 * ServerRequest constructor.
29
	 */
30
	public function __construct(string $method, UriInterface|string $uri, array $serverParams = null){
31
		parent::__construct($method, $uri);
32
33
		$this->serverParams = $serverParams ?? [];
34
	}
35
36
	/**
37
	 * @inheritDoc
38
	 */
39
	public function getServerParams():array{
40
		return $this->serverParams;
41
	}
42
43
	/**
44
	 * @inheritDoc
45
	 */
46
	public function getCookieParams():array{
47
		return $this->cookieParams;
48
	}
49
50
	/**
51
	 * @inheritDoc
52
	 */
53
	public function withCookieParams(array $cookies):ServerRequestInterface{
54
		$clone               = clone $this;
55
		$clone->cookieParams = $cookies;
56
57
		return $clone;
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	public function getQueryParams():array{
64
		return $this->queryParams;
65
	}
66
67
	/**
68
	 * @inheritDoc
69
	 */
70
	public function withQueryParams(array $query):ServerRequestInterface{
71
		$clone              = clone $this;
72
		$clone->queryParams = $query;
73
74
		return $clone;
75
	}
76
77
	/**
78
	 * @inheritDoc
79
	 */
80
	public function getUploadedFiles():array{
81
		return $this->uploadedFiles;
82
	}
83
84
	/**
85
	 * @inheritDoc
86
	 */
87
	public function withUploadedFiles(array $uploadedFiles):ServerRequestInterface{
88
		$clone                = clone $this;
89
		$clone->uploadedFiles = $uploadedFiles;
90
91
		return $clone;
92
	}
93
94
	/**
95
	 * @inheritDoc
96
	 */
97
	public function getParsedBody():array|object|null{
98
		return $this->parsedBody;
99
	}
100
101
	/**
102
	 * @inheritDoc
103
	 */
104
	public function withParsedBody($data):ServerRequestInterface{
105
106
		if($data !== null && !is_object($data) && !is_array($data)){
107
			throw new InvalidArgumentException('parsed body value must be an array, object or null');
108
		}
109
110
		$clone             = clone $this;
111
		$clone->parsedBody = $data;
112
113
		return $clone;
114
	}
115
116
	/**
117
	 * @inheritDoc
118
	 */
119
	public function getAttributes():array{
120
		return $this->attributes;
121
	}
122
123
	/**
124
	 * @inheritDoc
125
	 */
126
	public function getAttribute($name, $default = null):mixed{
127
128
		if(!array_key_exists($name, $this->attributes)){
129
			return $default;
130
		}
131
132
		return $this->attributes[$name];
133
	}
134
135
	/**
136
	 * @inheritDoc
137
	 */
138
	public function withAttribute($name, $value):ServerRequestInterface{
139
		$clone                    = clone $this;
140
		$clone->attributes[$name] = $value;
141
142
		return $clone;
143
	}
144
145
	/**
146
	 * @inheritDoc
147
	 */
148
	public function withoutAttribute($name):ServerRequestInterface{
149
150
		if(!array_key_exists($name, $this->attributes)){
151
			return $this;
152
		}
153
154
		$clone = clone $this;
155
		unset($clone->attributes[$name]);
156
157
		return $clone;
158
	}
159
160
}
161