| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
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 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
19 |
* @author Oskar Stark <oskarstark@googlemail.com>
|
|
|
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(string $dsn)
|
|
|
33 |
{
|
|
|
34 |
$this->originalDsn = $dsn;
|
|
|
35 |
|
|
|
36 |
if (false === $parsedDsn = parse_url($dsn)) {
|
|
|
37 |
throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN is invalid.', $dsn));
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
if (!isset($parsedDsn['scheme'])) {
|
|
|
41 |
throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN must contain a scheme.', $dsn));
|
|
|
42 |
}
|
|
|
43 |
$this->scheme = $parsedDsn['scheme'];
|
|
|
44 |
|
|
|
45 |
if (!isset($parsedDsn['host'])) {
|
|
|
46 |
throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN must contain a host (use "default" by default).', $dsn));
|
|
|
47 |
}
|
|
|
48 |
$this->host = $parsedDsn['host'];
|
|
|
49 |
|
|
|
50 |
$this->user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
|
|
|
51 |
$this->password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
|
|
|
52 |
$this->port = $parsedDsn['port'] ?? null;
|
|
|
53 |
$this->path = $parsedDsn['path'] ?? null;
|
|
|
54 |
parse_str($parsedDsn['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)
|
|
|
83 |
{
|
|
|
84 |
return $this->options[$key] ?? $default;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function getRequiredOption(string $key)
|
|
|
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 |
}
|