Passed
Pull Request — master (#31)
by Anatoly
39:30
created
src/UploadedFile.php 1 patch
Indentation   +227 added lines, -227 removed lines patch added patch discarded remove patch
@@ -49,231 +49,231 @@
 block discarded – undo
49 49
 class UploadedFile implements UploadedFileInterface
50 50
 {
51 51
 
52
-    /**
53
-     * List of upload errors
54
-     *
55
-     * @link https://www.php.net/manual/en/features.file-upload.errors.php
56
-     *
57
-     * @var array<int, string>
58
-     */
59
-    public const UPLOAD_ERRORS = [
60
-        UPLOAD_ERR_OK         => 'No error',
61
-        UPLOAD_ERR_INI_SIZE   => 'Uploaded file exceeds the upload_max_filesize directive in php.ini',
62
-        UPLOAD_ERR_FORM_SIZE  => 'Uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the form',
63
-        UPLOAD_ERR_PARTIAL    => 'Uploaded file was only partially uploaded',
64
-        UPLOAD_ERR_NO_FILE    => 'No file was uploaded',
65
-        UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
66
-        UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
67
-        UPLOAD_ERR_EXTENSION  => 'File upload stopped by extension',
68
-    ];
69
-
70
-    /**
71
-     * The file stream
72
-     *
73
-     * @var StreamInterface|null
74
-     */
75
-    private ?StreamInterface $stream = null;
76
-
77
-    /**
78
-     * The file size
79
-     *
80
-     * @var int|null
81
-     */
82
-    private ?int $size;
83
-
84
-    /**
85
-     * The file's error code
86
-     *
87
-     * @var int
88
-     */
89
-    private int $errorCode;
90
-
91
-    /**
92
-     * The file's error message
93
-     *
94
-     * @var string
95
-     */
96
-    private string $errorMessage;
97
-
98
-    /**
99
-     * The client's file name
100
-     *
101
-     * @var string|null
102
-     */
103
-    private ?string $clientFilename;
104
-
105
-    /**
106
-     * The client's file media type
107
-     *
108
-     * @var string|null
109
-     */
110
-    private ?string $clientMediaType;
111
-
112
-    /**
113
-     * Constructor of the class
114
-     *
115
-     * @param StreamInterface $stream
116
-     * @param int|null $size
117
-     * @param int $error
118
-     * @param string|null $clientFilename
119
-     * @param string|null $clientMediaType
120
-     */
121
-    public function __construct(
122
-        StreamInterface $stream,
123
-        ?int $size = null,
124
-        int $error = UPLOAD_ERR_OK,
125
-        ?string $clientFilename = null,
126
-        ?string $clientMediaType = null
127
-    ) {
128
-        if (UPLOAD_ERR_OK === $error) {
129
-            $this->stream = $stream;
130
-        }
131
-
132
-        $message = self::UPLOAD_ERRORS[$error] ?? 'Unknown error';
133
-
134
-        $this->size = $size;
135
-        $this->errorCode = $error;
136
-        $this->errorMessage = $message;
137
-        $this->clientFilename = $clientFilename;
138
-        $this->clientMediaType = $clientMediaType;
139
-    }
140
-
141
-    /**
142
-     * Gets the file stream
143
-     *
144
-     * @return StreamInterface
145
-     *
146
-     * @throws RuntimeException
147
-     *         - If the file has no a stream due to an error;
148
-     *         - If the file was already moved.
149
-     */
150
-    public function getStream(): StreamInterface
151
-    {
152
-        if (UPLOAD_ERR_OK <> $this->errorCode) {
153
-            throw new RuntimeException(sprintf(
154
-                'Uploaded file has no a stream due to the error #%d (%s)',
155
-                $this->errorCode,
156
-                $this->errorMessage
157
-            ));
158
-        }
159
-
160
-        if (!isset($this->stream)) {
161
-            throw new RuntimeException(
162
-                'Uploaded file has no a stream because it was already moved'
163
-            );
164
-        }
165
-
166
-        return $this->stream;
167
-    }
168
-
169
-    /**
170
-     * Moves the file to the given path
171
-     *
172
-     * @param string $targetPath
173
-     *
174
-     * @return void
175
-     *
176
-     * @throws RuntimeException
177
-     *         - If the file has no a stream due to an error;
178
-     *         - If the file was already moved;
179
-     *         - If the file cannot be read;
180
-     *         - If the target path cannot be used.
181
-     */
182
-    public function moveTo($targetPath): void
183
-    {
184
-        if (UPLOAD_ERR_OK <> $this->errorCode) {
185
-            throw new RuntimeException(sprintf(
186
-                'Uploaded file cannot be moved due to the error #%d (%s)',
187
-                $this->errorCode,
188
-                $this->errorMessage
189
-            ));
190
-        }
191
-
192
-        if (!isset($this->stream)) {
193
-            throw new RuntimeException(
194
-                'Uploaded file cannot be moved because it was already moved'
195
-            );
196
-        }
197
-
198
-        if (!$this->stream->isReadable()) {
199
-            throw new RuntimeException(
200
-                'Uploaded file cannot be moved because it is not readable'
201
-            );
202
-        }
203
-
204
-        $targetDir = dirname($targetPath);
205
-        if (!is_dir($targetDir) || !is_writable($targetDir)) {
206
-            throw new RuntimeException(sprintf(
207
-                'Uploaded file cannot be moved because the directory "%s" is not writable',
208
-                $targetDir
209
-            ));
210
-        }
211
-
212
-        $targetStream = new FileStream($targetPath, 'wb');
213
-
214
-        if ($this->stream->isSeekable()) {
215
-            $this->stream->rewind();
216
-        }
217
-
218
-        while (!$this->stream->eof()) {
219
-            $targetStream->write(
220
-                $this->stream->read(4096)
221
-            );
222
-        }
223
-
224
-        $targetStream->close();
225
-
226
-        /** @var string|null */
227
-        $sourcePath = $this->stream->getMetadata('uri');
228
-
229
-        $this->stream->close();
230
-        $this->stream = null;
231
-
232
-        if (isset($sourcePath) && is_file($sourcePath)) {
233
-            $sourceDir = dirname($sourcePath);
234
-            if (is_writable($sourceDir)) {
235
-                unlink($sourcePath);
236
-            }
237
-        }
238
-    }
239
-
240
-    /**
241
-     * Gets the file size
242
-     *
243
-     * @return int|null
244
-     */
245
-    public function getSize(): ?int
246
-    {
247
-        return $this->size;
248
-    }
249
-
250
-    /**
251
-     * Gets the file's error code
252
-     *
253
-     * @return int
254
-     */
255
-    public function getError(): int
256
-    {
257
-        return $this->errorCode;
258
-    }
259
-
260
-    /**
261
-     * Gets the client's file name
262
-     *
263
-     * @return string|null
264
-     */
265
-    public function getClientFilename(): ?string
266
-    {
267
-        return $this->clientFilename;
268
-    }
269
-
270
-    /**
271
-     * Gets the client's file media type
272
-     *
273
-     * @return string|null
274
-     */
275
-    public function getClientMediaType(): ?string
276
-    {
277
-        return $this->clientMediaType;
278
-    }
52
+	/**
53
+	 * List of upload errors
54
+	 *
55
+	 * @link https://www.php.net/manual/en/features.file-upload.errors.php
56
+	 *
57
+	 * @var array<int, string>
58
+	 */
59
+	public const UPLOAD_ERRORS = [
60
+		UPLOAD_ERR_OK         => 'No error',
61
+		UPLOAD_ERR_INI_SIZE   => 'Uploaded file exceeds the upload_max_filesize directive in php.ini',
62
+		UPLOAD_ERR_FORM_SIZE  => 'Uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the form',
63
+		UPLOAD_ERR_PARTIAL    => 'Uploaded file was only partially uploaded',
64
+		UPLOAD_ERR_NO_FILE    => 'No file was uploaded',
65
+		UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
66
+		UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
67
+		UPLOAD_ERR_EXTENSION  => 'File upload stopped by extension',
68
+	];
69
+
70
+	/**
71
+	 * The file stream
72
+	 *
73
+	 * @var StreamInterface|null
74
+	 */
75
+	private ?StreamInterface $stream = null;
76
+
77
+	/**
78
+	 * The file size
79
+	 *
80
+	 * @var int|null
81
+	 */
82
+	private ?int $size;
83
+
84
+	/**
85
+	 * The file's error code
86
+	 *
87
+	 * @var int
88
+	 */
89
+	private int $errorCode;
90
+
91
+	/**
92
+	 * The file's error message
93
+	 *
94
+	 * @var string
95
+	 */
96
+	private string $errorMessage;
97
+
98
+	/**
99
+	 * The client's file name
100
+	 *
101
+	 * @var string|null
102
+	 */
103
+	private ?string $clientFilename;
104
+
105
+	/**
106
+	 * The client's file media type
107
+	 *
108
+	 * @var string|null
109
+	 */
110
+	private ?string $clientMediaType;
111
+
112
+	/**
113
+	 * Constructor of the class
114
+	 *
115
+	 * @param StreamInterface $stream
116
+	 * @param int|null $size
117
+	 * @param int $error
118
+	 * @param string|null $clientFilename
119
+	 * @param string|null $clientMediaType
120
+	 */
121
+	public function __construct(
122
+		StreamInterface $stream,
123
+		?int $size = null,
124
+		int $error = UPLOAD_ERR_OK,
125
+		?string $clientFilename = null,
126
+		?string $clientMediaType = null
127
+	) {
128
+		if (UPLOAD_ERR_OK === $error) {
129
+			$this->stream = $stream;
130
+		}
131
+
132
+		$message = self::UPLOAD_ERRORS[$error] ?? 'Unknown error';
133
+
134
+		$this->size = $size;
135
+		$this->errorCode = $error;
136
+		$this->errorMessage = $message;
137
+		$this->clientFilename = $clientFilename;
138
+		$this->clientMediaType = $clientMediaType;
139
+	}
140
+
141
+	/**
142
+	 * Gets the file stream
143
+	 *
144
+	 * @return StreamInterface
145
+	 *
146
+	 * @throws RuntimeException
147
+	 *         - If the file has no a stream due to an error;
148
+	 *         - If the file was already moved.
149
+	 */
150
+	public function getStream(): StreamInterface
151
+	{
152
+		if (UPLOAD_ERR_OK <> $this->errorCode) {
153
+			throw new RuntimeException(sprintf(
154
+				'Uploaded file has no a stream due to the error #%d (%s)',
155
+				$this->errorCode,
156
+				$this->errorMessage
157
+			));
158
+		}
159
+
160
+		if (!isset($this->stream)) {
161
+			throw new RuntimeException(
162
+				'Uploaded file has no a stream because it was already moved'
163
+			);
164
+		}
165
+
166
+		return $this->stream;
167
+	}
168
+
169
+	/**
170
+	 * Moves the file to the given path
171
+	 *
172
+	 * @param string $targetPath
173
+	 *
174
+	 * @return void
175
+	 *
176
+	 * @throws RuntimeException
177
+	 *         - If the file has no a stream due to an error;
178
+	 *         - If the file was already moved;
179
+	 *         - If the file cannot be read;
180
+	 *         - If the target path cannot be used.
181
+	 */
182
+	public function moveTo($targetPath): void
183
+	{
184
+		if (UPLOAD_ERR_OK <> $this->errorCode) {
185
+			throw new RuntimeException(sprintf(
186
+				'Uploaded file cannot be moved due to the error #%d (%s)',
187
+				$this->errorCode,
188
+				$this->errorMessage
189
+			));
190
+		}
191
+
192
+		if (!isset($this->stream)) {
193
+			throw new RuntimeException(
194
+				'Uploaded file cannot be moved because it was already moved'
195
+			);
196
+		}
197
+
198
+		if (!$this->stream->isReadable()) {
199
+			throw new RuntimeException(
200
+				'Uploaded file cannot be moved because it is not readable'
201
+			);
202
+		}
203
+
204
+		$targetDir = dirname($targetPath);
205
+		if (!is_dir($targetDir) || !is_writable($targetDir)) {
206
+			throw new RuntimeException(sprintf(
207
+				'Uploaded file cannot be moved because the directory "%s" is not writable',
208
+				$targetDir
209
+			));
210
+		}
211
+
212
+		$targetStream = new FileStream($targetPath, 'wb');
213
+
214
+		if ($this->stream->isSeekable()) {
215
+			$this->stream->rewind();
216
+		}
217
+
218
+		while (!$this->stream->eof()) {
219
+			$targetStream->write(
220
+				$this->stream->read(4096)
221
+			);
222
+		}
223
+
224
+		$targetStream->close();
225
+
226
+		/** @var string|null */
227
+		$sourcePath = $this->stream->getMetadata('uri');
228
+
229
+		$this->stream->close();
230
+		$this->stream = null;
231
+
232
+		if (isset($sourcePath) && is_file($sourcePath)) {
233
+			$sourceDir = dirname($sourcePath);
234
+			if (is_writable($sourceDir)) {
235
+				unlink($sourcePath);
236
+			}
237
+		}
238
+	}
239
+
240
+	/**
241
+	 * Gets the file size
242
+	 *
243
+	 * @return int|null
244
+	 */
245
+	public function getSize(): ?int
246
+	{
247
+		return $this->size;
248
+	}
249
+
250
+	/**
251
+	 * Gets the file's error code
252
+	 *
253
+	 * @return int
254
+	 */
255
+	public function getError(): int
256
+	{
257
+		return $this->errorCode;
258
+	}
259
+
260
+	/**
261
+	 * Gets the client's file name
262
+	 *
263
+	 * @return string|null
264
+	 */
265
+	public function getClientFilename(): ?string
266
+	{
267
+		return $this->clientFilename;
268
+	}
269
+
270
+	/**
271
+	 * Gets the client's file media type
272
+	 *
273
+	 * @return string|null
274
+	 */
275
+	public function getClientMediaType(): ?string
276
+	{
277
+		return $this->clientMediaType;
278
+	}
279 279
 }
Please login to merge, or discard this patch.
src/Entity/IpAddress.php 1 patch
Indentation   +103 added lines, -103 removed lines patch added patch discarded remove patch
@@ -31,107 +31,107 @@
 block discarded – undo
31 31
 final class IpAddress
32 32
 {
33 33
 
34
-    /**
35
-     * The IP address value
36
-     *
37
-     * @var string
38
-     */
39
-    private string $value;
40
-
41
-    /**
42
-     * The list of proxies in front of this IP address
43
-     *
44
-     * @var list<string>
45
-     */
46
-    private array $proxies;
47
-
48
-    /**
49
-     * Constructor of the class
50
-     *
51
-     * @param string $value
52
-     * @param list<string> $proxies
53
-     */
54
-    public function __construct(string $value, array $proxies = [])
55
-    {
56
-        $this->value = $value;
57
-        $this->proxies = $proxies;
58
-    }
59
-
60
-    /**
61
-     * Gets the IP address value
62
-     *
63
-     * @return string
64
-     */
65
-    public function getValue(): string
66
-    {
67
-        return $this->value;
68
-    }
69
-
70
-    /**
71
-     * Gets the list of proxies in front of this IP address
72
-     *
73
-     * @return list<string>
74
-     */
75
-    public function getProxies(): array
76
-    {
77
-        return $this->proxies;
78
-    }
79
-
80
-    /**
81
-     * Checks if the IP address is valid
82
-     *
83
-     * @return bool
84
-     */
85
-    public function isValid(): bool
86
-    {
87
-        return false !== filter_var($this->value, FILTER_VALIDATE_IP);
88
-    }
89
-
90
-    /**
91
-     * Checks if the IP address is IPv4
92
-     *
93
-     * @return bool
94
-     */
95
-    public function isVersion4(): bool
96
-    {
97
-        return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
98
-    }
99
-
100
-    /**
101
-     * Checks if the IP address is IPv6
102
-     *
103
-     * @return bool
104
-     */
105
-    public function isVersion6(): bool
106
-    {
107
-        return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
108
-    }
109
-
110
-    /**
111
-     * Checks if the IP address is in the private range
112
-     *
113
-     * @return bool
114
-     */
115
-    public function isInPrivateRange(): bool
116
-    {
117
-        if (!$this->isValid()) {
118
-            return false;
119
-        }
120
-
121
-        return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE);
122
-    }
123
-
124
-    /**
125
-     * Checks if the IP address is in the reserved range
126
-     *
127
-     * @return bool
128
-     */
129
-    public function isInReservedRange(): bool
130
-    {
131
-        if (!$this->isValid()) {
132
-            return false;
133
-        }
134
-
135
-        return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
136
-    }
34
+	/**
35
+	 * The IP address value
36
+	 *
37
+	 * @var string
38
+	 */
39
+	private string $value;
40
+
41
+	/**
42
+	 * The list of proxies in front of this IP address
43
+	 *
44
+	 * @var list<string>
45
+	 */
46
+	private array $proxies;
47
+
48
+	/**
49
+	 * Constructor of the class
50
+	 *
51
+	 * @param string $value
52
+	 * @param list<string> $proxies
53
+	 */
54
+	public function __construct(string $value, array $proxies = [])
55
+	{
56
+		$this->value = $value;
57
+		$this->proxies = $proxies;
58
+	}
59
+
60
+	/**
61
+	 * Gets the IP address value
62
+	 *
63
+	 * @return string
64
+	 */
65
+	public function getValue(): string
66
+	{
67
+		return $this->value;
68
+	}
69
+
70
+	/**
71
+	 * Gets the list of proxies in front of this IP address
72
+	 *
73
+	 * @return list<string>
74
+	 */
75
+	public function getProxies(): array
76
+	{
77
+		return $this->proxies;
78
+	}
79
+
80
+	/**
81
+	 * Checks if the IP address is valid
82
+	 *
83
+	 * @return bool
84
+	 */
85
+	public function isValid(): bool
86
+	{
87
+		return false !== filter_var($this->value, FILTER_VALIDATE_IP);
88
+	}
89
+
90
+	/**
91
+	 * Checks if the IP address is IPv4
92
+	 *
93
+	 * @return bool
94
+	 */
95
+	public function isVersion4(): bool
96
+	{
97
+		return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
98
+	}
99
+
100
+	/**
101
+	 * Checks if the IP address is IPv6
102
+	 *
103
+	 * @return bool
104
+	 */
105
+	public function isVersion6(): bool
106
+	{
107
+		return false !== filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
108
+	}
109
+
110
+	/**
111
+	 * Checks if the IP address is in the private range
112
+	 *
113
+	 * @return bool
114
+	 */
115
+	public function isInPrivateRange(): bool
116
+	{
117
+		if (!$this->isValid()) {
118
+			return false;
119
+		}
120
+
121
+		return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE);
122
+	}
123
+
124
+	/**
125
+	 * Checks if the IP address is in the reserved range
126
+	 *
127
+	 * @return bool
128
+	 */
129
+	public function isInReservedRange(): bool
130
+	{
131
+		if (!$this->isValid()) {
132
+			return false;
133
+		}
134
+
135
+		return false === filter_var($this->value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
136
+	}
137 137
 }
