TranslationProviderCollection.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. final class TranslationProviderCollection
  16. {
  17. /**
  18. * @var array<string, ProviderInterface>
  19. */
  20. private $providers;
  21. /**
  22. * @param array<string, ProviderInterface> $providers
  23. */
  24. public function __construct(iterable $providers)
  25. {
  26. $this->providers = \is_array($providers) ? $providers : iterator_to_array($providers);
  27. }
  28. public function __toString(): string
  29. {
  30. return '['.implode(',', array_keys($this->providers)).']';
  31. }
  32. public function has(string $name): bool
  33. {
  34. return isset($this->providers[$name]);
  35. }
  36. public function get(string $name): ProviderInterface
  37. {
  38. if (!$this->has($name)) {
  39. throw new InvalidArgumentException(sprintf('Provider "%s" not found. Available: "%s".', $name, (string) $this));
  40. }
  41. return $this->providers[$name];
  42. }
  43. public function keys(): array
  44. {
  45. return array_keys($this->providers);
  46. }
  47. }