-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDsn.php
145 lines (123 loc) · 3.45 KB
/
Dsn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\SmsSender\Transport;
use Klipper\Component\SmsSender\Exception\InvalidArgumentException;
/**
* DSN.
*
* @author François Pluchino <[email protected]>
*/
final class Dsn
{
private string $scheme;
private string $host;
private ?string $user;
private ?string $password;
private ?int $port;
private array $options;
/**
* @param string $scheme The scheme
* @param string $host The host
* @param null|string $user The user
* @param null|string $password The password
* @param null|int $port The port
* @param array $options The options
*/
public function __construct(
string $scheme,
string $host,
?string $user = null,
?string $password = null,
?int $port = null,
array $options = []
) {
$this->scheme = $scheme;
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->port = $port;
$this->options = $options;
}
/**
* Create a DSN instance from a string.
*
* @param string $dsn The string of dsn
*/
public static function fromString(string $dsn): self
{
if (false === $parsedDsn = parse_url($dsn)) {
throw new InvalidArgumentException(sprintf('The "%s" SMS Sender DSN is invalid.', $dsn));
}
if (!isset($parsedDsn['scheme'])) {
throw new InvalidArgumentException(sprintf('The "%s" SMS Sender DSN must contain a transport scheme.', $dsn));
}
if (!isset($parsedDsn['host'])) {
throw new InvalidArgumentException(sprintf('The "%s" SMS Sender DSN must contain a SMS Sender name.', $dsn));
}
$user = isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null;
$password = isset($parsedDsn['pass']) ? urldecode($parsedDsn['pass']) : null;
$port = $parsedDsn['port'] ?? null;
parse_str($parsedDsn['query'] ?? '', $query);
$query = $query ?? [];
foreach ($query as $key => $value) {
$query[$key] = urldecode($value);
}
return new self($parsedDsn['scheme'], $parsedDsn['host'], $user, $password, $port, $query);
}
/**
* Get the scheme.
*/
public function getScheme(): string
{
return $this->scheme;
}
/**
* Get the host.
*/
public function getHost(): string
{
return $this->host;
}
/**
* Get the user.
*/
public function getUser(): ?string
{
return $this->user;
}
/**
* Get the password.
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* Get the port.
*
* @param null|int $default The default value
*/
public function getPort(int $default = null): ?int
{
return $this->port ?? $default;
}
/**
* Get the option.
*
* @param string $key The key option
* @param null|mixed $default The default value
*
* @return null|mixed
*/
public function getOption(string $key, $default = null)
{
return $this->options[$key] ?? $default;
}
}