Passed
Push — master ( 140918...3dc018 )
by Anatoly
26:40 queued 24:39
created
src/Response.php 2 patches
Indentation   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -42,138 +42,138 @@
 block discarded – undo
42 42
 class Response extends Message implements ResponseInterface, StatusCodeInterface
43 43
 {
44 44
 
45
-    /**
46
-     * The response's status code
47
-     *
48
-     * @var int
49
-     */
50
-    protected $statusCode = self::STATUS_OK;
51
-
52
-    /**
53
-     * The response's reason phrase
54
-     *
55
-     * @var string
56
-     */
57
-    protected $reasonPhrase = REASON_PHRASES[self::STATUS_OK];
58
-
59
-    /**
60
-     * Constrictor of the class
61
-     *
62
-     * @param int|null $statusCode
63
-     * @param string|null $reasonPhrase
64
-     * @param array<string, string|string[]>|null $headers
65
-     * @param StreamInterface|null $body
66
-     * @param string|null $protocolVersion
67
-     */
68
-    public function __construct(
69
-        ?int $statusCode = null,
70
-        ?string $reasonPhrase = null,
71
-        ?array $headers = null,
72
-        ?StreamInterface $body = null,
73
-        ?string $protocolVersion = null
74
-    ) {
75
-        parent::__construct(
76
-            $headers,
77
-            $body,
78
-            $protocolVersion
79
-        );
80
-
81
-        if (isset($statusCode)) {
82
-            $this->setStatus($statusCode, $reasonPhrase ?? '');
83
-        }
84
-    }
85
-
86
-    /**
87
-     * {@inheritdoc}
88
-     */
89
-    public function getStatusCode() : int
90
-    {
91
-        return $this->statusCode;
92
-    }
93
-
94
-    /**
95
-     * {@inheritdoc}
96
-     */
97
-    public function getReasonPhrase() : string
98
-    {
99
-        return $this->reasonPhrase;
100
-    }
101
-
102
-    /**
103
-     * {@inheritdoc}
104
-     *
105
-     * @psalm-suppress ParamNameMismatch
106
-     */
107
-    public function withStatus($statusCode, $reasonPhrase = '') : ResponseInterface
108
-    {
109
-        $clone = clone $this;
110
-        $clone->setStatus($statusCode, $reasonPhrase);
111
-
112
-        return $clone;
113
-    }
114
-
115
-    /**
116
-     * Sets the given status to the response
117
-     *
118
-     * @param int $statusCode
119
-     * @param string $reasonPhrase
120
-     *
121
-     * @return void
122
-     */
123
-    protected function setStatus($statusCode, $reasonPhrase) : void
124
-    {
125
-        $this->validateStatusCode($statusCode);
126
-        $this->validateReasonPhrase($reasonPhrase);
127
-
128
-        if ('' === $reasonPhrase) {
129
-            $reasonPhrase = REASON_PHRASES[$statusCode] ?? 'Unknown Status Code';
130
-        }
131
-
132
-        $this->statusCode = $statusCode;
133
-        $this->reasonPhrase = $reasonPhrase;
134
-    }
135
-
136
-    /**
137
-     * Validates the given status-code
138
-     *
139
-     * @param mixed $statusCode
140
-     *
141
-     * @return void
142
-     *
143
-     * @throws InvalidArgumentException
144
-     *
145
-     * @link https://tools.ietf.org/html/rfc7230#section-3.1.2
146
-     */
147
-    protected function validateStatusCode($statusCode) : void
148
-    {
149
-        if (!is_int($statusCode)) {
150
-            throw new InvalidArgumentException('HTTP status-code must be an integer');
151
-        }
152
-
153
-        if (! ($statusCode >= 100 && $statusCode <= 599)) {
154
-            throw new InvalidArgumentException(sprintf('HTTP status-code "%d" is not valid', $statusCode));
155
-        }
156
-    }
157
-
158
-    /**
159
-     * Validates the given reason-phrase
160
-     *
161
-     * @param mixed $reasonPhrase
162
-     *
163
-     * @return void
164
-     *
165
-     * @throws InvalidArgumentException
166
-     *
167
-     * @link https://tools.ietf.org/html/rfc7230#section-3.1.2
168
-     */
169
-    protected function validateReasonPhrase($reasonPhrase) : void
170
-    {
171
-        if (!is_string($reasonPhrase)) {
172
-            throw new InvalidArgumentException('HTTP reason-phrase must be a string');
173
-        }
174
-
175
-        if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $reasonPhrase)) {
176
-            throw new InvalidArgumentException(sprintf('HTTP reason-phrase "%s" is not valid', $reasonPhrase));
177
-        }
178
-    }
45
+	/**
46
+	 * The response's status code
47
+	 *
48
+	 * @var int
49
+	 */
50
+	protected $statusCode = self::STATUS_OK;
51
+
52
+	/**
53
+	 * The response's reason phrase
54
+	 *
55
+	 * @var string
56
+	 */
57
+	protected $reasonPhrase = REASON_PHRASES[self::STATUS_OK];
58
+
59
+	/**
60
+	 * Constrictor of the class
61
+	 *
62
+	 * @param int|null $statusCode
63
+	 * @param string|null $reasonPhrase
64
+	 * @param array<string, string|string[]>|null $headers
65
+	 * @param StreamInterface|null $body
66
+	 * @param string|null $protocolVersion
67
+	 */
68
+	public function __construct(
69
+		?int $statusCode = null,
70
+		?string $reasonPhrase = null,
71
+		?array $headers = null,
72
+		?StreamInterface $body = null,
73
+		?string $protocolVersion = null
74
+	) {
75
+		parent::__construct(
76
+			$headers,
77
+			$body,
78
+			$protocolVersion
79
+		);
80
+
81
+		if (isset($statusCode)) {
82
+			$this->setStatus($statusCode, $reasonPhrase ?? '');
83
+		}
84
+	}
85
+
86
+	/**
87
+	 * {@inheritdoc}
88
+	 */
89
+	public function getStatusCode() : int
90
+	{
91
+		return $this->statusCode;
92
+	}
93
+
94
+	/**
95
+	 * {@inheritdoc}
96
+	 */
97
+	public function getReasonPhrase() : string
98
+	{
99
+		return $this->reasonPhrase;
100
+	}
101
+
102
+	/**
103
+	 * {@inheritdoc}
104
+	 *
105
+	 * @psalm-suppress ParamNameMismatch
106
+	 */
107
+	public function withStatus($statusCode, $reasonPhrase = '') : ResponseInterface
108
+	{
109
+		$clone = clone $this;
110
+		$clone->setStatus($statusCode, $reasonPhrase);
111
+
112
+		return $clone;
113
+	}
114
+
115
+	/**
116
+	 * Sets the given status to the response
117
+	 *
118
+	 * @param int $statusCode
119
+	 * @param string $reasonPhrase
120
+	 *
121
+	 * @return void
122
+	 */
123
+	protected function setStatus($statusCode, $reasonPhrase) : void
124
+	{
125
+		$this->validateStatusCode($statusCode);
126
+		$this->validateReasonPhrase($reasonPhrase);
127
+
128
+		if ('' === $reasonPhrase) {
129
+			$reasonPhrase = REASON_PHRASES[$statusCode] ?? 'Unknown Status Code';
130
+		}
131
+
132
+		$this->statusCode = $statusCode;
133
+		$this->reasonPhrase = $reasonPhrase;
134
+	}
135
+
136
+	/**
137
+	 * Validates the given status-code
138
+	 *
139
+	 * @param mixed $statusCode
140
+	 *
141
+	 * @return void
142
+	 *
143
+	 * @throws InvalidArgumentException
144
+	 *
145
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.1.2
146
+	 */
147
+	protected function validateStatusCode($statusCode) : void
148
+	{
149
+		if (!is_int($statusCode)) {
150
+			throw new InvalidArgumentException('HTTP status-code must be an integer');
151
+		}
152
+
153
+		if (! ($statusCode >= 100 && $statusCode <= 599)) {
154
+			throw new InvalidArgumentException(sprintf('HTTP status-code "%d" is not valid', $statusCode));
155
+		}
156
+	}
157
+
158
+	/**
159
+	 * Validates the given reason-phrase
160
+	 *
161
+	 * @param mixed $reasonPhrase
162
+	 *
163
+	 * @return void
164
+	 *
165
+	 * @throws InvalidArgumentException
166
+	 *
167
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.1.2
168
+	 */
169
+	protected function validateReasonPhrase($reasonPhrase) : void
170
+	{
171
+		if (!is_string($reasonPhrase)) {
172
+			throw new InvalidArgumentException('HTTP reason-phrase must be a string');
173
+		}
174
+
175
+		if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $reasonPhrase)) {
176
+			throw new InvalidArgumentException(sprintf('HTTP reason-phrase "%s" is not valid', $reasonPhrase));
177
+		}
178
+	}
179 179
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -150,7 +150,7 @@
 block discarded – undo
