FilteringProvider.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Provider;
  11. use Symfony\Component\Translation\TranslatorBag;
  12. use Symfony\Component\Translation\TranslatorBagInterface;
  13. /**
  14. * Filters domains and locales between the Translator config values and those specific to each provider.
  15. *
  16. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  17. */
  18. class FilteringProvider implements ProviderInterface
  19. {
  20. private $provider;
  21. private $locales;
  22. private $domains;
  23. public function __construct(ProviderInterface $provider, array $locales, array $domains = [])
  24. {
  25. $this->provider = $provider;
  26. $this->locales = $locales;
  27. $this->domains = $domains;
  28. }
  29. public function __toString(): string
  30. {
  31. return (string) $this->provider;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function write(TranslatorBagInterface $translatorBag): void
  37. {
  38. $this->provider->write($translatorBag);
  39. }
  40. public function read(array $domains, array $locales): TranslatorBag
  41. {
  42. $domains = !$this->domains ? $domains : array_intersect($this->domains, $domains);
  43. $locales = array_intersect($this->locales, $locales);
  44. return $this->provider->read($domains, $locales);
  45. }
  46. public function delete(TranslatorBagInterface $translatorBag): void
  47. {
  48. $this->provider->delete($translatorBag);
  49. }
  50. public function getDomains(): array
  51. {
  52. return $this->domains;
  53. }
  54. }