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.

59 lines
1.4 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. /**
  13. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  14. *
  15. * @experimental in 5.3
  16. */
  17. final class TranslationProviderCollection
  18. {
  19. private $providers;
  20. /**
  21. * @param array<string, ProviderInterface> $providers
  22. */
  23. public function __construct(iterable $providers)
  24. {
  25. $this->providers = [];
  26. foreach ($providers as $name => $provider) {
  27. $this->providers[$name] = $provider;
  28. }
  29. }
  30. public function __toString(): string
  31. {
  32. return '['.implode(',', array_keys($this->providers)).']';
  33. }
  34. public function has(string $name): bool
  35. {
  36. return isset($this->providers[$name]);
  37. }
  38. public function get(string $name): ProviderInterface
  39. {
  40. if (!$this->has($name)) {
  41. throw new InvalidArgumentException(sprintf('Provider "%s" not found. Available: "%s".', $name, (string) $this));
  42. }
  43. return $this->providers[$name];
  44. }
  45. public function keys(): array
  46. {
  47. return array_keys($this->providers);
  48. }
  49. }