Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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 | |
| 16 | /** |
| 17 | * @author Mathieu Santostefano <msantostefano@protonmail.com> |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 18 | */ |
| 19 | final class TranslationProviderCollection |
| 20 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 21 | /** |
| 22 | * @var array<string, ProviderInterface> |
| 23 | */ |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 24 | private $providers; |
| 25 | |
| 26 | /** |
| 27 | * @param array<string, ProviderInterface> $providers |
| 28 | */ |
| 29 | public function __construct(iterable $providers) |
| 30 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 31 | $this->providers = \is_array($providers) ? $providers : iterator_to_array($providers); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | public function __toString(): string |
| 35 | { |
| 36 | return '['.implode(',', array_keys($this->providers)).']'; |
| 37 | } |
| 38 | |
| 39 | public function has(string $name): bool |
| 40 | { |
| 41 | return isset($this->providers[$name]); |
| 42 | } |
| 43 | |
| 44 | public function get(string $name): ProviderInterface |
| 45 | { |
| 46 | if (!$this->has($name)) { |
| 47 | throw new InvalidArgumentException(sprintf('Provider "%s" not found. Available: "%s".', $name, (string) $this)); |
| 48 | } |
| 49 | |
| 50 | return $this->providers[$name]; |
| 51 | } |
| 52 | |
| 53 | public function keys(): array |
| 54 | { |
| 55 | return array_keys($this->providers); |
| 56 | } |
| 57 | } |