You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.0 KiB

3 years ago
  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;
  11. use Symfony\Component\Translation\Catalogue\AbstractOperation;
  12. use Symfony\Component\Translation\Catalogue\TargetOperation;
  13. final class TranslatorBag implements TranslatorBagInterface
  14. {
  15. /** @var MessageCatalogue[] */
  16. private $catalogues = [];
  17. public function addCatalogue(MessageCatalogue $catalogue): void
  18. {
  19. if (null !== $existingCatalogue = $this->getCatalogue($catalogue->getLocale())) {
  20. $catalogue->addCatalogue($existingCatalogue);
  21. }
  22. $this->catalogues[$catalogue->getLocale()] = $catalogue;
  23. }
  24. public function addBag(TranslatorBagInterface $bag): void
  25. {
  26. foreach ($bag->getCatalogues() as $catalogue) {
  27. $this->addCatalogue($catalogue);
  28. }
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getCatalogue(string $locale = null)
  34. {
  35. if (null === $locale || !isset($this->catalogues[$locale])) {
  36. $this->catalogues[$locale] = new MessageCatalogue($locale);
  37. }
  38. return $this->catalogues[$locale];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getCatalogues(): array
  44. {
  45. return array_values($this->catalogues);
  46. }
  47. public function diff(TranslatorBagInterface $diffBag): self
  48. {
  49. $diff = new self();
  50. foreach ($this->catalogues as $locale => $catalogue) {
  51. if (null === $diffCatalogue = $diffBag->getCatalogue($locale)) {
  52. $diff->addCatalogue($catalogue);
  53. continue;
  54. }
  55. $operation = new TargetOperation($diffCatalogue, $catalogue);
  56. $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::NEW_BATCH);
  57. $newCatalogue = new MessageCatalogue($locale);
  58. foreach ($operation->getDomains() as $domain) {
  59. $newCatalogue->add($operation->getNewMessages($domain), $domain);
  60. }
  61. $diff->addCatalogue($newCatalogue);
  62. }
  63. return $diff;
  64. }
  65. public function intersect(TranslatorBagInterface $intersectBag): self
  66. {
  67. $diff = new self();
  68. foreach ($this->catalogues as $locale => $catalogue) {
  69. if (null === $intersectCatalogue = $intersectBag->getCatalogue($locale)) {
  70. continue;
  71. }
  72. $operation = new TargetOperation($catalogue, $intersectCatalogue);
  73. $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::OBSOLETE_BATCH);
  74. $obsoleteCatalogue = new MessageCatalogue($locale);
  75. foreach ($operation->getDomains() as $domain) {
  76. $obsoleteCatalogue->add($operation->getObsoleteMessages($domain), $domain);
  77. }
  78. $diff->addCatalogue($obsoleteCatalogue);
  79. }
  80. return $diff;
  81. }
  82. }