|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Dsn; |
| 4 | + |
| 5 | +class Dsn |
| 6 | +{ |
| 7 | + /** |
| 8 | + * @var string |
| 9 | + */ |
| 10 | + private $dsn; |
| 11 | + |
| 12 | + /** |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + private $scheme; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var string|null |
| 19 | + */ |
| 20 | + private $user; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string|null |
| 24 | + */ |
| 25 | + private $password; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string|null |
| 29 | + */ |
| 30 | + private $host; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var int|null |
| 34 | + */ |
| 35 | + private $port; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string|null |
| 39 | + */ |
| 40 | + private $path; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string|null |
| 44 | + */ |
| 45 | + private $queryString; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var array |
| 49 | + */ |
| 50 | + private $query; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + private $schemeProtocol; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var string[] |
| 59 | + */ |
| 60 | + private $schemeExtensions; |
| 61 | + |
| 62 | + public function __construct(string $dsn) |
| 63 | + { |
| 64 | + $this->dsn = $dsn; |
| 65 | + $this->query = []; |
| 66 | + |
| 67 | + $this->parse($dsn); |
| 68 | + } |
| 69 | + |
| 70 | + public function __toString(): string |
| 71 | + { |
| 72 | + return $this->dsn; |
| 73 | + } |
| 74 | + |
| 75 | + public function getDsn(): string |
| 76 | + { |
| 77 | + return $this->dsn; |
| 78 | + } |
| 79 | + |
| 80 | + public function getScheme(): string |
| 81 | + { |
| 82 | + return $this->scheme; |
| 83 | + } |
| 84 | + |
| 85 | + public function getSchemeProtocol(): string |
| 86 | + { |
| 87 | + return $this->schemeProtocol; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return string[] |
| 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 | + /** |
| 104 | + * @return null|string |
| 105 | + */ |
| 106 | + public function getUser(): ?string |
| 107 | + { |
| 108 | + return $this->user; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return null|string |
| 113 | + */ |
| 114 | + public function getPassword(): ?string |
| 115 | + { |
| 116 | + return $this->password; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return null|string |
| 121 | + */ |
| 122 | + public function getHost(): ?string |
| 123 | + { |
| 124 | + return $this->host; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return int|null |
| 129 | + */ |
| 130 | + public function getPort(): ?int |
| 131 | + { |
| 132 | + return $this->port; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return null|string |
| 137 | + */ |
| 138 | + public function getPath(): ?string |
| 139 | + { |
| 140 | + return $this->path; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return null|string |
| 145 | + */ |
| 146 | + public function getQueryString(): ?string |
| 147 | + { |
| 148 | + return $this->queryString; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @return array |
| 153 | + */ |
| 154 | + public function getQuery(): array |
| 155 | + { |
| 156 | + return $this->query; |
| 157 | + } |
| 158 | + |
| 159 | + public function getQueryParameter(string $name, $default = null) |
| 160 | + { |
| 161 | + return array_key_exists($name, $this->query) ? $this->query[$name] : $default; |
| 162 | + } |
| 163 | + |
| 164 | + public function toArray() |
| 165 | + { |
| 166 | + return [ |
| 167 | + 'scheme' => $this->scheme, |
| 168 | + 'schemeProtocol' => $this->schemeProtocol, |
| 169 | + 'schemeExtensions' => $this->schemeExtensions, |
| 170 | + 'user' => $this->user, |
| 171 | + 'password' => $this->password, |
| 172 | + 'host' => $this->host, |
| 173 | + 'port' => $this->port, |
| 174 | + 'path' => $this->path, |
| 175 | + 'queryString' => $this->queryString, |
| 176 | + 'query' => $this->query, |
| 177 | + ]; |
| 178 | + } |
| 179 | + |
| 180 | + private function parse(string $dsn): void |
| 181 | + { |
| 182 | + if (false === strpos($dsn, ':')) { |
| 183 | + throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".')); |
| 184 | + } |
| 185 | + |
| 186 | + list($scheme, $dsnWithoutScheme) = explode(':', $dsn, 2); |
| 187 | + if (false == preg_match('/[\w\d+-.]/', $scheme)) { |
| 188 | + throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.'); |
| 189 | + } |
| 190 | + |
| 191 | + $scheme = strtolower($scheme); |
| 192 | + |
| 193 | + $schemeParts = explode('+', $scheme); |
| 194 | + $this->scheme = $scheme; |
| 195 | + $this->schemeProtocol = $schemeParts[0]; |
| 196 | + |
| 197 | + unset($schemeParts[0]); |
| 198 | + $this->schemeExtensions = $schemeParts; |
| 199 | + |
| 200 | + if ($host = parse_url($dsn, PHP_URL_HOST)) { |
| 201 | + $this->host = $host; |
| 202 | + } |
| 203 | + |
| 204 | + if ($port = parse_url($dsn, PHP_URL_PORT)) { |
| 205 | + $this->port = (int) $port; |
| 206 | + } |
| 207 | + |
| 208 | + if ($user = parse_url($dsn, PHP_URL_USER)) { |
| 209 | + $this->user = $user; |
| 210 | + } |
| 211 | + |
| 212 | + if ($password = parse_url($dsn, PHP_URL_PASS)) { |
| 213 | + $this->password = $password; |
| 214 | + } |
| 215 | + |
| 216 | + if ($path = parse_url($dsn, PHP_URL_PATH)) { |
| 217 | + $this->path = $path; |
| 218 | + } |
| 219 | + |
| 220 | + if ($queryString = parse_url($dsn, PHP_URL_QUERY)) { |
| 221 | + $this->queryString = $queryString; |
| 222 | + |
| 223 | + $query = []; |
| 224 | + parse_str($queryString, $query); |
| 225 | + $this->query = $query; |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments