Passed
Push — master ( c01d71...667403 )
by Anatoly
04:35
created
src/Message.php 1 patch
Indentation   +292 added lines, -292 removed lines patch added patch discarded remove patch
@@ -24,296 +24,296 @@
 block discarded – undo
24 24
 
25 25
 abstract class Message implements MessageInterface
26 26
 {
27
-    /**
28
-     * @deprecated 3.2.0
29
-     */
30
-    public const ALLOWED_HTTP_VERSIONS = ['1.0', '1.1', '2.0', '2'];
31
-
32
-    public const HTTP_VERSION_REGEX = '/^[0-9](?:[.][0-9])?$/';
33
-    public const DEFAULT_HTTP_VERSION = '1.1';
34
-
35
-    private string $protocolVersion = self::DEFAULT_HTTP_VERSION;
36
-
37
-    /**
38
-     * @var array<string, list<string>>
39
-     */
40
-    private array $headers = [];
41
-
42
-    /**
43
-     * @var array<string, string>
44
-     */
45
-    private array $headerNames = [];
46
-
47
-    private ?StreamInterface $body = null;
48
-
49
-    /**
50
-     * @inheritDoc
51
-     */
52
-    public function getProtocolVersion(): string
53
-    {
54
-        return $this->protocolVersion;
55
-    }
56
-
57
-    /**
58
-     * @inheritDoc
59
-     *
60
-     * @throws InvalidArgumentException
61
-     */
62
-    public function withProtocolVersion($version): MessageInterface
63
-    {
64
-        $clone = clone $this;
65
-        $clone->setProtocolVersion($version);
66
-
67
-        return $clone;
68
-    }
69
-
70
-    /**
71
-     * @inheritDoc
72
-     */
73
-    public function getHeaders(): array
74
-    {
75
-        return $this->headers;
76
-    }
77
-
78
-    /**
79
-     * @inheritDoc
80
-     */
81
-    public function hasHeader($name): bool
82
-    {
83
-        $key = strtolower($name);
84
-
85
-        return isset($this->headerNames[$key]);
86
-    }
87
-
88
-    /**
89
-     * @inheritDoc
90
-     */
91
-    public function getHeader($name): array
92
-    {
93
-        $key = strtolower($name);
94
-
95
-        if (!isset($this->headerNames[$key])) {
96
-            return [];
97
-        }
98
-
99
-        return $this->headers[$this->headerNames[$key]];
100
-    }
101
-
102
-    /**
103
-     * @inheritDoc
104
-     */
105
-    public function getHeaderLine($name): string
106
-    {
107
-        $key = strtolower($name);
108
-
109
-        if (!isset($this->headerNames[$key])) {
110
-            return '';
111
-        }
112
-
113
-        return implode(',', $this->headers[$this->headerNames[$key]]);
114
-    }
115
-
116
-    /**
117
-     * @inheritDoc
118
-     */
119
-    public function withHeader($name, $value): MessageInterface
120
-    {
121
-        $clone = clone $this;
122
-        $clone->setHeader($name, $value, true);
123
-
124
-        return $clone;
125
-    }
126
-
127
-    /**
128
-     * @inheritDoc
129
-     */
130
-    public function withAddedHeader($name, $value): MessageInterface
131
-    {
132
-        $clone = clone $this;
133
-        $clone->setHeader($name, $value, false);
134
-
135
-        return $clone;
136
-    }
137
-
138
-    /**
139
-     * @inheritDoc
140
-     */
141
-    public function withoutHeader($name): MessageInterface
142
-    {
143
-        $clone = clone $this;
144
-        $clone->deleteHeader($name);
145
-
146
-        return $clone;
147
-    }
148
-
149
-    /**
150
-     * @inheritDoc
151
-     */
152
-    public function getBody(): StreamInterface
153
-    {
154
-        return $this->body ??= new PhpTempStream();
155
-    }
156
-
157
-    /**
158
-     * @inheritDoc
159
-     */
160
-    public function withBody(StreamInterface $body): MessageInterface
161
-    {
162
-        $clone = clone $this;
163
-        $clone->setBody($body);
164
-
165
-        return $clone;
166
-    }
167
-
168
-    /**
169
-     * Sets the given HTTP version to the message
170
-     *
171
-     * @param string $protocolVersion
172
-     *
173
-     * @throws InvalidArgumentException
174
-     */
175
-    final protected function setProtocolVersion($protocolVersion): void
176
-    {
177
-        $this->validateProtocolVersion($protocolVersion);
178
-
179
-        $this->protocolVersion = $protocolVersion;
180
-    }
181
-
182
-    /**
183
-     * Sets a new header to the message with the given name and value(s)
184
-     *
185
-     * @param string $name
186
-     * @param string|string[] $value
187
-     *
188
-     * @throws InvalidArgumentException
189
-     */
190
-    final protected function setHeader($name, $value, bool $replace = true): void
191
-    {
192
-        if (!is_array($value)) {
193
-            $value = [$value];
194
-        }
195
-
196
-        $this->validateHeaderName($name);
197
-        $this->validateHeaderValue($name, $value);
198
-
199
-        $replace and $this->deleteHeader($name);
200
-
201
-        $key = strtolower($name);
202
-
203
-        $this->headerNames[$key] ??= $name;
204
-        $this->headers[$this->headerNames[$key]] ??= [];
205
-
206
-        foreach ($value as $item) {
207
-            $this->headers[$this->headerNames[$key]][] = $item;
208
-        }
209
-    }
210
-
211
-    /**
212
-     * Sets the given headers to the message
213
-     *
214
-     * @param array<string, string|string[]> $headers
215
-     *
216
-     * @throws InvalidArgumentException
217
-     */
218
-    final protected function setHeaders(array $headers): void
219
-    {
220
-        foreach ($headers as $name => $value) {
221
-            $this->setHeader($name, $value, false);
222
-        }
223
-    }
224
-
225
-    /**
226
-     * Deletes a header from the message by the given name
227
-     *
228
-     * @param string $name
229
-     */
230
-    final protected function deleteHeader($name): void
231
-    {
232
-        $key = strtolower($name);
233
-
234
-        if (isset($this->headerNames[$key])) {
235
-            unset($this->headers[$this->headerNames[$key]]);
236
-            unset($this->headerNames[$key]);
237
-        }
238
-    }
239
-
240
-    /**
241
-     * Sets the given body to the message
242
-     */
243
-    final protected function setBody(StreamInterface $body): void
244
-    {
245
-        $this->body = $body;
246
-    }
247
-
248
-    /**
249
-     * Validates the given HTTP version
250
-     *
251
-     * @param mixed $protocolVersion
252
-     *
253
-     * @throws InvalidArgumentException
254
-     */
255
-    private function validateProtocolVersion($protocolVersion): void
256
-    {
257
-        if ($protocolVersion === '') {
258
-            throw new InvalidArgumentException('HTTP version cannot be an empty');
259
-        }
260
-
261
-        if (!is_string($protocolVersion)) {
262
-            throw new InvalidArgumentException('HTTP version must be a string');
263
-        }
264
-
265
-        if (!preg_match(self::HTTP_VERSION_REGEX, $protocolVersion)) {
266
-            throw new InvalidArgumentException('HTTP version is invalid');
267
-        }
268
-    }
269
-
270
-    /**
271
-     * Validates the given header name
272
-     *
273
-     * @param mixed $name
274
-     *
275
-     * @throws InvalidArgumentException
276
-     */
277
-    private function validateHeaderName($name): void
278
-    {
279
-        if ($name === '') {
280
-            throw new InvalidArgumentException('HTTP header name cannot be an empty');
281
-        }
282
-
283
-        if (!is_string($name)) {
284
-            throw new InvalidArgumentException('HTTP header name must be a string');
285
-        }
286
-
287
-        if (!preg_match(HeaderInterface::RFC7230_TOKEN_REGEX, $name)) {
288
-            throw new InvalidArgumentException('HTTP header name is invalid');
289
-        }
290
-    }
291
-
292
-    /**
293
-     * Validates the given header value
294
-     *
295
-     * @param array<array-key, mixed> $value
296
-     *
297
-     * @throws InvalidArgumentException
298
-     */
299
-    private function validateHeaderValue(string $name, array $value): void
300
-    {
301
-        if ($value === []) {
302
-            throw new InvalidArgumentException("The value of the HTTP header $name cannot be an empty array");
303
-        }
304
-
305
-        foreach ($value as $key => $item) {
306
-            if ($item === '') {
307
-                continue;
308
-            }
309
-
310
-            if (!is_string($item)) {
311
-                throw new InvalidArgumentException("The value of the HTTP header $name:$key must be a string");
312
-            }
313
-
314
-            if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE_REGEX, $item)) {
315
-                throw new InvalidArgumentException("The value of the HTTP header $name:$key is invalid");
316
-            }
317
-        }
318
-    }
27
+	/**
28
+	 * @deprecated 3.2.0
29
+	 */
30
+	public const ALLOWED_HTTP_VERSIONS = ['1.0', '1.1', '2.0', '2'];
31
+
32
+	public const HTTP_VERSION_REGEX = '/^[0-9](?:[.][0-9])?$/';
33
+	public const DEFAULT_HTTP_VERSION = '1.1';
34
+
35
+	private string $protocolVersion = self::DEFAULT_HTTP_VERSION;
36
+
37
+	/**
38
+	 * @var array<string, list<string>>
39
+	 */
40
+	private array $headers = [];
41
+
42
+	/**
43
+	 * @var array<string, string>
44
+	 */
45
+	private array $headerNames = [];
46
+
47
+	private ?StreamInterface $body = null;
48
+
49
+	/**
50
+	 * @inheritDoc
51
+	 */
52
+	public function getProtocolVersion(): string
53
+	{
54
+		return $this->protocolVersion;
55
+	}
56
+
57
+	/**
58
+	 * @inheritDoc
59
+	 *
60
+	 * @throws InvalidArgumentException
61
+	 */
62
+	public function withProtocolVersion($version): MessageInterface
63
+	{
64
+		$clone = clone $this;
65
+		$clone->setProtocolVersion($version);
66
+
67
+		return $clone;
68
+	}
69
+
70
+	/**
71
+	 * @inheritDoc
72
+	 */
73
+	public function getHeaders(): array
74
+	{
75
+		return $this->headers;
76
+	}
77
+
78
+	/**
79
+	 * @inheritDoc
80
+	 */
81
+	public function hasHeader($name): bool
82
+	{
83
+		$key = strtolower($name);
84
+
85
+		return isset($this->headerNames[$key]);
86
+	}
87
+
88
+	/**
89
+	 * @inheritDoc
90
+	 */
91
+	public function getHeader($name): array
92
+	{
93
+		$key = strtolower($name);
94
+
95
+		if (!isset($this->headerNames[$key])) {
96
+			return [];
97
+		}
98
+
99
+		return $this->headers[$this->headerNames[$key]];
100
+	}
101
+
102
+	/**
103
+	 * @inheritDoc
104
+	 */
105
+	public function getHeaderLine($name): string
106
+	{
107
+		$key = strtolower($name);
108
+
109
+		if (!isset($this->headerNames[$key])) {
110
+			return '';
111
+		}
112
+
113
+		return implode(',', $this->headers[$this->headerNames[$key]]);
114
+	}
115
+
116
+	/**
117
+	 * @inheritDoc
118
+	 */
119
+	public function withHeader($name, $value): MessageInterface
120
+	{
121
+		$clone = clone $this;
122
+		$clone->setHeader($name, $value, true);
123
+
124
+		return $clone;
125
+	}
126
+
127
+	/**
128
+	 * @inheritDoc
129
+	 */
130
+	public function withAddedHeader($name, $value): MessageInterface
131
+	{
132
+		$clone = clone $this;
133
+		$clone->setHeader($name, $value, false);
134
+
135
+		return $clone;
136
+	}
137
+
138
+	/**
139
+	 * @inheritDoc
140
+	 */
141
+	public function withoutHeader($name): MessageInterface
142
+	{
143
+		$clone = clone $this;
144
+		$clone->deleteHeader($name);
145
+
146
+		return $clone;
147
+	}
148
+
149
+	/**
150
+	 * @inheritDoc
151
+	 */
152
+	public function getBody(): StreamInterface
153
+	{
154
+		return $this->body ??= new PhpTempStream();
155
+	}
156
+
157
+	/**
158
+	 * @inheritDoc
159
+	 */
160
+	public function withBody(StreamInterface $body): MessageInterface
161
+	{
162
+		$clone = clone $this;
163
+		$clone->setBody($body);
164
+
165
+		return $clone;
166
+	}
167
+
168
+	/**
169
+	 * Sets the given HTTP version to the message
170
+	 *
171
+	 * @param string $protocolVersion
172
+	 *
173
+	 * @throws InvalidArgumentException
174
+	 */
175
+	final protected function setProtocolVersion($protocolVersion): void
176
+	{
177
+		$this->validateProtocolVersion($protocolVersion);
178
+
179
+		$this->protocolVersion = $protocolVersion;
180
+	}
181
+
182
+	/**
183
+	 * Sets a new header to the message with the given name and value(s)
184
+	 *
185
+	 * @param string $name
186
+	 * @param string|string[] $value
187
+	 *
188
+	 * @throws InvalidArgumentException
189
+	 */
190
+	final protected function setHeader($name, $value, bool $replace = true): void
191
+	{
192
+		if (!is_array($value)) {
193
+			$value = [$value];
194
+		}
195
+
196
+		$this->validateHeaderName($name);
197
+		$this->validateHeaderValue($name, $value);
198
+
199
+		$replace and $this->deleteHeader($name);
200
+
201
+		$key = strtolower($name);
202
+
203
+		$this->headerNames[$key] ??= $name;
204
+		$this->headers[$this->headerNames[$key]] ??= [];
205
+
206
+		foreach ($value as $item) {
207
+			$this->headers[$this->headerNames[$key]][] = $item;
208
+		}
209
+	}
210
+
211
+	/**
212
+	 * Sets the given headers to the message
213
+	 *
214
+	 * @param array<string, string|string[]> $headers
215
+	 *
216
+	 * @throws InvalidArgumentException
217
+	 */
218
+	final protected function setHeaders(array $headers): void
219
+	{
220
+		foreach ($headers as $name => $value) {
221
+			$this->setHeader($name, $value, false);
222
+		}
223
+	}
224
+
225
+	/**
226
+	 * Deletes a header from the message by the given name
227
+	 *
228
+	 * @param string $name
229
+	 */
230
+	final protected function deleteHeader($name): void
231
+	{
232
+		$key = strtolower($name);
233
+
234
+		if (isset($this->headerNames[$key])) {
235
+			unset($this->headers[$this->headerNames[$key]]);
236
+			unset($this->headerNames[$key]);
237
+		}
238
+	}
239
+
240
+	/**
241
+	 * Sets the given body to the message
242
+	 */
243
+	final protected function setBody(StreamInterface $body): void
244
+	{
245
+		$this->body = $body;
246
+	}
247
+
248
+	/**
249
+	 * Validates the given HTTP version
250
+	 *
251
+	 * @param mixed $protocolVersion
252
+	 *
253
+	 * @throws InvalidArgumentException
254
+	 */
255
+	private function validateProtocolVersion($protocolVersion): void
256
+	{
257
+		if ($protocolVersion === '') {
258
+			throw new InvalidArgumentException('HTTP version cannot be an empty');
259
+		}
260
+
261
+		if (!is_string($protocolVersion)) {
262
+			throw new InvalidArgumentException('HTTP version must be a string');
263
+		}
264
+
265
+		if (!preg_match(self::HTTP_VERSION_REGEX, $protocolVersion)) {
266
+			throw new InvalidArgumentException('HTTP version is invalid');
267
+		}
268
+	}
269
+
270
+	/**
271
+	 * Validates the given header name
272
+	 *
273
+	 * @param mixed $name
274
+	 *
275
+	 * @throws InvalidArgumentException
276
+	 */
277
+	private function validateHeaderName($name): void
278
+	{
279
+		if ($name === '') {
280
+			throw new InvalidArgumentException('HTTP header name cannot be an empty');
281
+		}
282
+
283
+		if (!is_string($name)) {
284
+			throw new InvalidArgumentException('HTTP header name must be a string');
285
+		}
286
+
287
+		if (!preg_match(HeaderInterface::RFC7230_TOKEN_REGEX, $name)) {
288
+			throw new InvalidArgumentException('HTTP header name is invalid');
289
+		}
290
+	}
291
+
292
+	/**
293
+	 * Validates the given header value
294
+	 *
295
+	 * @param array<array-key, mixed> $value
296
+	 *
297
+	 * @throws InvalidArgumentException
298
+	 */
299
+	private function validateHeaderValue(string $name, array $value): void
300
+	{
301
+		if ($value === []) {
302
+			throw new InvalidArgumentException("The value of the HTTP header $name cannot be an empty array");
303
+		}
304
+
305
+		foreach ($value as $key => $item) {
306
+			if ($item === '') {
307
+				continue;
308
+			}
309
+
310
+			if (!is_string($item)) {
311
+				throw new InvalidArgumentException("The value of the HTTP header $name:$key must be a string");
312
+			}
313
+
314
+			if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE_REGEX, $item)) {
315
+				throw new InvalidArgumentException("The value of the HTTP header $name:$key is invalid");
316
+			}
317
+		}
318
+	}
319 319
 }
Please login to merge, or discard this patch.