150 150
             throw new InvalidArgumentException('HTTP status-code must be an integer');
151 151
         }
152 152
 
153
-        if (! ($statusCode >= 100 && $statusCode <= 599)) {
153
+        if (!($statusCode >= 100 && $statusCode <= 599)) {
154 154
             throw new InvalidArgumentException(sprintf('HTTP status-code "%d" is not valid', $statusCode));
155 155
         }
156 156
     }
Please login to merge, or discard this patch.
src/RequestFactory.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -39,46 +39,46 @@
 block discarded – undo
39 39
 class RequestFactory implements RequestFactoryInterface
40 40
 {
41 41
 
42
-    /**
43
-     * {@inheritdoc}
44
-     */
45
-    public function createRequest(string $method, $uri) : RequestInterface
46
-    {
47
-        return new Request($method, $uri);
48
-    }
42
+	/**
43
+	 * {@inheritdoc}
44
+	 */
45
+	public function createRequest(string $method, $uri) : RequestInterface
46
+	{
47
+		return new Request($method, $uri);
48
+	}
49 49
 
50
-    /**
51
-     * Creates a JSON request
52
-     *
53
-     * @param string $method
54
-     * @param string|UriInterface|null $uri
55
-     * @param mixed $data
56
-     * @param int $flags
57
-     * @param int $depth
58
-     *
59
-     * @return RequestInterface
60
-     *
61
-     * @throws InvalidArgumentException
62
-     *         If the data cannot be encoded.
63
-     */
64
-    public function createJsonRequest(string $method, $uri, $data, int $flags = 0, int $depth = 512) : RequestInterface
65
-    {
66
-        /**
67
-         * @psalm-suppress UnusedFunctionCall
68
-         */
69
-        json_encode('');
50
+	/**
51
+	 * Creates a JSON request
52
+	 *
53
+	 * @param string $method
54
+	 * @param string|UriInterface|null $uri
55
+	 * @param mixed $data
56
+	 * @param int $flags
57
+	 * @param int $depth
58
+	 *
59
+	 * @return RequestInterface
60
+	 *
61
+	 * @throws InvalidArgumentException
62
+	 *         If the data cannot be encoded.
63
+	 */
64
+	public function createJsonRequest(string $method, $uri, $data, int $flags = 0, int $depth = 512) : RequestInterface
65
+	{
66
+		/**
67
+		 * @psalm-suppress UnusedFunctionCall
68
+		 */
69
+		json_encode('');
70 70
 
71
-        $json = json_encode($data, $flags, $depth);
72
-        if (JSON_ERROR_NONE <> json_last_error()) {
73
-            throw new InvalidArgumentException(json_last_error_msg());
74
-        }
71
+		$json = json_encode($data, $flags, $depth);
72
+		if (JSON_ERROR_NONE <> json_last_error()) {
73
+			throw new InvalidArgumentException(json_last_error_msg());
74
+		}
75 75
 
76
-        $request = new Request($method, $uri, [
77
-            'Content-Type' => 'application/json; charset=UTF-8',
78
-        ]);
76
+		$request = new Request($method, $uri, [
77
+			'Content-Type' => 'application/json; charset=UTF-8',
78
+		]);
79 79
 
80
-        $request->getBody()->write($json);
80
+		$request->getBody()->write($json);
81 81
 
82
-        return $request;
83
-    }
82
+		return $request;
83
+	}
84 84
 }
Please login to merge, or discard this patch.
src/ResponseFactory.php 1 patch
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -40,68 +40,68 @@
 block discarded – undo
