carno-php /
dsn
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Data Source Name |
||
| 4 | * User: moyo |
||
| 5 | * Date: 02/11/2017 |
||
| 6 | * Time: 3:41 PM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace Carno\DSN; |
||
| 10 | |||
| 11 | class DSN |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $raw = null; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $scheme = 'none'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $host = 'localhost'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | private $port = 0; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $user = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $pass = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $path = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $options = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * DSN constructor. |
||
| 55 | * @param string $dsn |
||
| 56 | */ |
||
| 57 | public function __construct(string $dsn) |
||
| 58 | { |
||
| 59 | $this->raw = $dsn; |
||
| 60 | |||
| 61 | if (false === strpos($dsn, '://')) { |
||
| 62 | $dsn = "{$this->scheme}://{$dsn}"; |
||
| 63 | } |
||
| 64 | |||
| 65 | $parsed = parse_url($dsn); |
||
| 66 | |||
| 67 | $this->scheme = $parsed['scheme'] ?? $this->scheme; |
||
| 68 | |||
| 69 | $this->host = $parsed['host'] ?? $this->host; |
||
| 70 | $this->port = $parsed['port'] ?? $this->port; |
||
| 71 | |||
| 72 | $this->user = $parsed['user'] ?? $this->user; |
||
| 73 | $this->pass = $parsed['pass'] ?? $this->pass; |
||
| 74 | |||
| 75 | if (isset($parsed['path'])) { |
||
| 76 | $this->path = substr($parsed['path'], 1); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (isset($parsed['query'])) { |
||
| 80 | foreach (explode('&', $parsed['query']) as $expr) { |
||
| 81 | if (false === strpos($expr, '=')) { |
||
| 82 | [$key, $val] = [$expr, true]; |
||
| 83 | } else { |
||
| 84 | [$key, $val] = explode('=', $expr); |
||
| 85 | } |
||
| 86 | $this->options[$key] = $val; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function scheme() : string |
||
| 95 | { |
||
| 96 | return $this->scheme; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function host() : string |
||
| 103 | { |
||
| 104 | return $this->host; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return int |
||
| 109 | */ |
||
| 110 | public function port() : int |
||
| 111 | { |
||
| 112 | return $this->port; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function user() : string |
||
| 119 | { |
||
| 120 | return $this->user ?? ''; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function pass() : string |
||
| 127 | { |
||
| 128 | return $this->pass ?? ''; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function path() : string |
||
| 135 | { |
||
| 136 | return $this->path ?? '/'; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $key |
||
| 141 | * @param null $default |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 142 | * @return mixed|null |
||
| 143 | */ |
||
| 144 | public function option(string $key, $default = null) |
||
| 145 | { |
||
| 146 | return $this->options[$key] ?? $default; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |