.github
2 years ago
Dsn.php
4 years ago
InvalidQueryParameterTypeException.php
4 years ago
LICENSE
4 years ago
QueryBag.php
4 years ago
README.md
4 years ago
composer.json
2 years ago
Dsn.php
357 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Enqueue\Dsn; |
| 6 | |
| 7 | class Dsn |
| 8 | { |
| 9 | /** |
| 10 | * @var string |
| 11 | */ |
| 12 | private $scheme; |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | private $schemeProtocol; |
| 18 | |
| 19 | /** |
| 20 | * @var string[] |
| 21 | */ |
| 22 | private $schemeExtensions; |
| 23 | |
| 24 | /** |
| 25 | * @var string|null |
| 26 | */ |
| 27 | private $user; |
| 28 | |
| 29 | /** |
| 30 | * @var string|null |
| 31 | */ |
| 32 | private $password; |
| 33 | |
| 34 | /** |
| 35 | * @var string|null |
| 36 | */ |
| 37 | private $host; |
| 38 | |
| 39 | /** |
| 40 | * @var int|null |
| 41 | */ |
| 42 | private $port; |
| 43 | |
| 44 | /** |
| 45 | * @var string|null |
| 46 | */ |
| 47 | private $path; |
| 48 | |
| 49 | /** |
| 50 | * @var string|null |
| 51 | */ |
| 52 | private $queryString; |
| 53 | |
| 54 | /** |
| 55 | * @var QueryBag |
| 56 | */ |
| 57 | private $queryBag; |
| 58 | |
| 59 | public function __construct( |
| 60 | string $scheme, |
| 61 | string $schemeProtocol, |
| 62 | array $schemeExtensions, |
| 63 | ?string $user, |
| 64 | ?string $password, |
| 65 | ?string $host, |
| 66 | ?int $port, |
| 67 | ?string $path, |
| 68 | ?string $queryString, |
| 69 | array $query |
| 70 | ) { |
| 71 | $this->scheme = $scheme; |
| 72 | $this->schemeProtocol = $schemeProtocol; |
| 73 | $this->schemeExtensions = $schemeExtensions; |
| 74 | $this->user = $user; |
| 75 | $this->password = $password; |
| 76 | $this->host = $host; |
| 77 | $this->port = $port; |
| 78 | $this->path = $path; |
| 79 | $this->queryString = $queryString; |
| 80 | $this->queryBag = new QueryBag($query); |
| 81 | } |
| 82 | |
| 83 | public function getScheme(): string |
| 84 | { |
| 85 | return $this->scheme; |
| 86 | } |
| 87 | |
| 88 | public function getSchemeProtocol(): string |
| 89 | { |
| 90 | return $this->schemeProtocol; |
| 91 | } |
| 92 | |
| 93 | public function getSchemeExtensions(): array |
| 94 | { |
| 95 | return $this->schemeExtensions; |
| 96 | } |
| 97 | |
| 98 | public function hasSchemeExtension(string $extension): bool |
| 99 | { |
| 100 | return in_array($extension, $this->schemeExtensions, true); |
| 101 | } |
| 102 | |
| 103 | public function getUser(): ?string |
| 104 | { |
| 105 | return $this->user; |
| 106 | } |
| 107 | |
| 108 | public function getPassword(): ?string |
| 109 | { |
| 110 | return $this->password; |
| 111 | } |
| 112 | |
| 113 | public function getHost(): ?string |
| 114 | { |
| 115 | return $this->host; |
| 116 | } |
| 117 | |
| 118 | public function getPort(): ?int |
| 119 | { |
| 120 | return $this->port; |
| 121 | } |
| 122 | |
| 123 | public function getPath(): ?string |
| 124 | { |
| 125 | return $this->path; |
| 126 | } |
| 127 | |
| 128 | public function getQueryString(): ?string |
| 129 | { |
| 130 | return $this->queryString; |
| 131 | } |
| 132 | |
| 133 | public function getQueryBag(): QueryBag |
| 134 | { |
| 135 | return $this->queryBag; |
| 136 | } |
| 137 | |
| 138 | public function getQuery(): array |
| 139 | { |
| 140 | return $this->queryBag->toArray(); |
| 141 | } |
| 142 | |
| 143 | public function getString(string $name, string $default = null): ?string |
| 144 | { |
| 145 | return $this->queryBag->getString($name, $default); |
| 146 | } |
| 147 | |
| 148 | public function getDecimal(string $name, int $default = null): ?int |
| 149 | { |
| 150 | return $this->queryBag->getDecimal($name, $default); |
| 151 | } |
| 152 | |
| 153 | public function getOctal(string $name, int $default = null): ?int |
| 154 | { |
| 155 | return $this->queryBag->getOctal($name, $default); |
| 156 | } |
| 157 | |
| 158 | public function getFloat(string $name, float $default = null): ?float |
| 159 | { |
| 160 | return $this->queryBag->getFloat($name, $default); |
| 161 | } |
| 162 | |
| 163 | public function getBool(string $name, bool $default = null): ?bool |
| 164 | { |
| 165 | return $this->queryBag->getBool($name, $default); |
| 166 | } |
| 167 | |
| 168 | public function getArray(string $name, array $default = []): QueryBag |
| 169 | { |
| 170 | return $this->queryBag->getArray($name, $default); |
| 171 | } |
| 172 | |
| 173 | public function toArray() |
| 174 | { |
| 175 | return [ |
| 176 | 'scheme' => $this->scheme, |
| 177 | 'schemeProtocol' => $this->schemeProtocol, |
| 178 | 'schemeExtensions' => $this->schemeExtensions, |
| 179 | 'user' => $this->user, |
| 180 | 'password' => $this->password, |
| 181 | 'host' => $this->host, |
| 182 | 'port' => $this->port, |
| 183 | 'path' => $this->path, |
| 184 | 'queryString' => $this->queryString, |
| 185 | 'query' => $this->queryBag->toArray(), |
| 186 | ]; |
| 187 | } |
| 188 | |
| 189 | public static function parseFirst(string $dsn): ?self |
| 190 | { |
| 191 | return self::parse($dsn)[0]; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param string $dsn |
| 196 | * |
| 197 | * @return Dsn[] |
| 198 | */ |
| 199 | public static function parse(string $dsn): array |
| 200 | { |
| 201 | if (false === strpos($dsn, ':')) { |
| 202 | throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".')); |
| 203 | } |
| 204 | |
| 205 | list($scheme, $dsnWithoutScheme) = explode(':', $dsn, 2); |
| 206 | |
| 207 | $scheme = strtolower($scheme); |
| 208 | if (false == preg_match('/^[a-z\d+-.]*$/', $scheme)) { |
| 209 | throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.'); |
| 210 | } |
| 211 | |
| 212 | $schemeParts = explode('+', $scheme); |
| 213 | $schemeProtocol = $schemeParts[0]; |
| 214 | |
| 215 | unset($schemeParts[0]); |
| 216 | $schemeExtensions = array_values($schemeParts); |
| 217 | |
| 218 | $user = parse_url($dsn, PHP_URL_USER) ?: null; |
| 219 | if (is_string($user)) { |
| 220 | $user = rawurldecode($user); |
| 221 | } |
| 222 | |
| 223 | $password = parse_url($dsn, PHP_URL_PASS) ?: null; |
| 224 | if (is_string($password)) { |
| 225 | $password = rawurldecode($password); |
| 226 | } |
| 227 | |
| 228 | $path = parse_url($dsn, PHP_URL_PATH) ?: null; |
| 229 | if ($path) { |
| 230 | $path = rawurldecode($path); |
| 231 | } |
| 232 | |
| 233 | $query = []; |
| 234 | $queryString = parse_url($dsn, PHP_URL_QUERY) ?: null; |
| 235 | if (is_string($queryString)) { |
| 236 | $query = self::httpParseQuery($queryString, '&', PHP_QUERY_RFC3986); |
| 237 | } |
| 238 | $hostsPorts = ''; |
| 239 | if (0 === strpos($dsnWithoutScheme, '//')) { |
| 240 | $dsnWithoutScheme = substr($dsnWithoutScheme, 2); |
| 241 | $dsnWithoutUserPassword = explode('@', $dsnWithoutScheme, 2); |
| 242 | $dsnWithoutUserPassword = 2 === count($dsnWithoutUserPassword) ? |
| 243 | $dsnWithoutUserPassword[1] : |
| 244 | $dsnWithoutUserPassword[0] |
| 245 | ; |
| 246 | |
| 247 | list($hostsPorts) = explode('#', $dsnWithoutUserPassword, 2); |
| 248 | list($hostsPorts) = explode('?', $hostsPorts, 2); |
| 249 | list($hostsPorts) = explode('/', $hostsPorts, 2); |
| 250 | } |
| 251 | |
| 252 | if (empty($hostsPorts)) { |
| 253 | return [ |
| 254 | new self( |
| 255 | $scheme, |
| 256 | $schemeProtocol, |
| 257 | $schemeExtensions, |
| 258 | null, |
| 259 | null, |
| 260 | null, |
| 261 | null, |
| 262 | $path, |
| 263 | $queryString, |
| 264 | $query |
| 265 | ), |
| 266 | ]; |
| 267 | } |
| 268 | |
| 269 | $dsns = []; |
| 270 | $hostParts = explode(',', $hostsPorts); |
| 271 | foreach ($hostParts as $key => $hostPart) { |
| 272 | unset($hostParts[$key]); |
| 273 | |
| 274 | $parts = explode(':', $hostPart, 2); |
| 275 | $host = $parts[0]; |
| 276 | |
| 277 | $port = null; |
| 278 | if (isset($parts[1])) { |
| 279 | $port = (int) $parts[1]; |
| 280 | } |
| 281 | |
| 282 | $dsns[] = new self( |
| 283 | $scheme, |
| 284 | $schemeProtocol, |
| 285 | $schemeExtensions, |
| 286 | $user, |
| 287 | $password, |
| 288 | $host, |
| 289 | $port, |
| 290 | $path, |
| 291 | $queryString, |
| 292 | $query |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | return $dsns; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * based on http://php.net/manual/en/function.parse-str.php#119484 with some slight modifications. |
| 301 | */ |
| 302 | private static function httpParseQuery(string $queryString, string $argSeparator = '&', int $decType = PHP_QUERY_RFC1738): array |
| 303 | { |
| 304 | $result = []; |
| 305 | $parts = explode($argSeparator, $queryString); |
| 306 | |
| 307 | foreach ($parts as $part) { |
| 308 | list($paramName, $paramValue) = explode('=', $part, 2); |
| 309 | |
| 310 | switch ($decType) { |
| 311 | case PHP_QUERY_RFC3986: |
| 312 | $paramName = rawurldecode($paramName); |
| 313 | $paramValue = rawurldecode($paramValue); |
| 314 | break; |
| 315 | case PHP_QUERY_RFC1738: |
| 316 | default: |
| 317 | $paramName = urldecode($paramName); |
| 318 | $paramValue = urldecode($paramValue); |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | if (preg_match_all('/\[([^\]]*)\]/m', $paramName, $matches)) { |
| 323 | $paramName = substr($paramName, 0, strpos($paramName, '[')); |
| 324 | $keys = array_merge([$paramName], $matches[1]); |
| 325 | } else { |
| 326 | $keys = [$paramName]; |
| 327 | } |
| 328 | |
| 329 | $target = &$result; |
| 330 | |
| 331 | foreach ($keys as $index) { |
| 332 | if ('' === $index) { |
| 333 | if (is_array($target)) { |
| 334 | $intKeys = array_filter(array_keys($target), 'is_int'); |
| 335 | $index = count($intKeys) ? max($intKeys) + 1 : 0; |
| 336 | } else { |
| 337 | $target = [$target]; |
| 338 | $index = 1; |
| 339 | } |
| 340 | } elseif (isset($target[$index]) && !is_array($target[$index])) { |
| 341 | $target[$index] = [$target[$index]]; |
| 342 | } |
| 343 | |
| 344 | $target = &$target[$index]; |
| 345 | } |
| 346 | |
| 347 | if (is_array($target)) { |
| 348 | $target[] = $paramValue; |
| 349 | } else { |
| 350 | $target = $paramValue; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return $result; |
| 355 | } |
| 356 | } |
| 357 |