Please login to merge, or discard this patch.
src/Request.php 1 patch
Indentation   +297 added lines, -297 removed lines patch added patch discarded remove patch
@@ -36,301 +36,301 @@
 block discarded – undo
36 36
 class Request extends Message implements RequestInterface, RequestMethodInterface
37 37
 {
38 38
 
39
-    /**
40
-     * Regular Expression for a request target validation
41
-     *
42
-     * @link https://tools.ietf.org/html/rfc7230#section-5.3
43
-     *
44
-     * @var string
45
-     */
46
-    public const RFC7230_VALID_REQUEST_TARGET = '/^[\x21-\x7E\x80-\xFF]+$/';
47
-
48
-    /**
49
-     * Default request method
50
-     *
51
-     * @var string
52
-     */
53
-    public const DEFAULT_METHOD = self::METHOD_GET;
54
-
55
-    /**
56
-     * Default request URI
57
-     *
58
-     * @var string
59
-     */
60
-    public const DEFAULT_URI = '/';
61
-
62
-    /**
63
-     * The request method (aka verb)
64
-     *
65
-     * @var string
66
-     */
67
-    private string $method = self::DEFAULT_METHOD;
68
-
69
-    /**
70
-     * The request URI
71
-     *
72
-     * @var UriInterface
73
-     */
74
-    private UriInterface $uri;
75
-
76
-    /**
77
-     * The request target
78
-     *
79
-     * @var string|null
80
-     */
81
-    private ?string $requestTarget = null;
82
-
83
-    /**
84
-     * Constructor of the class
85
-     *
86
-     * @param string|null $method
87
-     * @param mixed $uri
88
-     * @param array<string, string|string[]>|null $headers
89
-     * @param StreamInterface|null $body
90
-     *
91
-     * @throws InvalidArgumentException
92
-     *         If one of the arguments isn't valid.
93
-     */
94
-    public function __construct(
95
-        ?string $method = null,
96
-        $uri = null,
97
-        ?array $headers = null,
98
-        ?StreamInterface $body = null
99
-    ) {
100
-        if (isset($method)) {
101
-            $this->setMethod($method);
102
-        }
103
-
104
-        $this->setUri($uri ?? self::DEFAULT_URI);
105
-
106
-        if (isset($headers)) {
107
-            $this->setHeaders($headers);
108
-        }
109
-
110
-        if (isset($body)) {
111
-            $this->setBody($body);
112
-        }
113
-    }
114
-
115
-    /**
116
-     * Gets the request method
117
-     *
118
-     * @return string
119
-     */
120
-    public function getMethod(): string
121
-    {
122
-        return $this->method;
123
-    }
124
-
125
-    /**
126
-     * Creates a new instance of the request with the given method
127
-     *
128
-     * @param string $method
129
-     *
130
-     * @return static
131
-     *
132
-     * @throws InvalidArgumentException
133
-     *         If the method isn't valid.
134
-     */
135
-    public function withMethod($method): RequestInterface
136
-    {
137
-        $clone = clone $this;
138
-        $clone->setMethod($method);
139
-
140
-        return $clone;
141
-    }
142
-
143
-    /**
144
-     * Gets the request URI
145
-     *
146
-     * @return UriInterface
147
-     */
148
-    public function getUri(): UriInterface
149
-    {
150
-        return $this->uri;
151
-    }
152
-
153
-    /**
154
-     * Creates a new instance of the request with the given URI
155
-     *
156
-     * @param UriInterface $uri
157
-     * @param bool $preserveHost
158
-     *
159
-     * @return static
160
-     */
161
-    public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
162
-    {
163
-        $clone = clone $this;
164
-        $clone->setUri($uri, $preserveHost);
165
-
166
-        return $clone;
167
-    }
168
-
169
-    /**
170
-     * Gets the request target
171
-     *
172
-     * @return string
173
-     */
174
-    public function getRequestTarget(): string
175
-    {
176
-        if (isset($this->requestTarget)) {
177
-            return $this->requestTarget;
178
-        }
179
-
180
-        $requestTarget = $this->uri->getPath();
181
-
182
-        // https://tools.ietf.org/html/rfc7230#section-5.3.1
183
-        // https://tools.ietf.org/html/rfc7230#section-2.7
184
-        //
185
-        // origin-form = absolute-path [ "?" query ]
186
-        // absolute-path = 1*( "/" segment )
187
-        if (strncmp($requestTarget, '/', 1) !== 0) {
188
-            return '/';
189
-        }
190
-
191
-        $queryString = $this->uri->getQuery();
192
-        if ($queryString !== '') {
193
-            $requestTarget .= '?' . $queryString;
194
-        }
195
-
196
-        return $requestTarget;
197
-    }
198
-
199
-    /**
200
-     * Creates a new instance of the request with the given request target
201
-     *
202
-     * @param mixed $requestTarget
203
-     *
204
-     * @return static
205
-     *
206
-     * @throws InvalidArgumentException
207
-     *         If the request target isn't valid.
208
-     */
209
-    public function withRequestTarget($requestTarget): RequestInterface
210
-    {
211
-        $clone = clone $this;
212
-        $clone->setRequestTarget($requestTarget);
213
-
214
-        return $clone;
215
-    }
216
-
217
-    /**
218
-     * Sets the given method to the request
219
-     *
220
-     * @param string $method
221
-     *
222
-     * @return void
223
-     *
224
-     * @throws InvalidArgumentException
225
-     *         If the method isn't valid.
226
-     */
227
-    final protected function setMethod($method): void
228
-    {
229
-        $this->validateMethod($method);
230
-
231
-        $this->method = $method;
232
-    }
233
-
234
-    /**
235
-     * Sets the given URI to the request
236
-     *
237
-     * @param mixed $uri
238
-     * @param bool $preserveHost
239
-     *
240
-     * @return void
241
-     *
242
-     * @throws InvalidArgumentException
243
-     *         If the URI isn't valid.
244
-     */
245
-    final protected function setUri($uri, $preserveHost = false): void
246
-    {
247
-        $this->uri = Uri::create($uri);
248
-
249
-        if ($preserveHost && $this->hasHeader('Host')) {
250
-            return;
251
-        }
252
-
253
-        $host = $this->uri->getHost();
254
-        if ($host === '') {
255
-            return;
256
-        }
257
-
258
-        $port = $this->uri->getPort();
259
-        if (isset($port)) {
260
-            $host .= ':' . $port;
261
-        }
262
-
263
-        $this->setHeader('Host', $host, true);
264
-    }
265
-
266
-    /**
267
-     * Sets the given request target to the request
268
-     *
269
-     * @param mixed $requestTarget
270
-     *
271
-     * @return void
272
-     *
273
-     * @throws InvalidArgumentException
274
-     *         If the request target isn't valid.
275
-     */
276
-    final protected function setRequestTarget($requestTarget): void
277
-    {
278
-        $this->validateRequestTarget($requestTarget);
279
-
280
-        /** @var string $requestTarget */
281
-
282
-        $this->requestTarget = $requestTarget;
283
-    }
284
-
285
-    /**
286
-     * Validates the given method
287
-     *
288
-     * @link https://tools.ietf.org/html/rfc7230#section-3.1.1
289
-     *
290
-     * @param mixed $method
291
-     *
292
-     * @return void
293
-     *
294
-     * @throws InvalidArgumentException
295
-     *         If the method isn't valid.
296
-     */
297
-    private function validateMethod($method): void
298
-    {
299
-        if ('' === $method) {
300
-            throw new InvalidArgumentException('HTTP method cannot be an empty');
301
-        }
302
-
303
-        if (!is_string($method)) {
304
-            throw new InvalidArgumentException('HTTP method must be a string');
305
-        }
306
-
307
-        if (!preg_match(Header::RFC7230_VALID_TOKEN, $method)) {
308
-            throw new InvalidArgumentException('Invalid HTTP method');
309
-        }
310
-    }
311
-
312
-    /**
313
-     * Validates the given request target
314
-     *
315
-     * @param mixed $requestTarget
316
-     *
317
-     * @return void
318
-     *
319
-     * @throws InvalidArgumentException
320
-     *         If the request target isn't valid.
321
-     */
322
-    private function validateRequestTarget($requestTarget): void
323
-    {
324
-        if ('' === $requestTarget) {
325
-            throw new InvalidArgumentException('HTTP request target cannot be an empty');
326
-        }
327
-
328
-        if (!is_string($requestTarget)) {
329
-            throw new InvalidArgumentException('HTTP request target must be a string');
330
-        }
331
-
332
-        if (!preg_match(self::RFC7230_VALID_REQUEST_TARGET, $requestTarget)) {
333
-            throw new InvalidArgumentException('Invalid HTTP request target');
334
-        }
335
-    }
39
+	/**
40
+	 * Regular Expression for a request target validation
41
+	 *
42
+	 * @link https://tools.ietf.org/html/rfc7230#section-5.3
43
+	 *
44
+	 * @var string
45
+	 */
46
+	public const RFC7230_VALID_REQUEST_TARGET = '/^[\x21-\x7E\x80-\xFF]+$/';
47
+
48
+	/**
49
+	 * Default request method
50
+	 *
51
+	 * @var string
52
+	 */
53
+	public const DEFAULT_METHOD = self::METHOD_GET;
54
+
55
+	/**
56
+	 * Default request URI
57
+	 *
58
+	 * @var string
59
+	 */
60
+	public const DEFAULT_URI = '/';
61
+
62
+	/**
63
+	 * The request method (aka verb)
64
+	 *
65
+	 * @var string
66
+	 */
67
+	private string $method = self::DEFAULT_METHOD;
68
+
69
+	/**
70
+	 * The request URI
71
+	 *
72
+	 * @var UriInterface
73
+	 */
74
+	private UriInterface $uri;
75
+
76
+	/**
77
+	 * The request target
78
+	 *
79
+	 * @var string|null
80
+	 */
81
+	private ?string $requestTarget = null;
82
+
83
+	/**
84
+	 * Constructor of the class
85
+	 *
86
+	 * @param string|null $method
87
+	 * @param mixed $uri
88
+	 * @param array<string, string|string[]>|null $headers
89
+	 * @param StreamInterface|null $body
90
+	 *
91
+	 * @throws InvalidArgumentException
92
+	 *         If one of the arguments isn't valid.
93
+	 */
94
+	public function __construct(
95
+		?string $method = null,
96
+		$uri = null,
97
+		?array $headers = null,
98
+		?StreamInterface $body = null
99
+	) {
100
+		if (isset($method)) {
101
+			$this->setMethod($method);
102
+		}
103
+
104
+		$this->setUri($uri ?? self::DEFAULT_URI);
105
+
106
+		if (isset($headers)) {
107
+			$this->setHeaders($headers);
108
+		}
109
+
110
+		if (isset($body)) {
111
+			$this->setBody($body);
112
+		}
113
+	}
114
+
115
+	/**
116
+	 * Gets the request method
117
+	 *
118
+	 * @return string
119
+	 */
120
+	public function getMethod(): string
121
+	{
122
+		return $this->method;
123
+	}
124
+
125
+	/**
126
+	 * Creates a new instance of the request with the given method
127
+	 *
128
+	 * @param string $method
129
+	 *
130
+	 * @return static
131
+	 *
132
+	 * @throws InvalidArgumentException
133
+	 *         If the method isn't valid.
134
+	 */
135
+	public function withMethod($method): RequestInterface
136
+	{
137
+		$clone = clone $this;
138
+		$clone->setMethod($method);
139
+
140
+		return $clone;
141
+	}
142
+
143
+	/**
144
+	 * Gets the request URI
145
+	 *
146
+	 * @return UriInterface
147
+	 */
148
+	public function getUri(): UriInterface
149
+	{
150
+		return $this->uri;
151
+	}
152
+
153
+	/**
154
+	 * Creates a new instance of the request with the given URI
155
+	 *
156
+	 * @param UriInterface $uri
157
+	 * @param bool $preserveHost
158
+	 *
159
+	 * @return static
160
+	 */
161
+	public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
162
+	{
163
+		$clone = clone $this;
164
+		$clone->setUri($uri, $preserveHost);
165
+
166
+		return $clone;
167
+	}
168
+
169
+	/**
170
+	 * Gets the request target
171
+	 *
172
+	 * @return string
173
+	 */
174
+	public function getRequestTarget(): string
175
+	{
176
+		if (isset($this->requestTarget)) {
177
+			return $this->requestTarget;
178
+		}
179
+
180
+		$requestTarget = $this->uri->getPath();
181
+
182
+		// https://tools.ietf.org/html/rfc7230#section-5.3.1
183
+		// https://tools.ietf.org/html/rfc7230#section-2.7
184
+		//
185
+		// origin-form = absolute-path [ "?" query ]
186
+		// absolute-path = 1*( "/" segment )
187
+		if (strncmp($requestTarget, '/', 1) !== 0) {
188
+			return '/';
189
+		}
190
+
191
+		$queryString = $this->uri->getQuery();
192
+		if ($queryString !== '') {
193
+			$requestTarget .= '?' . $queryString;
194
+		}
195
+
196
+		return $requestTarget;
197
+	}
198
+
199
+	/**
200
+	 * Creates a new instance of the request with the given request target
201
+	 *
202
+	 * @param mixed $requestTarget
203
+	 *
204
+	 * @return static
205
+	 *
206
+	 * @throws InvalidArgumentException
207
+	 *         If the request target isn't valid.
208
+	 */
209
+	public function withRequestTarget($requestTarget): RequestInterface
210
+	{
211
+		$clone = clone $this;
212
+		$clone->setRequestTarget($requestTarget);
213
+
214
+		return $clone;
215
+	}
216
+
217
+	/**
218
+	 * Sets the given method to the request
219
+	 *
220
+	 * @param string $method
221
+	 *
222
+	 * @return void
223
+	 *
224
+	 * @throws InvalidArgumentException
225
+	 *         If the method isn't valid.
226
+	 */
227
+	final protected function setMethod($method): void
228
+	{
229
+		$this->validateMethod($method);
230
+
231
+		$this->method = $method;
232
+	}
233
+
234
+	/**
235
+	 * Sets the given URI to the request
236
+	 *
237
+	 * @param mixed $uri
238
+	 * @param bool $preserveHost
239
+	 *
240
+	 * @return void
241
+	 *
242
+	 * @throws InvalidArgumentException
243
+	 *         If the URI isn't valid.
244
+	 */
245
+	final protected function setUri($uri, $preserveHost = false): void
246
+	{
247
+		$this->uri = Uri::create($uri);
248
+
249
+		if ($preserveHost && $this->hasHeader('Host')) {
250
+			return;
251
+		}
252
+
253
+		$host = $this->uri->getHost();
254
+		if ($host === '') {
255
+			return;
256
+		}
257
+
258
+		$port = $this->uri->getPort();
259
+		if (isset($port)) {
260
+			$host .= ':' . $port;
261
+		}
262
+
263
+		$this->setHeader('Host', $host, true);
264
+	}
265
+
266
+	/**
267
+	 * Sets the given request target to the request
268
+	 *
269
+	 * @param mixed $requestTarget
270
+	 *
271
+	 * @return void
272
+	 *
273
+	 * @throws InvalidArgumentException
274
+	 *         If the request target isn't valid.
275
+	 */
276
+	final protected function setRequestTarget($requestTarget): void
277
+	{
278
+		$this->validateRequestTarget($requestTarget);
279
+
280
+		/** @var string $requestTarget */
281
+
282
+		$this->requestTarget = $requestTarget;
283
+	}
284
+
285
+	/**
286
+	 * Validates the given method
287
+	 *
288
+	 * @link https://tools.ietf.org/html/rfc7230#section-3.1.1
289
+	 *
290
+	 * @param mixed $method
291
+	 *
292
+	 * @return void
293
+	 *
294
+	 * @throws InvalidArgumentException
295
+	 *         If the method isn't valid.
296
+	 */
297
+	private function validateMethod($method): void
298
+	{
299
+		if ('' === $method) {
300
+			throw new InvalidArgumentException('HTTP method cannot be an empty');
301
+		}
302
+
303
+		if (!is_string($method)) {
304
+			throw new InvalidArgumentException('HTTP method must be a string');
305
+		}
306
+
307
+		if (!preg_match(Header::RFC7230_VALID_TOKEN, $method)) {
308
+			throw new InvalidArgumentException('Invalid HTTP method');
309
+		}
310
+	}
311
+
312
+	/**
313
+	 * Validates the given request target
314
+	 *
315
+	 * @param mixed $requestTarget
316
+	 *
317
+	 * @return void
318
+	 *
319
+	 * @throws InvalidArgumentException
320
+	 *         If the request target isn't valid.
321
+	 */
322
+	private function validateRequestTarget($requestTarget): void
323
+	{
324
+		if ('' === $requestTarget) {
325
+			throw new InvalidArgumentException('HTTP request target cannot be an empty');
326
+		}
327
+
328
+		if (!is_string($requestTarget)) {
329
+			throw new InvalidArgumentException('HTTP request target must be a string');
330
+		}
331
+
332
+		if (!preg_match(self::RFC7230_VALID_REQUEST_TARGET, $requestTarget)) {
333
+			throw new InvalidArgumentException('Invalid HTTP request target');
334
+		}
335
+	}
336 336
 }
Please login to merge, or discard this patch.
src/Uri/Component/UserInfo.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -19,48 +19,48 @@
 block discarded – undo
19 19
 final class UserInfo implements ComponentInterface
20 20
 {
21 21
 
22
-    /**
23
-     * URI component "user"
24
-     *
25
-     * @var User
26
-     */
27
-    private User $user;
22
+	/**
23
+	 * URI component "user"
24
+	 *
25
+	 * @var User
26
+	 */
27
+	private User $user;
28 28
 
29
-    /**
30
-     * URI component "password"
31
-     *
32
-     * @var Password|null
33
-     */
34
-    private ?Password $password = null;
29
+	/**
30
+	 * URI component "password"
31
+	 *
32
+	 * @var Password|null
33
+	 */
34
+	private ?Password $password = null;
35 35
 
36
-    /**
37
-     * Constructor of the class
38
-     *
39
-     * @param mixed $user
40
-     * @param mixed $password
41
-     */
42
-    public function __construct($user, $password = null)
43
-    {
44
-        $this->user = User::create($user);
36
+	/**
37
+	 * Constructor of the class
38
+	 *
39
+	 * @param mixed $user
40
+	 * @param mixed $password
41
+	 */
42
+	public function __construct($user, $password = null)
43
+	{
44
+		$this->user = User::create($user);
45 45
 
46
-        if (isset($password)) {
47
-            $this->password = Password::create($password);
48
-        }
49
-    }
46
+		if (isset($password)) {
47
+			$this->password = Password::create($password);
48
+		}
49
+	}
50 50
 
51
-    /**
52
-     * {@inheritdoc}
53
-     *
54
-     * @return string
55
-     */
56
-    public function getValue(): string
57
-    {
58
-        $value = $this->user->getValue();
51
+	/**
52
+	 * {@inheritdoc}
53
+	 *
54
+	 * @return string
55
+	 */
56
+	public function getValue(): string
57
+	{
58
+		$value = $this->user->getValue();
59 59
 
60
-        if (isset($this->password)) {
61
-            $value .= ':' . $this->password->getValue();
62
-        }
60
+		if (isset($this->password)) {
61
+			$value .= ':' . $this->password->getValue();
62
+		}
63 63
 
64
-        return $value;
65
-    }
64
+		return $value;
65
+	}
66 66
 }
