1 | <?php |
||
18 | final class Dsn |
||
19 | { |
||
20 | /** @var string */ |
||
21 | private $dsn; |
||
22 | |||
23 | /** @var string */ |
||
24 | private $scheme; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $host; |
||
28 | |||
29 | /** @var int */ |
||
30 | private $port; |
||
31 | |||
32 | /** @var string */ |
||
33 | private $user; |
||
34 | |||
35 | /** @var string */ |
||
36 | private $password; |
||
37 | |||
38 | /** @var string */ |
||
39 | private $path; |
||
40 | |||
41 | /** @var string[] */ |
||
42 | private $query = []; |
||
43 | |||
44 | /** @var string[] */ |
||
45 | private $parameters = []; |
||
46 | |||
47 | //@codingStandardsIgnoreStart |
||
48 | const WINDOWS_DSN = '~(^((?<scheme>file):\\/\\/)?(?<path>((?:[a-z]|[A-Z]):(?=\\\\(?![\\0-\\37<>:"/\\\\|?*])|\\/(?![\\0-\\37<>:"/\\\\|?*])|$)|^\\\\(?=[\\\\\\/][^\\0-\\37<>:"/\\\\|?*]+)|^(?=(\\\\|\\/)$)|^\\.(?=(\\\\|\\/)$)|^\\.\\.(?=(\\\\|\\/)$)|^(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+)|^\\.(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+)|^\\.\\.(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+))((\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+|(\\\\|\\/)$)*()))$~'; |
||
49 | //@codingStandardsIgnoreEnd |
||
50 | |||
51 | /** |
||
52 | * Initializes the Dsn |
||
53 | * |
||
54 | * @param string $dsn |
||
55 | */ |
||
56 | 9 | public function __construct($dsn) |
|
60 | |||
61 | /** |
||
62 | * Returns a string representation of the DSN. |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | 4 | public function __toString() |
|
70 | |||
71 | /** |
||
72 | * Returns the scheme part of the DSN |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 6 | public function getScheme() |
|
80 | |||
81 | /** |
||
82 | * Returns the host part of the DSN |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 4 | public function getHost() |
|
90 | |||
91 | /** |
||
92 | * Returns the port part of the DSN |
||
93 | * |
||
94 | * @return int |
||
95 | */ |
||
96 | 5 | public function getPort() |
|
100 | |||
101 | /** |
||
102 | * Returns the username part of the DSN |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 1 | public function getUsername() |
|
110 | |||
111 | /** |
||
112 | * Returns the password part of the DSN |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 1 | public function getPassword() |
|
120 | |||
121 | /** |
||
122 | * Returns the path part of the DSN |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public function getPath() |
|
130 | |||
131 | /** |
||
132 | * Returns the query part of the DSN |
||
133 | * |
||
134 | * @return string[] |
||
135 | */ |
||
136 | 1 | public function getQuery() |
|
140 | |||
141 | /** |
||
142 | * Returns the parameters part of the DSN |
||
143 | * |
||
144 | * @return string[] |
||
145 | */ |
||
146 | 1 | public function getParameters() |
|
150 | |||
151 | /** |
||
152 | * Parses the given DSN |
||
153 | * |
||
154 | * @param string $dsn |
||
155 | * @return void |
||
156 | */ |
||
157 | 9 | private function parse($dsn) |
|
207 | |||
208 | /** |
||
209 | * Reconstructs the original DSN but |
||
210 | * when scheme was omitted in the original DSN, it will now be file:// |
||
211 | * |
||
212 | * @param string $location |
||
213 | * @param string[] $dsnParts |
||
214 | * @return void |
||
215 | */ |
||
216 | 7 | private function parseDsn($location, array $dsnParts) |
|
221 | |||
222 | /** |
||
223 | * validates and sets the scheme property |
||
224 | * |
||
225 | * @param string[] $locationParts |
||
226 | * @return void |
||
227 | */ |
||
228 | 7 | private function parseScheme(array $locationParts) |
|
237 | |||
238 | /** |
||
239 | * Validated provided scheme. |
||
240 | * |
||
241 | * @param string $scheme |
||
242 | * @return bool |
||
243 | */ |
||
244 | 7 | private function isValidScheme(string $scheme): bool |
|
249 | |||
250 | /** |
||
251 | * Validates and sets the host and path properties |
||
252 | * |
||
253 | * @param string[] $locationParts |
||
254 | * @return void |
||
255 | */ |
||
256 | 6 | private function parseHostAndPath(array $locationParts) |
|
268 | |||
269 | /** |
||
270 | * Validates and sets the port property |
||
271 | * |
||
272 | * @param string[] $locationParts |
||
273 | * @return void |
||
274 | */ |
||
275 | 6 | private function parsePort(array $locationParts) |
|
289 | |||
290 | /** |
||
291 | * validates and sets the query property |
||
292 | * |
||
293 | * @param string[] $locationParts |
||
294 | * @return void |
||
295 | */ |
||
296 | 6 | private function parseQuery(array $locationParts) |
|
307 | |||
308 | /** |
||
309 | * validates and sets the parameters property |
||
310 | * |
||
311 | * @param string[] $dsnParts |
||
312 | * @return void |
||
313 | */ |
||
314 | 5 | private function parseParameters(array $dsnParts) |
|
322 | |||
323 | /** |
||
324 | * Splits a key-value pair |
||
325 | * |
||
326 | * @param string $pair |
||
327 | * @return string[] $option |
||
328 | */ |
||
329 | 2 | private function splitKeyValuePair($pair) |
|
340 | } |
||
341 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.