40 40
 class ResponseFactory implements ResponseFactoryInterface
41 41
 {
42 42
 
43
-    /**
44
-     * {@inheritdoc}
45
-     *
46
-     * @psalm-suppress ParamNameMismatch
47
-     */
48
-    public function createResponse(int $statusCode = 200, string $reasonPhrase = '') : ResponseInterface
49
-    {
50
-        return new Response($statusCode, $reasonPhrase);
51
-    }
52
-
53
-    /**
54
-     * Creates a HTML response
55
-     *
56
-     * @param int $statusCode
57
-     * @param string|StreamInterface|Stringable $html
58
-     *
59
-     * @return ResponseInterface
60
-     */
61
-    public function createHtmlResponse(int $statusCode, $html) : ResponseInterface
62
-    {
63
-        $html = (string) $html;
64
-
65
-        $response = new Response($statusCode, null, [
66
-            'Content-Type' => 'text/html; charset=UTF-8',
67
-        ]);
68
-
69
-        $response->getBody()->write($html);
70
-
71
-        return $response;
72
-    }
73
-
74
-    /**
75
-     * Creates a JSON response
76
-     *
77
-     * @param int $statusCode
78
-     * @param mixed $data
79
-     * @param int $flags
80
-     * @param int $depth
81
-     *
82
-     * @return ResponseInterface
83
-     *
84
-     * @throws InvalidArgumentException
85
-     *         If the data cannot be encoded.
86
-     */
87
-    public function createJsonResponse(int $statusCode, $data, int $flags = 0, int $depth = 512) : ResponseInterface
88
-    {
89
-        /**
90
-         * @psalm-suppress UnusedFunctionCall
91
-         */
92
-        json_encode('');
93
-
94
-        $json = json_encode($data, $flags, $depth);
95
-        if (JSON_ERROR_NONE <> json_last_error()) {
96
-            throw new InvalidArgumentException(json_last_error_msg());
97
-        }
98
-
99
-        $response = new Response($statusCode, null, [
100
-            'Content-Type' => 'application/json; charset=UTF-8',
101
-        ]);
102
-
103
-        $response->getBody()->write($json);
104
-
105
-        return $response;
106
-    }
43
+	/**
44
+	 * {@inheritdoc}
45
+	 *
46
+	 * @psalm-suppress ParamNameMismatch
47
+	 */
48
+	public function createResponse(int $statusCode = 200, string $reasonPhrase = '') : ResponseInterface
49
+	{
50
+		return new Response($statusCode, $reasonPhrase);
51
+	}
52
+
53
+	/**
54
+	 * Creates a HTML response
55
+	 *
56
+	 * @param int $statusCode
57
+	 * @param string|StreamInterface|Stringable $html
58
+	 *
59
+	 * @return ResponseInterface
60
+	 */
61
+	public function createHtmlResponse(int $statusCode, $html) : ResponseInterface
62
+	{
63
+		$html = (string) $html;
64
+
65
+		$response = new Response($statusCode, null, [
66
+			'Content-Type' => 'text/html; charset=UTF-8',
67
+		]);
68
+
69
+		$response->getBody()->write($html);
70
+
71
+		return $response;
72
+	}
73
+
74
+	/**
75
+	 * Creates a JSON response
76
+	 *
77
+	 * @param int $statusCode
78
+	 * @param mixed $data
79
+	 * @param int $flags
80
+	 * @param int $depth
81
+	 *
82
+	 * @return ResponseInterface
83
+	 *
84
+	 * @throws InvalidArgumentException
85
+	 *         If the data cannot be encoded.
86
+	 */
87
+	public function createJsonResponse(int $statusCode, $data, int $flags = 0, int $depth = 512) : ResponseInterface
88
+	{
89
+		/**
90
+		 * @psalm-suppress UnusedFunctionCall
91
+		 */
92
+		json_encode('');
93
+
94
+		$json = json_encode($data, $flags, $depth);
95
+		if (JSON_ERROR_NONE <> json_last_error()) {
96
+			throw new InvalidArgumentException(json_last_error_msg());
97
+		}
98
+
99
+		$response = new Response($statusCode, null, [
100
+			'Content-Type' => 'application/json; charset=UTF-8',
101
+		]);
102
+
103
+		$response->getBody()->write($json);
104
+
105
+		return $response;
106
+	}
107 107
 }
Please login to merge, or discard this patch.
src/Request.php 1 patch
Indentation   +247 added lines, -247 removed lines patch added patch discarded remove patch
@@ -40,251 +40,251 @@
 block discarded – undo
40 40
 class Request extends Message implements RequestInterface, RequestMethodInterface
