@@ -29,68 +29,68 @@ |
||
29 | 29 | |
30 | 30 | class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface |
31 | 31 | { |
32 | - public function createRequest(string $method, $uri): RequestInterface |
|
33 | - { |
|
34 | - if (is_string($uri)) { |
|
35 | - $uri = $this->createUri($uri); |
|
36 | - } |
|
37 | - |
|
38 | - return new Request($method, $uri); |
|
39 | - } |
|
40 | - |
|
41 | - public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
42 | - { |
|
43 | - if (2 > func_num_args()) { |
|
44 | - $reasonPhrase = null; |
|
45 | - } |
|
46 | - |
|
47 | - return new Response($code, [], null, '1.1', $reasonPhrase); |
|
48 | - } |
|
49 | - |
|
50 | - public function createStream(string $content = ''): StreamInterface |
|
51 | - { |
|
52 | - return Stream::create($content); |
|
53 | - } |
|
54 | - |
|
55 | - public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
|
56 | - { |
|
57 | - $resource = @fopen($filename, $mode); |
|
58 | - if (false === $resource) { |
|
59 | - if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) { |
|
60 | - throw new InvalidArgumentException('The mode ' . $mode . ' is invalid.'); |
|
61 | - } |
|
62 | - |
|
63 | - throw new RuntimeException('The file ' . $filename . ' cannot be opened.'); |
|
64 | - } |
|
65 | - |
|
66 | - return Stream::create($resource); |
|
67 | - } |
|
68 | - |
|
69 | - public function createStreamFromResource($resource): StreamInterface |
|
70 | - { |
|
71 | - return Stream::create($resource); |
|
72 | - } |
|
73 | - |
|
74 | - public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface |
|
75 | - { |
|
76 | - if (null === $size) { |
|
77 | - $size = $stream->getSize(); |
|
78 | - } |
|
79 | - |
|
80 | - return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
81 | - } |
|
82 | - |
|
83 | - public function createUri(string $uri = ''): UriInterface |
|
84 | - { |
|
85 | - return new Uri($uri); |
|
86 | - } |
|
87 | - |
|
88 | - public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
89 | - { |
|
90 | - if (is_string($uri)) { |
|
91 | - $uri = $this->createUri($uri); |
|
92 | - } |
|
93 | - |
|
94 | - return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
95 | - } |
|
32 | + public function createRequest(string $method, $uri): RequestInterface |
|
33 | + { |
|
34 | + if (is_string($uri)) { |
|
35 | + $uri = $this->createUri($uri); |
|
36 | + } |
|
37 | + |
|
38 | + return new Request($method, $uri); |
|
39 | + } |
|
40 | + |
|
41 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
42 | + { |
|
43 | + if (2 > func_num_args()) { |
|
44 | + $reasonPhrase = null; |
|
45 | + } |
|
46 | + |
|
47 | + return new Response($code, [], null, '1.1', $reasonPhrase); |
|
48 | + } |
|
49 | + |
|
50 | + public function createStream(string $content = ''): StreamInterface |
|
51 | + { |
|
52 | + return Stream::create($content); |
|
53 | + } |
|
54 | + |
|
55 | + public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
|
56 | + { |
|
57 | + $resource = @fopen($filename, $mode); |
|
58 | + if (false === $resource) { |
|
59 | + if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) { |
|
60 | + throw new InvalidArgumentException('The mode ' . $mode . ' is invalid.'); |
|
61 | + } |
|
62 | + |
|
63 | + throw new RuntimeException('The file ' . $filename . ' cannot be opened.'); |
|
64 | + } |
|
65 | + |
|
66 | + return Stream::create($resource); |
|
67 | + } |
|
68 | + |
|
69 | + public function createStreamFromResource($resource): StreamInterface |
|
70 | + { |
|
71 | + return Stream::create($resource); |
|
72 | + } |
|
73 | + |
|
74 | + public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface |
|
75 | + { |
|
76 | + if (null === $size) { |
|
77 | + $size = $stream->getSize(); |
|
78 | + } |
|
79 | + |
|
80 | + return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
81 | + } |
|
82 | + |
|
83 | + public function createUri(string $uri = ''): UriInterface |
|
84 | + { |
|
85 | + return new Uri($uri); |
|
86 | + } |
|
87 | + |
|
88 | + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
89 | + { |
|
90 | + if (is_string($uri)) { |
|
91 | + $uri = $this->createUri($uri); |
|
92 | + } |
|
93 | + |
|
94 | + return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
95 | + } |
|
96 | 96 | } |
97 | 97 | \ No newline at end of file |
@@ -57,10 +57,10 @@ |
||
57 | 57 | $resource = @fopen($filename, $mode); |
58 | 58 | if (false === $resource) { |
59 | 59 | if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) { |
60 | - throw new InvalidArgumentException('The mode ' . $mode . ' is invalid.'); |
|
60 | + throw new InvalidArgumentException('The mode '.$mode.' is invalid.'); |
|
61 | 61 | } |
62 | 62 | |
63 | - throw new RuntimeException('The file ' . $filename . ' cannot be opened.'); |
|
63 | + throw new RuntimeException('The file '.$filename.' cannot be opened.'); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | return Stream::create($resource); |
@@ -14,28 +14,28 @@ |
||
14 | 14 | |
15 | 15 | class HttplugFactory implements MessageFactory, StreamFactory, UriFactory |
16 | 16 | { |
17 | - public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') |
|
18 | - { |
|
19 | - $uri = $this->createUri($uri); |
|
20 | - return new Request($method, $uri, $headers, $body, $protocolVersion); |
|
21 | - } |
|
17 | + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') |
|
18 | + { |
|
19 | + $uri = $this->createUri($uri); |
|
20 | + return new Request($method, $uri, $headers, $body, $protocolVersion); |
|
21 | + } |
|
22 | 22 | |
23 | - public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $version = '1.1') |
|
24 | - { |
|
25 | - return new Response((int)$statusCode, $headers, $body, $version, $reasonPhrase); |
|
26 | - } |
|
23 | + public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $version = '1.1') |
|
24 | + { |
|
25 | + return new Response((int)$statusCode, $headers, $body, $version, $reasonPhrase); |
|
26 | + } |
|
27 | 27 | |
28 | - public function createStream($body = null) |
|
29 | - { |
|
30 | - return Stream::create($body ?? ''); |
|
31 | - } |
|
28 | + public function createStream($body = null) |
|
29 | + { |
|
30 | + return Stream::create($body ?? ''); |
|
31 | + } |
|
32 | 32 | |
33 | - public function createUri($uri = ''): UriInterface |
|
34 | - { |
|
35 | - if ($uri instanceof UriInterface) { |
|
36 | - return $uri; |
|
37 | - } |
|
33 | + public function createUri($uri = ''): UriInterface |
|
34 | + { |
|
35 | + if ($uri instanceof UriInterface) { |
|
36 | + return $uri; |
|
37 | + } |
|
38 | 38 | |
39 | - return new Uri($uri); |
|
40 | - } |
|
39 | + return new Uri($uri); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | \ No newline at end of file |
@@ -22,7 +22,7 @@ |
||
22 | 22 | |
23 | 23 | public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $version = '1.1') |
24 | 24 | { |
25 | - return new Response((int)$statusCode, $headers, $body, $version, $reasonPhrase); |
|
25 | + return new Response((int) $statusCode, $headers, $body, $version, $reasonPhrase); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | public function createStream($body = null) |
@@ -12,235 +12,235 @@ |
||
12 | 12 | |
13 | 13 | class Uri implements UriInterface |
14 | 14 | { |
15 | - use LowercaseTrait; |
|
16 | - |
|
17 | - protected const SCHEMES = ['http' => 80, 'https' => 443]; |
|
18 | - protected const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; |
|
19 | - protected const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
|
20 | - |
|
21 | - protected string $scheme = ''; |
|
22 | - protected string $userInfo = ''; |
|
23 | - protected string $host = ''; |
|
24 | - |
|
25 | - protected ?int $port = null; |
|
26 | - protected string $path = ''; |
|
27 | - protected string $query = ''; |
|
28 | - protected string $fragment = ''; |
|
29 | - |
|
30 | - public function __construct(string $uri = '') |
|
31 | - { |
|
32 | - if ('' !== $uri) { |
|
33 | - $parts = parse_url($uri); |
|
34 | - if ($parts === false) { |
|
35 | - throw new InvalidArgumentException("Unable to parse URI: $uri"); |
|
36 | - } |
|
37 | - |
|
38 | - $this->withScheme($parts['scheme'] ?? ''); |
|
39 | - $this->userInfo = $parts['user'] ?? ''; |
|
40 | - $this->withHost($parts['host'] ?? ''); |
|
41 | - $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; |
|
42 | - $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
|
43 | - $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
|
44 | - $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
|
45 | - if (isset($parts['pass'])) { |
|
46 | - $this->userInfo .= ':' . $parts['pass']; |
|
47 | - } |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - public function __toString(): string |
|
52 | - { |
|
53 | - return self::createUriString($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
|
54 | - } |
|
55 | - |
|
56 | - public function getScheme(): string |
|
57 | - { |
|
58 | - return $this->scheme; |
|
59 | - } |
|
60 | - |
|
61 | - public function getAuthority(): string |
|
62 | - { |
|
63 | - if ('' === $this->host) { |
|
64 | - return ''; |
|
65 | - } |
|
66 | - |
|
67 | - $authority = $this->host; |
|
68 | - if ('' !== $this->userInfo) { |
|
69 | - $authority = $this->userInfo . '@' . $authority; |
|
70 | - } |
|
71 | - |
|
72 | - if (null !== $this->port) { |
|
73 | - $authority .= ':' . $this->port; |
|
74 | - } |
|
75 | - |
|
76 | - return $authority; |
|
77 | - } |
|
78 | - |
|
79 | - public function getUserInfo(): string |
|
80 | - { |
|
81 | - return $this->userInfo; |
|
82 | - } |
|
83 | - |
|
84 | - public function getHost(): string |
|
85 | - { |
|
86 | - return $this->host; |
|
87 | - } |
|
88 | - |
|
89 | - public function getPort(): ?int |
|
90 | - { |
|
91 | - return $this->port; |
|
92 | - } |
|
93 | - |
|
94 | - public function getPath(): string |
|
95 | - { |
|
96 | - return $this->path; |
|
97 | - } |
|
98 | - |
|
99 | - public function getQuery(): string |
|
100 | - { |
|
101 | - return $this->query; |
|
102 | - } |
|
103 | - |
|
104 | - public function getFragment(): string |
|
105 | - { |
|
106 | - return $this->fragment; |
|
107 | - } |
|
108 | - |
|
109 | - public function withScheme($scheme): self |
|
110 | - { |
|
111 | - $this->scheme = self::lowercase($scheme); |
|
112 | - $this->port = $this->port ? $this->filterPort($this->port) : null; |
|
113 | - |
|
114 | - return $this; |
|
115 | - } |
|
116 | - |
|
117 | - public function withUserInfo($user, $password = null): self |
|
118 | - { |
|
119 | - $info = $user; |
|
120 | - if (null !== $password && '' !== $password) { |
|
121 | - $info .= ':' . $password; |
|
122 | - } |
|
123 | - |
|
124 | - $this->userInfo = $info; |
|
125 | - |
|
126 | - return $this; |
|
127 | - } |
|
128 | - |
|
129 | - public function withHost($host): self |
|
130 | - { |
|
131 | - $this->host = self::lowercase($host); |
|
132 | - return $this; |
|
133 | - } |
|
134 | - |
|
135 | - public function withPort($port): self |
|
136 | - { |
|
137 | - $this->port = $port === null ? null : $this->filterPort($port); |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - public function withPath($path): self |
|
142 | - { |
|
143 | - $path = $this->filterPath($path); |
|
144 | - if ($this->path !== $path) { |
|
145 | - $this->path = $path; |
|
146 | - } |
|
147 | - |
|
148 | - return $this; |
|
149 | - } |
|
150 | - |
|
151 | - public function withQuery($query): self |
|
152 | - { |
|
153 | - $query = $this->filterQueryAndFragment($query); |
|
154 | - if ($this->query !== $query) { |
|
155 | - $this->query = $query; |
|
156 | - } |
|
157 | - |
|
158 | - return $this; |
|
159 | - } |
|
160 | - |
|
161 | - public function withFragment($fragment): self |
|
162 | - { |
|
163 | - $fragment = $this->filterQueryAndFragment($fragment); |
|
164 | - if ($this->fragment !== $fragment) { |
|
165 | - $this->fragment = $fragment; |
|
166 | - } |
|
167 | - |
|
168 | - return $this; |
|
169 | - } |
|
170 | - |
|
171 | - private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string |
|
172 | - { |
|
173 | - $uri = ''; |
|
174 | - if ('' !== $scheme) { |
|
175 | - $uri .= $scheme . ':'; |
|
176 | - } |
|
177 | - |
|
178 | - if ('' !== $authority) { |
|
179 | - $uri .= '//' . $authority; |
|
180 | - } |
|
181 | - |
|
182 | - if ('' !== $path) { |
|
183 | - if ('/' !== $path[0]) { |
|
184 | - if ('' !== $authority) { |
|
185 | - // If the path is rootless and an authority is present, the path MUST be prefixed by "/" |
|
186 | - $path = '/' . $path; |
|
187 | - } |
|
188 | - } elseif (isset($path[1]) && '/' === $path[1]) { |
|
189 | - if ('' === $authority) { |
|
190 | - // If the path is starting with more than one "/" and no authority is present, the |
|
191 | - // starting slashes MUST be reduced to one. |
|
192 | - $path = '/' . \ltrim($path, '/'); |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - $uri .= $path; |
|
197 | - } |
|
198 | - |
|
199 | - if ('' !== $query) { |
|
200 | - $uri .= '?' . $query; |
|
201 | - } |
|
202 | - |
|
203 | - if ('' !== $fragment) { |
|
204 | - $uri .= '#' . $fragment; |
|
205 | - } |
|
206 | - |
|
207 | - return $uri; |
|
208 | - } |
|
209 | - |
|
210 | - private static function isNonStandardPort(string $scheme, int $port): bool |
|
211 | - { |
|
212 | - return !isset(self::SCHEMES[$scheme]) || $port !== self::SCHEMES[$scheme]; |
|
213 | - } |
|
214 | - |
|
215 | - protected function filterPort(int $port): ?int |
|
216 | - { |
|
217 | - if ($port < 1 || $port > 65535) { |
|
218 | - throw new InvalidArgumentException(sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
|
219 | - } |
|
220 | - |
|
221 | - return self::isNonStandardPort($this->scheme, $port) ? $port : null; |
|
222 | - } |
|
223 | - |
|
224 | - private function filterPath($path): string |
|
225 | - { |
|
226 | - if (!is_string($path)) { |
|
227 | - throw new InvalidArgumentException('Path must be a string'); |
|
228 | - } |
|
229 | - |
|
230 | - return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); |
|
231 | - } |
|
232 | - |
|
233 | - private function filterQueryAndFragment($str): string |
|
234 | - { |
|
235 | - if (!is_string($str)) { |
|
236 | - throw new InvalidArgumentException('Query and fragment must be a string'); |
|
237 | - } |
|
238 | - |
|
239 | - return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); |
|
240 | - } |
|
241 | - |
|
242 | - private static function rawUrlEncodeMatchZero(array $match): string |
|
243 | - { |
|
244 | - return rawurlencode($match[0]); |
|
245 | - } |
|
15 | + use LowercaseTrait; |
|
16 | + |
|
17 | + protected const SCHEMES = ['http' => 80, 'https' => 443]; |
|
18 | + protected const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; |
|
19 | + protected const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
|
20 | + |
|
21 | + protected string $scheme = ''; |
|
22 | + protected string $userInfo = ''; |
|
23 | + protected string $host = ''; |
|
24 | + |
|
25 | + protected ?int $port = null; |
|
26 | + protected string $path = ''; |
|
27 | + protected string $query = ''; |
|
28 | + protected string $fragment = ''; |
|
29 | + |
|
30 | + public function __construct(string $uri = '') |
|
31 | + { |
|
32 | + if ('' !== $uri) { |
|
33 | + $parts = parse_url($uri); |
|
34 | + if ($parts === false) { |
|
35 | + throw new InvalidArgumentException("Unable to parse URI: $uri"); |
|
36 | + } |
|
37 | + |
|
38 | + $this->withScheme($parts['scheme'] ?? ''); |
|
39 | + $this->userInfo = $parts['user'] ?? ''; |
|
40 | + $this->withHost($parts['host'] ?? ''); |
|
41 | + $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; |
|
42 | + $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
|
43 | + $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
|
44 | + $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
|
45 | + if (isset($parts['pass'])) { |
|
46 | + $this->userInfo .= ':' . $parts['pass']; |
|
47 | + } |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + public function __toString(): string |
|
52 | + { |
|
53 | + return self::createUriString($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
|
54 | + } |
|
55 | + |
|
56 | + public function getScheme(): string |
|
57 | + { |
|
58 | + return $this->scheme; |
|
59 | + } |
|
60 | + |
|
61 | + public function getAuthority(): string |
|
62 | + { |
|
63 | + if ('' === $this->host) { |
|
64 | + return ''; |
|
65 | + } |
|
66 | + |
|
67 | + $authority = $this->host; |
|
68 | + if ('' !== $this->userInfo) { |
|
69 | + $authority = $this->userInfo . '@' . $authority; |
|
70 | + } |
|
71 | + |
|
72 | + if (null !== $this->port) { |
|
73 | + $authority .= ':' . $this->port; |
|
74 | + } |
|
75 | + |
|
76 | + return $authority; |
|
77 | + } |
|
78 | + |
|
79 | + public function getUserInfo(): string |
|
80 | + { |
|
81 | + return $this->userInfo; |
|
82 | + } |
|
83 | + |
|
84 | + public function getHost(): string |
|
85 | + { |
|
86 | + return $this->host; |
|
87 | + } |
|
88 | + |
|
89 | + public function getPort(): ?int |
|
90 | + { |
|
91 | + return $this->port; |
|
92 | + } |
|
93 | + |
|
94 | + public function getPath(): string |
|
95 | + { |
|
96 | + return $this->path; |
|
97 | + } |
|
98 | + |
|
99 | + public function getQuery(): string |
|
100 | + { |
|
101 | + return $this->query; |
|
102 | + } |
|
103 | + |
|
104 | + public function getFragment(): string |
|
105 | + { |
|
106 | + return $this->fragment; |
|
107 | + } |
|
108 | + |
|
109 | + public function withScheme($scheme): self |
|
110 | + { |
|
111 | + $this->scheme = self::lowercase($scheme); |
|
112 | + $this->port = $this->port ? $this->filterPort($this->port) : null; |
|
113 | + |
|
114 | + return $this; |
|
115 | + } |
|
116 | + |
|
117 | + public function withUserInfo($user, $password = null): self |
|
118 | + { |
|
119 | + $info = $user; |
|
120 | + if (null !== $password && '' !== $password) { |
|
121 | + $info .= ':' . $password; |
|
122 | + } |
|
123 | + |
|
124 | + $this->userInfo = $info; |
|
125 | + |
|
126 | + return $this; |
|
127 | + } |
|
128 | + |
|
129 | + public function withHost($host): self |
|
130 | + { |
|
131 | + $this->host = self::lowercase($host); |
|
132 | + return $this; |
|
133 | + } |
|
134 | + |
|
135 | + public function withPort($port): self |
|
136 | + { |
|
137 | + $this->port = $port === null ? null : $this->filterPort($port); |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + public function withPath($path): self |
|
142 | + { |
|
143 | + $path = $this->filterPath($path); |
|
144 | + if ($this->path !== $path) { |
|
145 | + $this->path = $path; |
|
146 | + } |
|
147 | + |
|
148 | + return $this; |
|
149 | + } |
|
150 | + |
|
151 | + public function withQuery($query): self |
|
152 | + { |
|
153 | + $query = $this->filterQueryAndFragment($query); |
|
154 | + if ($this->query !== $query) { |
|
155 | + $this->query = $query; |
|
156 | + } |
|
157 | + |
|
158 | + return $this; |
|
159 | + } |
|
160 | + |
|
161 | + public function withFragment($fragment): self |
|
162 | + { |
|
163 | + $fragment = $this->filterQueryAndFragment($fragment); |
|
164 | + if ($this->fragment !== $fragment) { |
|
165 | + $this->fragment = $fragment; |
|
166 | + } |
|
167 | + |
|
168 | + return $this; |
|
169 | + } |
|
170 | + |
|
171 | + private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string |
|
172 | + { |
|
173 | + $uri = ''; |
|
174 | + if ('' !== $scheme) { |
|
175 | + $uri .= $scheme . ':'; |
|
176 | + } |
|
177 | + |
|
178 | + if ('' !== $authority) { |
|
179 | + $uri .= '//' . $authority; |
|
180 | + } |
|
181 | + |
|
182 | + if ('' !== $path) { |
|
183 | + if ('/' !== $path[0]) { |
|
184 | + if ('' !== $authority) { |
|
185 | + // If the path is rootless and an authority is present, the path MUST be prefixed by "/" |
|
186 | + $path = '/' . $path; |
|
187 | + } |
|
188 | + } elseif (isset($path[1]) && '/' === $path[1]) { |
|
189 | + if ('' === $authority) { |
|
190 | + // If the path is starting with more than one "/" and no authority is present, the |
|
191 | + // starting slashes MUST be reduced to one. |
|
192 | + $path = '/' . \ltrim($path, '/'); |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + $uri .= $path; |
|
197 | + } |
|
198 | + |
|
199 | + if ('' !== $query) { |
|
200 | + $uri .= '?' . $query; |
|
201 | + } |
|
202 | + |
|
203 | + if ('' !== $fragment) { |
|
204 | + $uri .= '#' . $fragment; |
|
205 | + } |
|
206 | + |
|
207 | + return $uri; |
|
208 | + } |
|
209 | + |
|
210 | + private static function isNonStandardPort(string $scheme, int $port): bool |
|
211 | + { |
|
212 | + return !isset(self::SCHEMES[$scheme]) || $port !== self::SCHEMES[$scheme]; |
|
213 | + } |
|
214 | + |
|
215 | + protected function filterPort(int $port): ?int |
|
216 | + { |
|
217 | + if ($port < 1 || $port > 65535) { |
|
218 | + throw new InvalidArgumentException(sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
|
219 | + } |
|
220 | + |
|
221 | + return self::isNonStandardPort($this->scheme, $port) ? $port : null; |
|
222 | + } |
|
223 | + |
|
224 | + private function filterPath($path): string |
|
225 | + { |
|
226 | + if (!is_string($path)) { |
|
227 | + throw new InvalidArgumentException('Path must be a string'); |
|
228 | + } |
|
229 | + |
|
230 | + return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); |
|
231 | + } |
|
232 | + |
|
233 | + private function filterQueryAndFragment($str): string |
|
234 | + { |
|
235 | + if (!is_string($str)) { |
|
236 | + throw new InvalidArgumentException('Query and fragment must be a string'); |
|
237 | + } |
|
238 | + |
|
239 | + return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); |
|
240 | + } |
|
241 | + |
|
242 | + private static function rawUrlEncodeMatchZero(array $match): string |
|
243 | + { |
|
244 | + return rawurlencode($match[0]); |
|
245 | + } |
|
246 | 246 | } |
247 | 247 | \ No newline at end of file |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
44 | 44 | $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
45 | 45 | if (isset($parts['pass'])) { |
46 | - $this->userInfo .= ':' . $parts['pass']; |
|
46 | + $this->userInfo .= ':'.$parts['pass']; |
|
47 | 47 | } |
48 | 48 | } |
49 | 49 | } |
@@ -66,11 +66,11 @@ discard block |
||
66 | 66 | |
67 | 67 | $authority = $this->host; |
68 | 68 | if ('' !== $this->userInfo) { |
69 | - $authority = $this->userInfo . '@' . $authority; |
|
69 | + $authority = $this->userInfo.'@'.$authority; |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | if (null !== $this->port) { |
73 | - $authority .= ':' . $this->port; |
|
73 | + $authority .= ':'.$this->port; |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | return $authority; |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | { |
119 | 119 | $info = $user; |
120 | 120 | if (null !== $password && '' !== $password) { |
121 | - $info .= ':' . $password; |
|
121 | + $info .= ':'.$password; |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | $this->userInfo = $info; |
@@ -172,24 +172,24 @@ discard block |
||
172 | 172 | { |
173 | 173 | $uri = ''; |
174 | 174 | if ('' !== $scheme) { |
175 | - $uri .= $scheme . ':'; |
|
175 | + $uri .= $scheme.':'; |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | if ('' !== $authority) { |
179 | - $uri .= '//' . $authority; |
|
179 | + $uri .= '//'.$authority; |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | if ('' !== $path) { |
183 | 183 | if ('/' !== $path[0]) { |
184 | 184 | if ('' !== $authority) { |
185 | 185 | // If the path is rootless and an authority is present, the path MUST be prefixed by "/" |
186 | - $path = '/' . $path; |
|
186 | + $path = '/'.$path; |
|
187 | 187 | } |
188 | 188 | } elseif (isset($path[1]) && '/' === $path[1]) { |
189 | 189 | if ('' === $authority) { |
190 | 190 | // If the path is starting with more than one "/" and no authority is present, the |
191 | 191 | // starting slashes MUST be reduced to one. |
192 | - $path = '/' . \ltrim($path, '/'); |
|
192 | + $path = '/'.\ltrim($path, '/'); |
|
193 | 193 | } |
194 | 194 | } |
195 | 195 | |
@@ -197,11 +197,11 @@ discard block |
||
197 | 197 | } |
198 | 198 | |
199 | 199 | if ('' !== $query) { |
200 | - $uri .= '?' . $query; |
|
200 | + $uri .= '?'.$query; |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | if ('' !== $fragment) { |
204 | - $uri .= '#' . $fragment; |
|
204 | + $uri .= '#'.$fragment; |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | return $uri; |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | throw new InvalidArgumentException('Path must be a string'); |
228 | 228 | } |
229 | 229 | |
230 | - return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); |
|
230 | + return preg_replace_callback('/(?:[^'.self::CHAR_UNRESERVED.self::CHAR_SUB_DELIMS.'%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | private function filterQueryAndFragment($str): string |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | throw new InvalidArgumentException('Query and fragment must be a string'); |
237 | 237 | } |
238 | 238 | |
239 | - return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); |
|
239 | + return preg_replace_callback('/(?:[^'.self::CHAR_UNRESERVED.self::CHAR_SUB_DELIMS.'%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | private static function rawUrlEncodeMatchZero(array $match): string |
@@ -7,51 +7,51 @@ |
||
7 | 7 | |
8 | 8 | trait ServerMessage |
9 | 9 | { |
10 | - protected array $attributes = []; |
|
11 | - |
|
12 | - /** |
|
13 | - * @return array |
|
14 | - */ |
|
15 | - public function getAttributes(): array |
|
16 | - { |
|
17 | - return $this->attributes; |
|
18 | - } |
|
19 | - |
|
20 | - /** |
|
21 | - * @param string $attribute |
|
22 | - * @param mixed $default |
|
23 | - * |
|
24 | - * @return mixed |
|
25 | - */ |
|
26 | - public function getAttribute($attribute, $default = null) |
|
27 | - { |
|
28 | - if (false === array_key_exists($attribute, $this->attributes)) { |
|
29 | - return $default; |
|
30 | - } |
|
31 | - |
|
32 | - return $this->attributes[$attribute]; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * @param string $attribute |
|
37 | - * @param mixed $value |
|
38 | - * |
|
39 | - * @return $this |
|
40 | - */ |
|
41 | - public function withAttribute($attribute, $value) |
|
42 | - { |
|
43 | - $this->attributes[$attribute] = $value; |
|
44 | - return $this; |
|
45 | - } |
|
46 | - |
|
47 | - public function withoutAttribute($attribute) |
|
48 | - { |
|
49 | - if (false === array_key_exists($attribute, $this->attributes)) { |
|
50 | - return $this; |
|
51 | - } |
|
52 | - |
|
53 | - unset($this->attributes[$attribute]); |
|
54 | - |
|
55 | - return $this; |
|
56 | - } |
|
10 | + protected array $attributes = []; |
|
11 | + |
|
12 | + /** |
|
13 | + * @return array |
|
14 | + */ |
|
15 | + public function getAttributes(): array |
|
16 | + { |
|
17 | + return $this->attributes; |
|
18 | + } |
|
19 | + |
|
20 | + /** |
|
21 | + * @param string $attribute |
|
22 | + * @param mixed $default |
|
23 | + * |
|
24 | + * @return mixed |
|
25 | + */ |
|
26 | + public function getAttribute($attribute, $default = null) |
|
27 | + { |
|
28 | + if (false === array_key_exists($attribute, $this->attributes)) { |
|
29 | + return $default; |
|
30 | + } |
|
31 | + |
|
32 | + return $this->attributes[$attribute]; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * @param string $attribute |
|
37 | + * @param mixed $value |
|
38 | + * |
|
39 | + * @return $this |
|
40 | + */ |
|
41 | + public function withAttribute($attribute, $value) |
|
42 | + { |
|
43 | + $this->attributes[$attribute] = $value; |
|
44 | + return $this; |
|
45 | + } |
|
46 | + |
|
47 | + public function withoutAttribute($attribute) |
|
48 | + { |
|
49 | + if (false === array_key_exists($attribute, $this->attributes)) { |
|
50 | + return $this; |
|
51 | + } |
|
52 | + |
|
53 | + unset($this->attributes[$attribute]); |
|
54 | + |
|
55 | + return $this; |
|
56 | + } |
|
57 | 57 | } |