| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Provider; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Translation\Exception\MissingRequiredOptionException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class Dsn |
| 22 |
{ |
| 23 |
private ?string $scheme; |
| 24 |
private ?string $host; |
| 25 |
private ?string $user; |
| 26 |
private ?string $password; |
| 27 |
private ?int $port; |
| 28 |
private ?string $path; |
| 29 |
private array $options = []; |
| 30 |
private string $originalDsn; |
| 31 |
|
| 32 |
public function __construct(#[\SensitiveParameter] string $dsn) |
| 33 |
{ |
| 34 |
$this->originalDsn = $dsn; |
| 35 |
|
| 36 |
if (false === $params = parse_url($dsn)) { |
| 37 |
throw new InvalidArgumentException('The translation provider DSN is invalid.'); |
| 38 |
} |
| 39 |
|
| 40 |
if (!isset($params['scheme'])) { |
| 41 |
throw new InvalidArgumentException('The translation provider DSN must contain a scheme.'); |
| 42 |
} |
| 43 |
$this->scheme = $params['scheme']; |
| 44 |
|
| 45 |
if (!isset($params['host'])) { |
| 46 |
throw new InvalidArgumentException('The translation provider DSN must contain a host (use "default" by default).'); |
| 47 |
} |
| 48 |
$this->host = $params['host']; |
| 49 |
|
| 50 |
$this->user = '' !== ($params['user'] ?? '') ? rawurldecode($params['user']) : null; |
| 51 |
$this->password = '' !== ($params['pass'] ?? '') ? rawurldecode($params['pass']) : null; |
| 52 |
$this->port = $params['port'] ?? null; |
| 53 |
$this->path = $params['path'] ?? null; |
| 54 |
parse_str($params['query'] ?? '', $this->options); |
| 55 |
} |
| 56 |
|
| 57 |
public function getScheme(): string |
| 58 |
{ |
| 59 |
return $this->scheme; |
| 60 |
} |
| 61 |
|
| 62 |
public function getHost(): string |
| 63 |
{ |
| 64 |
return $this->host; |
| 65 |
} |
| 66 |
|
| 67 |
public function getUser(): ?string |
| 68 |
{ |
| 69 |
return $this->user; |
| 70 |
} |
| 71 |
|
| 72 |
public function getPassword(): ?string |
| 73 |
{ |
| 74 |
return $this->password; |
| 75 |
} |
| 76 |
|
| 77 |
public function getPort(?int $default = null): ?int |
| 78 |
{ |
| 79 |
return $this->port ?? $default; |
| 80 |
} |
| 81 |
|
| 82 |
public function getOption(string $key, mixed $default = null): mixed |
| 83 |
{ |
| 84 |
return $this->options[$key] ?? $default; |
| 85 |
} |
| 86 |
|
| 87 |
public function getRequiredOption(string $key): mixed |
| 88 |
{ |
| 89 |
if (!\array_key_exists($key, $this->options) || '' === trim($this->options[$key])) { |
| 90 |
throw new MissingRequiredOptionException($key); |
| 91 |
} |
| 92 |
|
| 93 |
return $this->options[$key]; |
| 94 |
} |
| 95 |
|
| 96 |
public function getOptions(): array |
| 97 |
{ |
| 98 |
return $this->options; |
| 99 |
} |
| 100 |
|
| 101 |
public function getPath(): ?string |
| 102 |
{ |
| 103 |
return $this->path; |
| 104 |
} |
| 105 |
|
| 106 |
public function getOriginalDsn(): string |
| 107 |
{ |
| 108 |
return $this->originalDsn; |
| 109 |
} |
| 110 |
} |