blob: 61ac641cd6eaab4279659bafaf25c73e12f4ffde [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Provider;
13
14use Symfony\Component\Translation\Exception\InvalidArgumentException;
15
16/**
17 * @author Mathieu Santostefano <msantostefano@protonmail.com>
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020018 */
19final class TranslationProviderCollection
20{
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010021 /**
22 * @var array<string, ProviderInterface>
23 */
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020024 private $providers;
25
26 /**
27 * @param array<string, ProviderInterface> $providers
28 */
29 public function __construct(iterable $providers)
30 {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010031 $this->providers = \is_array($providers) ? $providers : iterator_to_array($providers);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020032 }
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}