1 | <?php |
||
23 | final class Dsn |
||
24 | { |
||
25 | /** @var string */ |
||
26 | private $dsn; |
||
27 | |||
28 | /** @var string */ |
||
29 | private $scheme; |
||
30 | |||
31 | /** @var string */ |
||
32 | private $host; |
||
33 | |||
34 | /** @var int */ |
||
35 | private $port; |
||
36 | |||
37 | /** @var string */ |
||
38 | private $user; |
||
39 | |||
40 | /** @var string */ |
||
41 | private $password; |
||
42 | |||
43 | /** @var string */ |
||
44 | private $path; |
||
45 | |||
46 | /** @var string[] */ |
||
47 | private $query = []; |
||
48 | |||
49 | /** @var string[] */ |
||
50 | private $parameters = []; |
||
51 | |||
52 | //@codingStandardsIgnoreStart |
||
53 | const WINDOWS_DSN = '~(^((?<scheme>file):\\/\\/)?(?<path>((?:[a-z]|[A-Z]):(?=\\\\(?![\\0-\\37<>:"/\\\\|?*])|\\/(?![\\0-\\37<>:"/\\\\|?*])|$)|^\\\\(?=[\\\\\\/][^\\0-\\37<>:"/\\\\|?*]+)|^(?=(\\\\|\\/)$)|^\\.(?=(\\\\|\\/)$)|^\\.\\.(?=(\\\\|\\/)$)|^(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+)|^\\.(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+)|^\\.\\.(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+))((\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+|(\\\\|\\/)$)*()))$~'; |
||
54 | |||
55 | //@codingStandardsIgnoreEnd |
||
56 | |||
57 | /** |
||
58 | * Initializes the Dsn |
||
59 | */ |
||
60 | 9 | public function __construct(string $dsn) |
|
64 | |||
65 | /** |
||
66 | * Returns a string representation of the DSN. |
||
67 | */ |
||
68 | 4 | public function __toString(): string |
|
72 | |||
73 | /** |
||
74 | * Returns the scheme part of the DSN |
||
75 | */ |
||
76 | 7 | public function getScheme(): string |
|
80 | |||
81 | /** |
||
82 | * Returns the host part of the DSN |
||
83 | */ |
||
84 | 4 | public function getHost(): ?string |
|
88 | |||
89 | /** |
||
90 | * Returns the port part of the DSN |
||
91 | */ |
||
92 | 5 | public function getPort(): int |
|
96 | |||
97 | /** |
||
98 | * Returns the username part of the DSN |
||
99 | */ |
||
100 | 1 | public function getUsername(): string |
|
104 | |||
105 | /** |
||
106 | * Returns the password part of the DSN |
||
107 | */ |
||
108 | 1 | public function getPassword(): string |
|
112 | |||
113 | /** |
||
114 | * Returns the path part of the DSN |
||
115 | */ |
||
116 | 4 | public function getPath(): Path |
|
120 | |||
121 | /** |
||
122 | * Returns the query part of the DSN |
||
123 | * |
||
124 | * @return string[] |
||
125 | */ |
||
126 | 1 | public function getQuery(): array |
|
130 | |||
131 | /** |
||
132 | * Returns the parameters part of the DSN |
||
133 | * |
||
134 | * @return string[] |
||
135 | */ |
||
136 | 1 | public function getParameters(): array |
|
140 | |||
141 | /** |
||
142 | * Parses the given DSN |
||
143 | */ |
||
144 | 9 | private function parse(string $dsn): void |
|
186 | |||
187 | /** |
||
188 | * Reconstructs the original DSN but |
||
189 | * when scheme was omitted in the original DSN, it will now be file:// |
||
190 | * |
||
191 | * @param string[] $dsnParts |
||
192 | */ |
||
193 | 8 | private function parseDsn(string $location, array $dsnParts): void |
|
198 | |||
199 | /** |
||
200 | * validates and sets the scheme property |
||
201 | * |
||
202 | * @param string[] $locationParts |
||
203 | * @throws InvalidArgumentException |
||
204 | */ |
||
205 | 8 | private function parseScheme(array $locationParts): void |
|
215 | |||
216 | /** |
||
217 | * Validated provided scheme. |
||
218 | */ |
||
219 | 8 | private function isValidScheme(string $scheme): bool |
|
224 | |||
225 | /** |
||
226 | * Validates and sets the host and path properties |
||
227 | * |
||
228 | * @param string[] $locationParts |
||
229 | */ |
||
230 | 7 | private function parseHostAndPath(array $locationParts): void |
|
242 | |||
243 | /** |
||
244 | * Validates and sets the port property |
||
245 | * |
||
246 | * @param string[] $locationParts |
||
247 | */ |
||
248 | 7 | private function parsePort(array $locationParts): void |
|
262 | |||
263 | /** |
||
264 | * validates and sets the query property |
||
265 | * |
||
266 | * @param string[] $locationParts |
||
267 | */ |
||
268 | 7 | private function parseQuery(array $locationParts): void |
|
279 | |||
280 | /** |
||
281 | * validates and sets the parameters property |
||
282 | * |
||
283 | * @param string[] $dsnParts |
||
284 | */ |
||
285 | 6 | private function parseParameters(array $dsnParts): void |
|
293 | |||
294 | /** |
||
295 | * Splits a key-value pair |
||
296 | * |
||
297 | * @return string[] |
||
298 | * @throws InvalidArgumentException |
||
299 | */ |
||
300 | 2 | private function splitKeyValuePair(string $pair): array |
|
311 | } |
||
312 |