blob: a43fedc71ae4408b5bad8d6948fa0103ae519e86 [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\TranslatorBag;
15use 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 Benkard7b2a3a12021-08-16 10:57:25 +020021 */
22class FilteringProvider implements ProviderInterface
23{
24 private $provider;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010025 private array $locales;
26 private array $domains;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020027
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}