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\TranslatorBag; |
| 15 | use Symfony\Component\Translation\TranslatorBagInterface; |
| 16 | |
| 17 | /** |
| 18 | * Filters domains and locales between the Translator config values and those specific to each provider. |
| 19 | * |
| 20 | * @author Mathieu Santostefano <msantostefano@protonmail.com> |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 21 | */ |
| 22 | class FilteringProvider implements ProviderInterface |
| 23 | { |
| 24 | private $provider; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 25 | private array $locales; |
| 26 | private array $domains; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 27 | |
| 28 | public function __construct(ProviderInterface $provider, array $locales, array $domains = []) |
| 29 | { |
| 30 | $this->provider = $provider; |
| 31 | $this->locales = $locales; |
| 32 | $this->domains = $domains; |
| 33 | } |
| 34 | |
| 35 | public function __toString(): string |
| 36 | { |
| 37 | return (string) $this->provider; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * {@inheritdoc} |
| 42 | */ |
| 43 | public function write(TranslatorBagInterface $translatorBag): void |
| 44 | { |
| 45 | $this->provider->write($translatorBag); |
| 46 | } |
| 47 | |
| 48 | public function read(array $domains, array $locales): TranslatorBag |
| 49 | { |
| 50 | $domains = !$this->domains ? $domains : array_intersect($this->domains, $domains); |
| 51 | $locales = array_intersect($this->locales, $locales); |
| 52 | |
| 53 | return $this->provider->read($domains, $locales); |
| 54 | } |
| 55 | |
| 56 | public function delete(TranslatorBagInterface $translatorBag): void |
| 57 | { |
| 58 | $this->provider->delete($translatorBag); |
| 59 | } |
| 60 | |
| 61 | public function getDomains(): array |
| 62 | { |
| 63 | return $this->domains; |
| 64 | } |
| 65 | } |