41 41
 {
42 42
 
43
-    /**
44
-     * The request method (aka verb)
45
-     *
46
-     * @var string
47
-     */
48
-    protected $method = self::METHOD_GET;
49
-
50
-    /**
51
-     * The request target
52
-     *
53
-     * @var string|null
54
-     */
55
-    protected $requestTarget = null;
56
-
57
-    /**
58
-     * The request URI
59
-     *
60
-     * @var UriInterface|null
61
-     */
62
-    protected $uri = null;
63
-
64
-    /**
65
-     * Constructor of the class
66
-     *
67
-     * @param string|null $method
68
-     * @param string|UriInterface|null $uri
69
-     * @param array<string, string|string[]>|null $headers
70
-     * @param StreamInterface|null $body
71
-     * @param string|null $requestTarget
72
-     * @param string|null $protocolVersion
73
-     */
74
-    public function __construct(
75
-        ?string $method = null,
76
-        $uri = null,
77
-        ?array $headers = null,
78
-        ?StreamInterface $body = null,
79
-        ?string $requestTarget = null,
80
-        ?string $protocolVersion = null
81
-    ) {
82
-        parent::__construct(
83
-            $headers,
84
-            $body,
85
-            $protocolVersion
86
-        );
87
-
88
-        if (isset($method)) {
89
-            $this->setMethod($method);
90
-        }
91
-
92
-        if (isset($requestTarget)) {
93
-            $this->setRequestTarget($requestTarget);
94
-        }
95
-
96
-        if (isset($uri)) {
97
-            $this->setUri($uri);
98
-        }
99
-    }
100
-
101
-    /**
102
-     * {@inheritdoc}
103
-     */
104
-    public function getMethod() : string
105
-    {
106
-        return $this->method;
107
-    }
108
-
109
-    /**
110
-     * {@inheritdoc}
111
-     */
112
-    public function withMethod($method) : RequestInterface
113
-    {
114
-        $clone = clone $this;
115
-        $clone->setMethod($method);
116
-
117
-        return $clone;
118
-    }
119
-
120
-    /**
121
-     * {@inheritdoc}
122
-     */
123
-    public function getRequestTarget() : string
124
-    {
125
-        if (isset($this->requestTarget)) {
126
-            return $this->requestTarget;
127
-        }
128
-
129
-        $uri = $this->getUri();
130
-        $path = $uri->getPath();
131
-
132
-        // https://tools.ietf.org/html/rfc7230#section-5.3.1
133
-        // https://tools.ietf.org/html/rfc7230#section-2.7
134
-        //
135
-        // origin-form = absolute-path [ "?" query ]
136
-        // absolute-path = 1*( "/" segment )
137
-        if (0 !== strncmp($path, '/', 1)) {
138
-            return '/';
139
-        }
140
-
141
-        $requestTarget = $path;
142
-
143
-        $query = $uri->getQuery();
144
-        if ('' !== $query) {
145
-            $requestTarget .= '?' . $query;
146
-        }
147
-
148
-        return $requestTarget;
149
-    }
150
-
151
-    /**
152
-     * {@inheritdoc}
153
-     */
154
-    public function withRequestTarget($requestTarget) : RequestInterface
155
-    {
156
-        $clone = clone $this;
157
-        $clone->setRequestTarget($requestTarget);
158
-
159
-        return $clone;
160
-    }
161
-
162
-    /**
163
-     * {@inheritdoc}
164
-     */
165
-    public function getUri() : UriInterface
166
-    {
167
-        if (null === $this->uri) {
168
-            $this->uri = (new UriFactory)->createUri();
169
-        }
170
-
171
-        return $this->uri;
172
-    }
173
-
174
-    /**
175
-     * {@inheritdoc}
176
-     */
177
-    public function withUri(UriInterface $uri, $preserveHost = false) : RequestInterface
178
-    {
179
-        $clone = clone $this;
180
-        $clone->setUri($uri, $preserveHost);
181
-
182
-        return $clone;
183
-    }
184
-
185
-    /**
186
-     * Sets the given method to the request
187
-     *
188
-     * @param string $method
189
-     *
190
-     * @return void
191
-     */
192
-    protected function setMethod($method) : void
193
-    {
194
-        $this->validateMethod($method);
195
-
196
-        $this->method = strtoupper($method);
197
-    }
198
-
199
-    /**
200
-     * Sets the given request-target to the request
201
-     *
202
-     * @param mixed $requestTarget
203
-     *
204
-     * @return void
205
-     */
206
-    protected function setRequestTarget($requestTarget) : void
207
-    {
208
-        $this->validateRequestTarget($requestTarget);
209
-
210
-        /**
211
-         * @var string $requestTarget
212
-         */
213
-
214
-        $this->requestTarget = $requestTarget;
215
-    }
216
-
217
-    /**
218
-     * Sets the given URI to the request
219
-     *
220
-     * @param string|UriInterface $uri
221
-     * @param bool $preserveHost
222
-     *
223
-     * @return void
224
-     */
225
-    protected function setUri($uri, $preserveHost = false) : void
226
-    {
227
-        if (! ($uri instanceof UriInterface)) {
228
-            $uri = (new UriFactory)->createUri($uri);
229
-        }
230
-
231
-        $this->uri = $uri;
232
-
233
-        if ('' === $uri->getHost() || ($preserveHost && $this->hasHeader('Host'))) {
234
-            return;
235
-        }
236
-
237
-        $newHost = $uri->getHost();
238
-
239
-        $port = $uri->getPort();
240
-        if (null !== $port) {
241
-            $newHost .= ':' . $port;
242
-        }
243
-
244
-        $this->addHeader('Host', $newHost);
245
-    }
246
-
247
-    /**
248
-     * Validates the given method
249
-     *
250
-     * @param mixed $method
251
-     *
252
-     * @return void
253
-     *
254
-     * @throws InvalidArgumentException
255
-     *
256
-     * @link https://tools.ietf.org/html/rfc7230#section-3.1.1
257
-     */
258
-    protected function validateMethod($method) : void
259
-    {
260
-        if (!is_string($method)) {
261
-            throw new InvalidArgumentException('HTTP method must be a string');
262
-        }
263
-
264
-        if (!preg_match(HeaderInterface::RFC7230_TOKEN, $method)) {
265
-            throw new InvalidArgumentException(sprintf('HTTP method "%s" is not valid', $method));
266
-        }
267
-    }
268
-
269
-    /**
270
-     * Validates the given request-target
271
-     *
272
-     * @param mixed $requestTarget
273
-     *
274
-     * @return void
275
-     *
276
-     * @throws InvalidArgumentException
277
-     *
278
-     * @link https://tools.ietf.org/html/rfc7230#section-5.3
279
-     */
280
-    protected function validateRequestTarget($requestTarget) : void
281
-    {
282
-        if (!is_string($requestTarget)) {
283
-            throw new InvalidArgumentException('HTTP request-target must be a string');
284
-        }
285
-
286
-        if (!preg_match('/^[\x21-\x7E\x80-\xFF]+$/', $requestTarget)) {
287
-            throw new InvalidArgumentException(sprintf('HTTP request-target "%s" is not valid', $requestTarget));
288
-        }
289
-    }
43
+	/**
44
+	 * The request method (aka verb)
45
+	 *
46
+	 * @var string
47
+	 */
48
+	protected $method = self::METHOD_GET;
49
+
50
+	/**
51
+	 * The request target
52
+	 *
53
+	 * @var string|null
54
+	 */
55
+	protected $requestTarget = null;
56
+
57
+	/**
58
+	 * The request URI
59
+	 *
60
+	 * @var UriInterface|null
61
+	 */
62
+	protected $uri = null;
63
+
64
+	/**
65
+	 * Constructor of the class
66
+	 *
67
+	 * @param string|null $method
68
+	 * @param string|UriInterface|null $uri
69
+	 * @param array<string, string|string[]>|null $headers
70
+	 * @param StreamInterface|null $body
71
+	 * @param string|null $requestTarget
72
+	 * @param string|null $protocolVersion
73
+	 */
74
+	public function __construct(
75
+		?string $method = null,
76
+		$uri = null,
77
+		?array $headers = null,
78
+		?StreamInterface $body = null,
79
+		?string $requestTarget = null,
80
+		?string $protocolVersion = null
81
+	) {
82
+		parent::__construct(
83
+			$headers,
84
+			$body,
85
+			$protocolVersion
86
+		);
87
+
88
+		if (isset($method)) {
89
+			$this->setMethod($method);
90
+		}
91
+
92
+		if (isset($requestTarget)) {
93
+			$this->setRequestTarget($requestTarget);
94
+		}
95
+
96
+		if (isset($uri)) {
97
+			$this->setUri($uri);
98
+		}
99
+	}
100
+
101
+	/**
102
+	 * {@inheritdoc}
103
+	 */
104
+	public function getMethod() : string
105
+	{
106
+		return $this->method;
107
+	}
108
+
109
+	/**
110
+	 * {@inheritdoc}
111
+	 */
112
+	public function withMethod($method) : RequestInterface
113
+	{
114
+		$clone = clone $this;
115
+		$clone->setMethod($method);
116
+
117
+		return $clone;
118
+	}
119
+
120
+	/**
121
+	 * {@inheritdoc}
122
+	 */
123
+	public function getRequestTarget() : string
124
+	{
125
+		if (isset($this->requestTarget)) {
126
+			return $this->requestTarget;
127
+		}
128
+
129
+		$uri = $this->getUri();
130
+		$path = $uri->getPath();
131
+
132
+		// https://tools.ietf.org/html/rfc7230#section-5.3.1
133
+		// https://tools.ietf.org/html/rfc7230#section-2.7
134
+		//
135
+		// origin-form = absolute-path [ "?" query ]
136
+		// absolute-path = 1*( "/" segment )
137
+		if (0 !== strncmp($path, '/', 1)) {
138
+			return '/';
139
+		}
140
+
141
+		$requestTarget = $path;
142
+
143
+		$query = $uri->getQuery();
144
+		if ('' !== $query) {
145
+			$requestTarget .= '?' . $query;
146
+		}
147
+
148
+		return $requestTarget;
149
+	}
150
+
151
+	/**
152
+	 * {@inheritdoc}
153
+	 */
154
+	public function withRequestTarget($requestTarget) : RequestInterface
155
+	{
156
+		$clone = clone $this;
157
+		$clone->setRequestTarget($requestTarget);
158
+
159
+		return $clone;
160
+	}
161
+
162
+	/**
163
+	 * {@inheritdoc}
164
+	 */
165
+	public function getUri() : UriInterface
166
+	{
167
+		if (null === $this->uri) {
168
+			$this->uri = (new UriFactory)->createUri();
169
+		}
170
+
171
+		return $this->uri;
172
+	}
173
+
174
+	/**
175
+	 * {@inheritdoc}
176
+	 */
177
+	public function withUri(UriInterface $uri, $preserveHost = false) : RequestInterface
178
+	{
179
+		$clone = clone $this;
180
+		$clone->setUri($uri, $preserveHost);
181
+
182
+		return $clone;
183
+	}
184
+
185
+	/**
186
+	 * Sets the given method to the request
187
+	 *
188
+	 * @param string $method
189
+	 *
190
+	 * @return void
191
+	 */
192
+	protected function setMethod($method) : void
193
+	{
194
+		$this->validateMethod($method);
195
+
196
+		$this->method = strtoupper($method);
197
+	}
198
+
199
+	/**
200
+	 * Sets the given request-target to the request
201
+	 *
202
+	 * @param mixed $requestTarget
203
+	 *
204
+	 * @return void
205
+	 */
206
+	protected function setRequestTarget($requestTarget) : void
207
+	{
208
+		$this->validateRequestTarget($requestTarget);
209
+
210
+		/**
211
+		 * @var string $requestTarget
212
+		 */
213
+
214
+		$this->requestTarget = $requestTarget;
215
+	}
216
+
217
+	/**
218
+	 * Sets the given URI to the request
219
+	 *
220
+	 * @param string|UriInterface $uri
221
+	 * @param bool $preserveHost
222
+	 *
223
+	 * @return void
224
+	 */
225
+	protected function setUri($uri, $preserveHost = false) : void
226
+	{
227
+		if (! ($uri instanceof UriInterface)) {
228
+			$uri = (new UriFactory)->createUri($uri);
229
+		}
230
+
231
+		$this->uri = $uri;
232
+
233
+		if ('' === $uri->getHost() || ($preserveHost && $this->hasHeader('Host'))) {
234
+			return;
235
+		}
236
+
237
+		$newHost = $uri->getHost();
238
+
239
+		$port = $uri->getPort();
240
+		if (null !== $port) {
241
+			$newHost .= ':' . $port;
242
+		}
243
+
244
+		$this->addHeader('Host', $newHost);
245
+	}
246
+
247
+	/**
248
+	 * Validates the given method
249
+	 *
250
+	 * @param mixed $method
251
+	 *
252
+	 * @return void
253
+	 *
254
+	 * @throws InvalidArgumentException
255
+	 *
256
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.1.1
257
+	 */
258
+	protected function validateMethod($method) : void
259
+	{
260
+		if (!is_string($method)) {
261
+			throw new InvalidArgumentException('HTTP method must be a string');
262
+		}
263
+
264
+		if (!preg_match(HeaderInterface::RFC7230_TOKEN, $method)) {
265
+			throw new InvalidArgumentException(sprintf('HTTP method "%s" is not valid', $method));
266
+		}
267
+	}
268
+
269
+	/**
270
+	 * Validates the given request-target
271
+	 *
272
+	 * @param mixed $requestTarget
273
+	 *
274
+	 * @return void
275
+	 *
276
+	 * @throws InvalidArgumentException
277
+	 *
278
+	 * @link https://tools.ietf.org/html/rfc7230#section-5.3
279
+	 */
280
+	protected function validateRequestTarget($requestTarget) : void
281
+	{
282
+		if (!is_string($requestTarget)) {
283
+			throw new InvalidArgumentException('HTTP request-target must be a string');
284
+		}
285
+
286
+		if (!preg_match('/^[\x21-\x7E\x80-\xFF]+$/', $requestTarget)) {
287
+			throw new InvalidArgumentException(sprintf('HTTP request-target "%s" is not valid', $requestTarget));
288
+		}
289
+	}
290 290
 }