Please login to merge, or discard this patch.
src/Uri/Component/User.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -31,70 +31,70 @@
 block discarded – undo
31 31
 final class User implements ComponentInterface
32 32
 {
33 33
 
34
-    /**
35
-     * Regular expression to normalize the component value
36
-     *
37
-     * @var string
38
-     */
39
-    private const NORMALIZE_REGEX = '/(?:(?:%[0-9A-Fa-f]{2}|[0-9A-Za-z\-\._~\!\$&\'\(\)\*\+,;\=]+)|(.?))/u';
34
+	/**
35
+	 * Regular expression to normalize the component value
36
+	 *
37
+	 * @var string
38
+	 */
39
+	private const NORMALIZE_REGEX = '/(?:(?:%[0-9A-Fa-f]{2}|[0-9A-Za-z\-\._~\!\$&\'\(\)\*\+,;\=]+)|(.?))/u';
40 40
 
41
-    /**
42
-     * The component value
43
-     *
44
-     * @var string
45
-     */
46
-    private string $value = '';
41
+	/**
42
+	 * The component value
43
+	 *
44
+	 * @var string
45
+	 */
46
+	private string $value = '';
47 47
 
48
-    /**
49
-     * Constructor of the class
50
-     *
51
-     * @param mixed $value
52
-     *
53
-     * @throws InvalidUriComponentException
54
-     *         If the component isn't valid.
55
-     */
56
-    public function __construct($value)
57
-    {
58
-        if ($value === '') {
59
-            return;
60
-        }
48
+	/**
49
+	 * Constructor of the class
50
+	 *
51
+	 * @param mixed $value
52
+	 *
53
+	 * @throws InvalidUriComponentException
54
+	 *         If the component isn't valid.
55
+	 */
56
+	public function __construct($value)
57
+	{
58
+		if ($value === '') {
59
+			return;
60
+		}
61 61
 
62
-        if (!is_string($value)) {
63
-            throw new InvalidUriComponentException('URI component "user" must be a string');
64
-        }
62
+		if (!is_string($value)) {
63
+			throw new InvalidUriComponentException('URI component "user" must be a string');
64
+		}
65 65
 
66
-        $this->value = preg_replace_callback(self::NORMALIZE_REGEX, function (array $match): string {
67
-            /** @var array{0: string, 1?: string} $match */
66
+		$this->value = preg_replace_callback(self::NORMALIZE_REGEX, function (array $match): string {
67
+			/** @var array{0: string, 1?: string} $match */
68 68
 
69
-            return isset($match[1]) ? rawurlencode($match[1]) : $match[0];
70
-        }, $value);
71
-    }
69
+			return isset($match[1]) ? rawurlencode($match[1]) : $match[0];
70
+		}, $value);
71
+	}
72 72
 
73
-    /**
74
-     * Creates a user component
75
-     *
76
-     * @param mixed $user
77
-     *
78
-     * @return User
79
-     *
80
-     * @throws InvalidUriComponentException
81
-     */
82
-    public static function create($user): User
83
-    {
84
-        if ($user instanceof User) {
85
-            return $user;
86
-        }
73
+	/**
74
+	 * Creates a user component
75
+	 *
76
+	 * @param mixed $user
77
+	 *
78
+	 * @return User
79
+	 *
80
+	 * @throws InvalidUriComponentException
81
+	 */
82
+	public static function create($user): User
83
+	{
84
+		if ($user instanceof User) {
85
+			return $user;
86
+		}
87 87
 
88
-        return new User($user);
89
-    }
88
+		return new User($user);
89
+	}
90 90
 
91
-    /**
92
-     * {@inheritdoc}
93
-     *
94
-     * @return string
95
-     */
96
-    public function getValue(): string
97
-    {
98
-        return $this->value;
99
-    }
91
+	/**
92
+	 * {@inheritdoc}
93
+	 *
94
+	 * @return string
95
+	 */
96
+	public function getValue(): string
97
+	{
98
+		return $this->value;
99
+	}
100 100
 }
