Spaces:
No application file
No application file
File size: 5,053 Bytes
d2897cd |
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\Helper\Dsn;
final class Dsn implements \Stringable
{
private const ALLOWED_DSN_ARRAY = [
'sync://' => ['sync', ''],
];
/**
* Create a new DSN.
*
* @param string $scheme The DSN scheme (e.g. sync://)
* @param string $host The DSN host (e.g. localhost)
* @param string|null $user The DSN user (e.g. root)
* @param string|null $password The DSN password (e.g. root)
* @param int|null $port The DSN port (e.g. 3306)
* @param string|null $path The DSN path (e.g. bucket/name/two)
* @param array<string, string> $options The DSN options (e.g. ['charset' => 'utf8'])
*/
public function __construct(
private string $scheme,
private string $host,
private ?string $user = null,
private ?string $password = null,
private ?int $port = null,
private ?string $path = null,
private array $options = [],
) {
}
/**
* Convert from a DSN string to a DSN object.
*
* @param string $dsn The DSN string
*
* @return self The DSN object
*/
public static function fromString(string $dsn): self
{
if (array_key_exists($dsn, self::ALLOWED_DSN_ARRAY)) {
return new self(...self::ALLOWED_DSN_ARRAY[$dsn]);
}
if (false === $parsedDsn = parse_url($dsn)) {
throw new \InvalidArgumentException(sprintf('The "%s" DSN is invalid.', $dsn));
}
if (!isset($parsedDsn['scheme'])) {
throw new \InvalidArgumentException(sprintf('The "%s" DSN must contain a scheme.', $dsn));
}
if (!isset($parsedDsn['host'])) {
throw new \InvalidArgumentException(sprintf('The "%s" DSN must contain a host (use "default" by default).', $dsn));
}
$host = urldecode($parsedDsn['host']);
$user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
$password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
$port = isset($parsedDsn['port']) ? (int) $parsedDsn['port'] : null;
$path = isset($parsedDsn['path']) ? ltrim(urldecode($parsedDsn['path']), '/') : null;
parse_str($parsedDsn['query'] ?? '', $query);
return new self($parsedDsn['scheme'], $host, $user, $password, $port, $path, $query);
}
public function __toString(): string
{
$dsn = $this->scheme.'://';
if ($this->user) {
$dsn .= urlencode($this->user);
}
if ($this->password) {
$dsn .= ':'.urlencode($this->password);
}
if ($this->user || $this->password) {
$dsn .= '@';
}
$dsn .= urlencode($this->host);
if ($this->port) {
$dsn .= ':'.$this->port;
}
if ($this->path) {
$dsn .= '/'.urlencode($this->path);
}
$query = http_build_query($this->options);
if ($query) {
$dsn .= '?'.$query;
}
return $dsn;
}
public function getScheme(): string
{
return $this->scheme;
}
public function setScheme(string $scheme): Dsn
{
$dsn = clone $this;
$dsn->scheme = $scheme;
return $dsn;
}
public function getHost(): string
{
return $this->host;
}
public function setHost(string $host): Dsn
{
$dsn = clone $this;
$dsn->host = $host;
return $dsn;
}
public function getUser(): ?string
{
return $this->user;
}
public function setUser(?string $user): Dsn
{
$dsn = clone $this;
$dsn->user = $user;
return $dsn;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$dsn = clone $this;
$dsn->password = $password;
return $dsn;
}
public function getPort(): ?int
{
return $this->port;
}
public function setPort(?int $port): Dsn
{
$dsn = clone $this;
$dsn->port = $port;
return $dsn;
}
public function getOption(string $key): ?string
{
return $this->options[$key] ?? null;
}
/**
* @return array<string, string>
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @param array<string, string> $options
*/
public function setOptions(array $options): Dsn
{
$dsn = clone $this;
$dsn->options = $options;
return $dsn;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): Dsn
{
$dsn = clone $this;
$dsn->path = $path;
return $dsn;
}
}
|