@@ -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 |
@@ -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 | } |
@@ -13,83 +13,83 @@ |
||
| 13 | 13 | |
| 14 | 14 | class ServerRequest extends Request implements ServerRequestInterface, ServerMessageInterface |
| 15 | 15 | { |
| 16 | - use ServerMessage; |
|
| 17 | - |
|
| 18 | - protected array $cookieParams = []; |
|
| 19 | - |
|
| 20 | - /** @var array|object|null */ |
|
| 21 | - protected $parsedBody; |
|
| 22 | - protected array $queryParams = []; |
|
| 23 | - protected array $serverParams; |
|
| 24 | - |
|
| 25 | - /** @var UploadedFileInterface[] */ |
|
| 26 | - protected array $uploadedFiles = []; |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - public function __construct( |
|
| 30 | - string $method, |
|
| 31 | - UriInterface $uri, |
|
| 32 | - array $headers = [], |
|
| 33 | - $body = null, |
|
| 34 | - string $version = '1.1', |
|
| 35 | - array $serverParams = [] |
|
| 36 | - ) { |
|
| 37 | - parent::__construct($method, $uri, $headers, $body, $version); |
|
| 38 | - |
|
| 39 | - $this->serverParams = $serverParams; |
|
| 40 | - parse_str($uri->getQuery(), $this->queryParams); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function getServerParams(): array |
|
| 44 | - { |
|
| 45 | - return $this->serverParams; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function getUploadedFiles(): array |
|
| 49 | - { |
|
| 50 | - return $this->uploadedFiles; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function withUploadedFiles(array $uploadedFiles) |
|
| 54 | - { |
|
| 55 | - $this->uploadedFiles = $uploadedFiles; |
|
| 56 | - return $this; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function getCookieParams(): array |
|
| 60 | - { |
|
| 61 | - return $this->cookieParams; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public function withCookieParams(array $cookies) |
|
| 65 | - { |
|
| 66 | - $this->cookieParams = $cookies; |
|
| 67 | - return $this; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function getQueryParams(): array |
|
| 71 | - { |
|
| 72 | - return $this->queryParams; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function withQueryParams(array $query) |
|
| 76 | - { |
|
| 77 | - $this->queryParams = $query; |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function getParsedBody() |
|
| 82 | - { |
|
| 83 | - return $this->parsedBody; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function withParsedBody($data) |
|
| 87 | - { |
|
| 88 | - if (!is_array($data) && !is_object($data) && null !== $data) { |
|
| 89 | - throw new InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null'); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $this->parsedBody = $data; |
|
| 93 | - return $this; |
|
| 94 | - } |
|
| 16 | + use ServerMessage; |
|
| 17 | + |
|
| 18 | + protected array $cookieParams = []; |
|
| 19 | + |
|
| 20 | + /** @var array|object|null */ |
|
| 21 | + protected $parsedBody; |
|
| 22 | + protected array $queryParams = []; |
|
| 23 | + protected array $serverParams; |
|
| 24 | + |
|
| 25 | + /** @var UploadedFileInterface[] */ |
|
| 26 | + protected array $uploadedFiles = []; |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + public function __construct( |
|
| 30 | + string $method, |
|
| 31 | + UriInterface $uri, |
|
| 32 | + array $headers = [], |
|
| 33 | + $body = null, |
|
| 34 | + string $version = '1.1', |
|
| 35 | + array $serverParams = [] |
|
| 36 | + ) { |
|
| 37 | + parent::__construct($method, $uri, $headers, $body, $version); |
|
| 38 | + |
|
| 39 | + $this->serverParams = $serverParams; |
|
| 40 | + parse_str($uri->getQuery(), $this->queryParams); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function getServerParams(): array |
|
| 44 | + { |
|
| 45 | + return $this->serverParams; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function getUploadedFiles(): array |
|
| 49 | + { |
|
| 50 | + return $this->uploadedFiles; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function withUploadedFiles(array $uploadedFiles) |
|
| 54 | + { |
|
| 55 | + $this->uploadedFiles = $uploadedFiles; |
|
| 56 | + return $this; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function getCookieParams(): array |
|
| 60 | + { |
|
| 61 | + return $this->cookieParams; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public function withCookieParams(array $cookies) |
|
| 65 | + { |
|
| 66 | + $this->cookieParams = $cookies; |
|
| 67 | + return $this; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function getQueryParams(): array |
|
| 71 | + { |
|
| 72 | + return $this->queryParams; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function withQueryParams(array $query) |
|
| 76 | + { |
|
| 77 | + $this->queryParams = $query; |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function getParsedBody() |
|
| 82 | + { |
|
| 83 | + return $this->parsedBody; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function withParsedBody($data) |
|
| 87 | + { |
|
| 88 | + if (!is_array($data) && !is_object($data) && null !== $data) { |
|
| 89 | + throw new InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null'); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $this->parsedBody = $data; |
|
| 93 | + return $this; |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -14,131 +14,131 @@ |
||
| 14 | 14 | class Message implements MessageInterface |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * RFC-2616, RFC-7230 - case-insensitive; HTTP2 pack convert all header to lowercase |
|
| 19 | - * |
|
| 20 | - * @var array - all header name will be convert to lowercase |
|
| 21 | - */ |
|
| 22 | - protected array $headers = []; |
|
| 23 | - protected string $protocol = '1.1'; |
|
| 24 | - protected ?StreamInterface $stream = null; |
|
| 25 | - |
|
| 26 | - public function getProtocolVersion(): string |
|
| 27 | - { |
|
| 28 | - return $this->protocol; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public function withProtocolVersion($version): self |
|
| 32 | - { |
|
| 33 | - if ($this->protocol !== $version) { |
|
| 34 | - $this->protocol = $version; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - return $this; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function getHeaders(): array |
|
| 41 | - { |
|
| 42 | - return $this->headers; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function hasHeader($name): bool |
|
| 46 | - { |
|
| 47 | - $name = strtolower($name); |
|
| 48 | - $hasHeader = $this->headers[$name] ?? null; |
|
| 49 | - return $hasHeader !== null; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - public function getHeader($name): array |
|
| 53 | - { |
|
| 54 | - $name = strtolower($name); |
|
| 55 | - return $this->headers[$name] ?? []; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function getHeaderLine($name): string |
|
| 59 | - { |
|
| 60 | - return implode(', ', $this->getHeader($name)); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function withHeader($name, $value): self |
|
| 64 | - { |
|
| 65 | - $name = strtolower($name); |
|
| 66 | - $this->headers[$name] = $this->validateAndTrimHeader($name, (array)$value); |
|
| 67 | - return $this; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function withoutHeader($name): self |
|
| 71 | - { |
|
| 72 | - $name = strtolower($name); |
|
| 73 | - unset($this->headers[$name]); |
|
| 74 | - return $this; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function getBody(): StreamInterface |
|
| 78 | - { |
|
| 79 | - if (!$this->stream) { |
|
| 80 | - $this->stream = Stream::create(''); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $this->stream; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function withBody(StreamInterface $body): self |
|
| 87 | - { |
|
| 88 | - $this->stream = $body; |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function reset(): self |
|
| 93 | - { |
|
| 94 | - $this->headers = []; |
|
| 95 | - if ($this->stream) { |
|
| 96 | - $this->stream->close(); |
|
| 97 | - $this->stream = null; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - protected function validateAndTrimHeader(string $name, array $values): array |
|
| 104 | - { |
|
| 105 | - if (preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $name) !== 1) { |
|
| 106 | - throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string.'); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $returnValues = []; |
|
| 110 | - foreach ($values as $v) { |
|
| 111 | - if ((!is_numeric($v) && !is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string)$v)) { |
|
| 112 | - throw new InvalidArgumentException('Header values must be RFC 7230 compatible strings.'); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $returnValues[] = trim((string)$v); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return $returnValues; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function withAddedHeader($name, $value): self |
|
| 122 | - { |
|
| 123 | - $this->setHeaders([$name => $value]); |
|
| 124 | - return $this; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - protected function setHeaders(array $headers): void |
|
| 128 | - { |
|
| 129 | - foreach ($headers as $name => $values) { |
|
| 130 | - $values = (array)$values; |
|
| 131 | - $name = strtolower((string)$name); |
|
| 132 | - |
|
| 133 | - if (!$this->hasHeader($name)) { |
|
| 134 | - $this->headers[$name] = []; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $values = $this->validateAndTrimHeader($name, $values); |
|
| 138 | - |
|
| 139 | - foreach ($values as $value) { |
|
| 140 | - $this->headers[$name][] = $value; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 17 | + /** |
|
| 18 | + * RFC-2616, RFC-7230 - case-insensitive; HTTP2 pack convert all header to lowercase |
|
| 19 | + * |
|
| 20 | + * @var array - all header name will be convert to lowercase |
|
| 21 | + */ |
|
| 22 | + protected array $headers = []; |
|
| 23 | + protected string $protocol = '1.1'; |
|
| 24 | + protected ?StreamInterface $stream = null; |
|
| 25 | + |
|
| 26 | + public function getProtocolVersion(): string |
|
| 27 | + { |
|
| 28 | + return $this->protocol; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public function withProtocolVersion($version): self |
|
| 32 | + { |
|
| 33 | + if ($this->protocol !== $version) { |
|
| 34 | + $this->protocol = $version; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + return $this; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function getHeaders(): array |
|
| 41 | + { |
|
| 42 | + return $this->headers; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function hasHeader($name): bool |
|
| 46 | + { |
|
| 47 | + $name = strtolower($name); |
|
| 48 | + $hasHeader = $this->headers[$name] ?? null; |
|
| 49 | + return $hasHeader !== null; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + public function getHeader($name): array |
|
| 53 | + { |
|
| 54 | + $name = strtolower($name); |
|
| 55 | + return $this->headers[$name] ?? []; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function getHeaderLine($name): string |
|
| 59 | + { |
|
| 60 | + return implode(', ', $this->getHeader($name)); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function withHeader($name, $value): self |
|
| 64 | + { |
|
| 65 | + $name = strtolower($name); |
|
| 66 | + $this->headers[$name] = $this->validateAndTrimHeader($name, (array)$value); |
|
| 67 | + return $this; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function withoutHeader($name): self |
|
| 71 | + { |
|
| 72 | + $name = strtolower($name); |
|
| 73 | + unset($this->headers[$name]); |
|
| 74 | + return $this; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function getBody(): StreamInterface |
|
| 78 | + { |
|
| 79 | + if (!$this->stream) { |
|
| 80 | + $this->stream = Stream::create(''); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $this->stream; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function withBody(StreamInterface $body): self |
|
| 87 | + { |
|
| 88 | + $this->stream = $body; |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function reset(): self |
|
| 93 | + { |
|
| 94 | + $this->headers = []; |
|
| 95 | + if ($this->stream) { |
|
| 96 | + $this->stream->close(); |
|
| 97 | + $this->stream = null; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + protected function validateAndTrimHeader(string $name, array $values): array |
|
| 104 | + { |
|
| 105 | + if (preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $name) !== 1) { |
|
| 106 | + throw new InvalidArgumentException('Header name must be an RFC 7230 compatible string.'); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $returnValues = []; |
|
| 110 | + foreach ($values as $v) { |
|
| 111 | + if ((!is_numeric($v) && !is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string)$v)) { |
|
| 112 | + throw new InvalidArgumentException('Header values must be RFC 7230 compatible strings.'); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $returnValues[] = trim((string)$v); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return $returnValues; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function withAddedHeader($name, $value): self |
|
| 122 | + { |
|
| 123 | + $this->setHeaders([$name => $value]); |
|
| 124 | + return $this; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + protected function setHeaders(array $headers): void |
|
| 128 | + { |
|
| 129 | + foreach ($headers as $name => $values) { |
|
| 130 | + $values = (array)$values; |
|
| 131 | + $name = strtolower((string)$name); |
|
| 132 | + |
|
| 133 | + if (!$this->hasHeader($name)) { |
|
| 134 | + $this->headers[$name] = []; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $values = $this->validateAndTrimHeader($name, $values); |
|
| 138 | + |
|
| 139 | + foreach ($values as $value) { |
|
| 140 | + $this->headers[$name][] = $value; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | } |