You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.8 KiB

3 years ago
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Provider;
  11. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  12. use Symfony\Component\Translation\Exception\MissingRequiredOptionException;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Oskar Stark <oskarstark@googlemail.com>
  16. */
  17. final class Dsn
  18. {
  19. private $scheme;
  20. private $host;
  21. private $user;
  22. private $password;
  23. private $port;
  24. private $path;
  25. private $options;
  26. private $originalDsn;
  27. public function __construct(string $dsn)
  28. {
  29. $this->originalDsn = $dsn;
  30. if (false === $parsedDsn = parse_url($dsn)) {
  31. throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN is invalid.', $dsn));
  32. }
  33. if (!isset($parsedDsn['scheme'])) {
  34. throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN must contain a scheme.', $dsn));
  35. }
  36. $this->scheme = $parsedDsn['scheme'];
  37. if (!isset($parsedDsn['host'])) {
  38. throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN must contain a host (use "default" by default).', $dsn));
  39. }
  40. $this->host = $parsedDsn['host'];
  41. $this->user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
  42. $this->password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
  43. $this->port = $parsedDsn['port'] ?? null;
  44. $this->path = $parsedDsn['path'] ?? null;
  45. parse_str($parsedDsn['query'] ?? '', $this->options);
  46. }
  47. public function getScheme(): string
  48. {
  49. return $this->scheme;
  50. }
  51. public function getHost(): string
  52. {
  53. return $this->host;
  54. }
  55. public function getUser(): ?string
  56. {
  57. return $this->user;
  58. }
  59. public function getPassword(): ?string
  60. {
  61. return $this->password;
  62. }
  63. public function getPort(int $default = null): ?int
  64. {
  65. return $this->port ?? $default;
  66. }
  67. public function getOption(string $key, $default = null)
  68. {
  69. return $this->options[$key] ?? $default;
  70. }
  71. public function getRequiredOption(string $key)
  72. {
  73. if (!\array_key_exists($key, $this->options) || '' === trim($this->options[$key])) {
  74. throw new MissingRequiredOptionException($key);
  75. }
  76. return $this->options[$key];
  77. }
  78. public function getOptions(): array
  79. {
  80. return $this->options;
  81. }
  82. public function getPath(): ?string
  83. {
  84. return $this->path;
  85. }
  86. public function getOriginalDsn(): string
  87. {
  88. return $this->originalDsn;
  89. }
  90. }