Please login to merge, or discard this patch.
src/Message.php 1 patch
Indentation   +320 added lines, -320 removed lines patch added patch discarded remove patch
@@ -39,324 +39,324 @@
 block discarded – undo
39 39
 class Message implements MessageInterface
40 40
 {
41 41
 
42
-    /**
43
-     * HTTP version
44
-     *
45
-     * @var string
46
-     */
47
-    protected $protocolVersion = '1.1';
48
-
49
-    /**
50
-     * The message headers
51
-     *
52
-     * @var array<string, string[]>
53
-     */
54
-    protected $headers = [];
55
-
56
-    /**
57
-     * The message body
58
-     *
59
-     * @var StreamInterface|null
60
-     */
61
-    protected $body = null;
62
-
63
-    /**
64
-     * Constructor of the class
65
-     *
66
-     * @param array<string, string|string[]>|null $headers
67
-     * @param StreamInterface|null $body
68
-     * @param string|null $protocolVersion
69
-     */
70
-    public function __construct(
71
-        ?array $headers = null,
72
-        ?StreamInterface $body = null,
73
-        ?string $protocolVersion = null
74
-    ) {
75
-        if (isset($protocolVersion)) {
76
-            $this->setProtocolVersion($protocolVersion);
77
-        }
78
-
79
-        if (isset($headers)) {
80
-            foreach ($headers as $name => $value) {
81
-                $this->addHeader($name, $value);
82
-            }
83
-        }
84
-
85
-        if (isset($body)) {
86
-            $this->body = $body;
87
-        }
88
-    }
89
-
90
-    /**
91
-     * {@inheritdoc}
92
-     */
93
-    public function getProtocolVersion() : string
94
-    {
95
-        return $this->protocolVersion;
96
-    }
97
-
98
-    /**
99
-     * {@inheritdoc}
100
-     */
101
-    public function withProtocolVersion($version) : MessageInterface
102
-    {
103
-        $clone = clone $this;
104
-        $clone->setProtocolVersion($version);
105
-
106
-        return $clone;
107
-    }
108
-
109
-    /**
110
-     * {@inheritdoc}
111
-     */
112
-    public function getHeaders() : array
113
-    {
114
-        return $this->headers;
115
-    }
116
-
117
-    /**
118
-     * {@inheritdoc}
119
-     */
120
-    public function hasHeader($name) : bool
121
-    {
122
-        $name = $this->normalizeHeaderName($name);
123
-
124
-        return ! empty($this->headers[$name]);
125
-    }
126
-
127
-    /**
128
-     * {@inheritdoc}
129
-     */
130
-    public function getHeader($name) : array
131
-    {
132
-        $name = $this->normalizeHeaderName($name);
133
-
134
-        if (empty($this->headers[$name])) {
135
-            return [];
136
-        }
137
-
138
-        return $this->headers[$name];
139
-    }
140
-
141
-    /**
142
-     * {@inheritdoc}
143
-     */
144
-    public function getHeaderLine($name) : string
145
-    {
146
-        $name = $this->normalizeHeaderName($name);
147
-
148
-        if (empty($this->headers[$name])) {
149
-            return '';
150
-        }
151
-
152
-        return implode(', ', $this->headers[$name]);
153
-    }
154
-
155
-    /**
156
-     * {@inheritdoc}
157
-     */
158
-    public function withHeader($name, $value) : MessageInterface
159
-    {
160
-        $clone = clone $this;
161
-        $clone->addHeader($name, $value);
162
-
163
-        return $clone;
164
-    }
165
-
166
-    /**
167
-     * {@inheritdoc}
168
-     */
169
-    public function withAddedHeader($name, $value) : MessageInterface
170
-    {
171
-        $clone = clone $this;
172
-        $clone->addHeader($name, $value, false);
173
-
174
-        return $clone;
175
-    }
176
-
177
-    /**
178
-     * {@inheritdoc}
179
-     */
180
-    public function withoutHeader($name) : MessageInterface
181
-    {
182
-        $name = $this->normalizeHeaderName($name);
183
-
184
-        $clone = clone $this;
185
-        unset($clone->headers[$name]);
186
-
187
-        return $clone;
188
-    }
189
-
190
-    /**
191
-     * {@inheritdoc}
192
-     */
193
-    public function getBody() : StreamInterface
194
-    {
195
-        if (null === $this->body) {
196
-            $this->body = (new StreamFactory)->createStream();
197
-        }
198
-
199
-        return $this->body;
200
-    }
201
-
202
-    /**
203
-     * {@inheritdoc}
204
-     */
205
-    public function withBody(StreamInterface $body) : MessageInterface
206
-    {
207
-        $clone = clone $this;
208
-        $clone->body = $body;
209
-
210
-        return $clone;
211
-    }
212
-
213
-    /**
214
-     * Sets the given HTTP version to the message
215
-     *
216
-     * @param string $version
217
-     *
218
-     * @return void
219
-     */
220
-    protected function setProtocolVersion($version) : void
221
-    {
222
-        $this->validateProtocolVersion($version);
223
-
224
-        $this->protocolVersion = $version;
225
-    }
226
-
227
-    /**
228
-     * Adds the given header field to the message
229
-     *
230
-     * @param string $name
231
-     * @param string|string[] $value
232
-     * @param bool $replace
233
-     *
234
-     * @return void
235
-     */
236
-    protected function addHeader($name, $value, bool $replace = true) : void
237
-    {
238
-        $this->validateHeaderName($name);
239
-        $this->validateHeaderValue($value, $name);
240
-
241
-        $name = $this->normalizeHeaderName($name);
242
-        $value = (array) $value;
243
-
244
-        if ($replace) {
245
-            $this->headers[$name] = $value;
246
-            return;
247
-        }
248
-
249
-        foreach ($value as $item) {
250
-            $this->headers[$name][] = $item;
251
-        }
252
-    }
253
-
254
-    /**
255
-     * Validates the given HTTP version
256
-     *
257
-     * @param mixed $version
258
-     *
259
-     * @return void
260
-     *
261
-     * @throws InvalidArgumentException
262
-     *
263
-     * @link https://tools.ietf.org/html/rfc2145
264
-     * @link https://tools.ietf.org/html/rfc7230#section-2.6
265
-     * @link https://tools.ietf.org/html/rfc7540
266
-     */
267
-    protected function validateProtocolVersion($version) : void
268
-    {
269
-        static $allowed = ['1.0' => true, '1.1' => true, '2.0' => true, '2' => true];
270
-
271
-        if (!is_string($version)) {
272
-            throw new InvalidArgumentException('HTTP version must be a string');
273
-        }
274
-
275
-        if (!isset($allowed[$version])) {
276
-            throw new InvalidArgumentException(sprintf(
277
-                'The HTTP version "%s" is not valid, use only: 1.0, 1.1, 2{.0}',
278
-                $version
279
-            ));
280
-        }
281
-    }
282
-
283
-    /**
284
-     * Validates the given header name
285
-     *
286
-     * @param mixed $name
287
-     *
288
-     * @return void
289
-     *
290
-     * @throws InvalidArgumentException
291
-     *
292
-     * @link https://tools.ietf.org/html/rfc7230#section-3.2
293
-     */
294
-    protected function validateHeaderName($name) : void
295
-    {
296
-        if (!is_string($name)) {
297
-            throw new InvalidArgumentException('Header name must be a string');
298
-        }
299
-
300
-        if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) {
301
-            throw new InvalidArgumentException(sprintf(
302
-                'The header name "%s" is not valid',
303
-                $name
304
-            ));
305
-        }
306
-    }
307
-
308
-    /**
309
-     * Validates the given header value
310
-     *
311
-     * @param mixed $value
312
-     * @param string $name
313
-     *
314
-     * @return void
315
-     *
316
-     * @throws InvalidArgumentException
317
-     *
318
-     * @link https://tools.ietf.org/html/rfc7230#section-3.2
319
-     */
320
-    protected function validateHeaderValue($value, string $name) : void
321
-    {
322
-        $items = (array) $value;
323
-
324
-        if ([] === $items) {
325
-            throw new InvalidArgumentException(sprintf(
326
-                'The header "%s" value must be a string or a non-empty array',
327
-                $name
328
-            ));
329
-        }
330
-
331
-        foreach ($items as $item) {
332
-            if (!is_string($item)) {
333
-                throw new InvalidArgumentException(sprintf(
334
-                    'The header "%s" value must be a string or an array with strings only',
335
-                    $name
336
-                ));
337
-            }
338
-
339
-            if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $item)) {
340
-                throw new InvalidArgumentException(sprintf(
341
-                    'The header "%s" value "%s" is not valid',
342
-                    $name,
343
-                    $item
344
-                ));
345
-            }
346
-        }
347
-    }
348
-
349
-    /**
350
-     * Normalizes the given header name
351
-     *
352
-     * @param string $name
353
-     *
354
-     * @return string
355
-     *
356
-     * @link https://tools.ietf.org/html/rfc7230#section-3.2
357
-     */
358
-    protected function normalizeHeaderName($name) : string
359
-    {
360
-        return ucwords(strtolower($name), '-');
361
-    }
42
+	/**
43
+	 * HTTP version
44
+	 *
45
+	 * @var string
46
+	 */
47
+	protected $protocolVersion = '1.1';
48
+
49
+	/**
50
+	 * The message headers
51
+	 *
52
+	 * @var array<string, string[]>
53
+	 */
54
+	protected $headers = [];
55
+
56
+	/**
57
+	 * The message body
58
+	 *
59
+	 * @var StreamInterface|null
60
+	 */
61
+	protected $body = null;
62
+
63
+	/**
64
+	 * Constructor of the class
65
+	 *
66
+	 * @param array<string, string|string[]>|null $headers
67
+	 * @param StreamInterface|null $body
68
+	 * @param string|null $protocolVersion
69
+	 */
70
+	public function __construct(
71
+		?array $headers = null,
72
+		?StreamInterface $body = null,
73
+		?string $protocolVersion = null
74
+	) {
75
+		if (isset($protocolVersion)) {
76
+			$this->setProtocolVersion($protocolVersion);
77
+		}
78
+
79
+		if (isset($headers)) {
80
+			foreach ($headers as $name => $value) {
81
+				$this->addHeader($name, $value);
82
+			}
83
+		}
84
+
85
+		if (isset($body)) {
86
+			$this->body = $body;
87
+		}
88
+	}
89
+
90
+	/**
91
+	 * {@inheritdoc}
92
+	 */
93
+	public function getProtocolVersion() : string
94
+	{
95
+		return $this->protocolVersion;
96
+	}
97
+
98
+	/**
99
+	 * {@inheritdoc}
100
+	 */
101
+	public function withProtocolVersion($version) : MessageInterface
102
+	{
103
+		$clone = clone $this;
104
+		$clone->setProtocolVersion($version);
105
+
106
+		return $clone;
107
+	}
108
+
109
+	/**
110
+	 * {@inheritdoc}
111
+	 */
112
+	public function getHeaders() : array
113
+	{
114
+		return $this->headers;
115
+	}
116
+
117
+	/**
118
+	 * {@inheritdoc}
119
+	 */
120
+	public function hasHeader($name) : bool
121
+	{
122
+		$name = $this->normalizeHeaderName($name);
123
+
124
+		return ! empty($this->headers[$name]);
125
+	}
126
+
127
+	/**
128
+	 * {@inheritdoc}
129
+	 */
130
+	public function getHeader($name) : array
131
+	{
132
+		$name = $this->normalizeHeaderName($name);
133
+
134
+		if (empty($this->headers[$name])) {
135
+			return [];
136
+		}
137
+
138
+		return $this->headers[$name];
139
+	}
140
+
141
+	/**
142
+	 * {@inheritdoc}
143
+	 */
144
+	public function getHeaderLine($name) : string
145
+	{
146
+		$name = $this->normalizeHeaderName($name);
147
+
148
+		if (empty($this->headers[$name])) {
149
+			return '';
150
+		}
151
+
152
+		return implode(', ', $this->headers[$name]);
153
+	}
154
+
155
+	/**
156
+	 * {@inheritdoc}
157
+	 */
158
+	public function withHeader($name, $value) : MessageInterface
159
+	{
160
+		$clone = clone $this;
161
+		$clone->addHeader($name, $value);
162
+
163
+		return $clone;
164
+	}
165
+
166
+	/**
167
+	 * {@inheritdoc}
168
+	 */
169
+	public function withAddedHeader($name, $value) : MessageInterface
170
+	{
171
+		$clone = clone $this;
172
+		$clone->addHeader($name, $value, false);
173
+
174
+		return $clone;
175
+	}
176
+
177
+	/**
178
+	 * {@inheritdoc}
179
+	 */
180
+	public function withoutHeader($name) : MessageInterface
181
+	{
182
+		$name = $this->normalizeHeaderName($name);
183
+
184
+		$clone = clone $this;
185
+		unset($clone->headers[$name]);
186
+
187
+		return $clone;
188
+	}
189
+
190
+	/**
191
+	 * {@inheritdoc}
192
+	 */
193
+	public function getBody() : StreamInterface
194
+	{
195
+		if (null === $this->body) {
196
+			$this->body = (new StreamFactory)->createStream();
197
+		}
198
+
199
+		return $this->body;
200
+	}
201
+
202
+	/**
203
+	 * {@inheritdoc}
204
+	 */
205
+	public function withBody(StreamInterface $body) : MessageInterface
206
+	{
207
+		$clone = clone $this;
208
+		$clone->body = $body;
209
+
210
+		return $clone;
211
+	}
212
+
213
+	/**
214
+	 * Sets the given HTTP version to the message
215
+	 *
216
+	 * @param string $version
217
+	 *
218
+	 * @return void
219
+	 */
220
+	protected function setProtocolVersion($version) : void
221
+	{
222
+		$this->validateProtocolVersion($version);
223
+
224
+		$this->protocolVersion = $version;
225
+	}
226
+
227
+	/**
228
+	 * Adds the given header field to the message
229
+	 *
230
+	 * @param string $name
231
+	 * @param string|string[] $value
232
+	 * @param bool $replace
233
+	 *
234
+	 * @return void
235
+	 */
236
+	protected function addHeader($name, $value, bool $replace = true) : void
237
+	{
238
+		$this->validateHeaderName($name);
239
+		$this->validateHeaderValue($value, $name);
240
+
241
+		$name = $this->normalizeHeaderName($name);
242
+		$value = (array) $value;
243
+
244
+		if ($replace) {
245
+			$this->headers[$name] = $value;
246
+			return;
247
+		}
248
+
249
+		foreach ($value as $item) {
250
+			$this->headers[$name][] = $item;
251
+		}
252
+	}
253
+
254
+	/**
255
+	 * Validates the given HTTP version
256
+	 *
257
+	 * @param mixed $version
258
+	 *
259
+	 * @return void
260
+	 *
261
+	 * @throws InvalidArgumentException
262
+	 *
263
+	 * @link https://tools.ietf.org/html/rfc2145
264
+	 * @link https://tools.ietf.org/html/rfc7230#section-2.6
265
+	 * @link https://tools.ietf.org/html/rfc7540
266
+	 */
267
+	protected function validateProtocolVersion($version) : void
268
+	{
269
+		static $allowed = ['1.0' => true, '1.1' => true, '2.0' => true, '2' => true];
270
+
271
+		if (!is_string($version)) {
272
+			throw new InvalidArgumentException('HTTP version must be a string');
273
+		}
274
+
275
+		if (!isset($allowed[$version])) {
276
+			throw new InvalidArgumentException(sprintf(
277
+				'The HTTP version "%s" is not valid, use only: 1.0, 1.1, 2{.0}',
278
+				$version
279
+			));
280
+		}
281
+	}
282
+
283
+	/**
284
+	 * Validates the given header name
285
+	 *
286
+	 * @param mixed $name
287
+	 *
288
+	 * @return void
289
+	 *
290
+	 * @throws InvalidArgumentException
291
+	 *
292
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.2
293
+	 */
294
+	protected function validateHeaderName($name) : void
295
+	{
296
+		if (!is_string($name)) {
297
+			throw new InvalidArgumentException('Header name must be a string');
298
+		}
299
+
300
+		if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) {
301
+			throw new InvalidArgumentException(sprintf(
302
+				'The header name "%s" is not valid',
303
+				$name
304
+			));
305
+		}
306
+	}
307
+
308
+	/**
309
+	 * Validates the given header value
310
+	 *
311
+	 * @param mixed $value
312
+	 * @param string $name
313
+	 *
314
+	 * @return void
315
+	 *
316
+	 * @throws InvalidArgumentException
317
+	 *
318
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.2
319
+	 */
320
+	protected function validateHeaderValue($value, string $name) : void
321
+	{
322
+		$items = (array) $value;
323
+
324
+		if ([] === $items) {
325
+			throw new InvalidArgumentException(sprintf(
326
+				'The header "%s" value must be a string or a non-empty array',
327
+				$name
328
+			));
329
+		}
330
+
331
+		foreach ($items as $item) {
332
+			if (!is_string($item)) {
333
+				throw new InvalidArgumentException(sprintf(
334
+					'The header "%s" value must be a string or an array with strings only',
335
+					$name
336
+				));
337
+			}
338
+
339
+			if (!preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $item)) {
340
+				throw new InvalidArgumentException(sprintf(
341
+					'The header "%s" value "%s" is not valid',
342
+					$name,
343
+					$item
344
+				));
345
+			}
346
+		}
347
+	}
348
+
349
+	/**
350
+	 * Normalizes the given header name
351
+	 *
352
+	 * @param string $name
353
+	 *
354
+	 * @return string
355
+	 *
356
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.2
357
+	 */
358
+	protected function normalizeHeaderName($name) : string
359
+	{
360
+		return ucwords(strtolower($name), '-');
361
+	}
362 362
 }
Please login to merge, or discard this patch.