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