Please login to merge, or discard this patch.
src/Uri/Component/Password.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -31,70 +31,70 @@
 block discarded – undo
31 31
 final class Password implements ComponentInterface
32 32
 {
33 33
 
34
-    /**
35
-     * Regular expression to normalize the component value
36
-     *
37
-     * @var string
38
-     */
39
-    private const NORMALIZE_REGEX = '/(?:(?:%[0-9A-Fa-f]{2}|[0-9A-Za-z\-\._~\!\$&\'\(\)\*\+,;\=]+)|(.?))/u';
34
+	/**
35
+	 * Regular expression to normalize the component value
36
+	 *
37
+	 * @var string
38
+	 */
39
+	private const NORMALIZE_REGEX = '/(?:(?:%[0-9A-Fa-f]{2}|[0-9A-Za-z\-\._~\!\$&\'\(\)\*\+,;\=]+)|(.?))/u';
40 40
 
41
-    /**
42
-     * The component value
43
-     *
44
-     * @var string
45
-     */
46
-    private string $value = '';
41
+	/**
42
+	 * The component value
43
+	 *
44
+	 * @var string
45
+	 */
46
+	private string $value = '';
47 47
 
48
-    /**
49
-     * Constructor of the class
50
-     *
51
-     * @param mixed $value
52
-     *
53
-     * @throws InvalidUriComponentException
54
-     *         If the component isn't valid.
55
-     */
56
-    public function __construct($value)
57
-    {
58
-        if ($value === '') {
59
-            return;
60
-        }
48
+	/**
49
+	 * Constructor of the class
50
+	 *
51
+	 * @param mixed $value
52
+	 *
53
+	 * @throws InvalidUriComponentException
54
+	 *         If the component isn't valid.
55
+	 */
56
+	public function __construct($value)
57
+	{
58
+		if ($value === '') {
59
+			return;
60
+		}
61 61
 
62
-        if (!is_string($value)) {
63
-            throw new InvalidUriComponentException('URI component "password" must be a string');
64
-        }
62
+		if (!is_string($value)) {
63
+			throw new InvalidUriComponentException('URI component "password" must be a string');
64
+		}
65 65
 
66
-        $this->value = preg_replace_callback(self::NORMALIZE_REGEX, function (array $match): string {
67
-            /** @var array{0: string, 1?: string} $match */
66
+		$this->value = preg_replace_callback(self::NORMALIZE_REGEX, function (array $match): string {
67
+			/** @var array{0: string, 1?: string} $match */
68 68
 
69
-            return isset($match[1]) ? rawurlencode($match[1]) : $match[0];
70
-        }, $value);
71
-    }
69
+			return isset($match[1]) ? rawurlencode($match[1]) : $match[0];
70
+		}, $value);
71
+	}
72 72
 
73
-    /**
74
-     * Creates a password component
75
-     *
76
-     * @param mixed $password
77
-     *
78
-     * @return Password
79
-     *
80
-     * @throws InvalidUriComponentException
81
-     */
82
-    public static function create($password): Password
83
-    {
84
-        if ($password instanceof Password) {
85
-            return $password;
86
-        }
73
+	/**
74
+	 * Creates a password component
75
+	 *
76
+	 * @param mixed $password
77
+	 *
78
+	 * @return Password
79
+	 *
80
+	 * @throws InvalidUriComponentException
81
+	 */
82
+	public static function create($password): Password
83
+	{
84
+		if ($password instanceof Password) {
85
+			return $password;
86
+		}
87 87
 
88
-        return new Password($password);
89
-    }
88
+		return new Password($password);
89
+	}
90 90
 
91
-    /**
92
-     * {@inheritdoc}
93
-     *
94
-     * @return string
95
-     */
96
-    public function getValue(): string
97
-    {
98
-        return $this->value;
99
-    }
91
+	/**
92
+	 * {@inheritdoc}
93
+	 *
94
+	 * @return string
95
+	 */
96
+	public function getValue(): string
97
+	{
98
+		return $this->value;
99
+	}
100 100
 }
Please login to merge, or discard this patch.