TranslatorBag.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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): MessageCatalogueInterface
  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(
  77. array_diff($operation->getMessages($domain), $operation->getNewMessages($domain)),
  78. $domain
  79. );
  80. }
  81. $diff->addCatalogue($obsoleteCatalogue);
  82. }
  83. return $diff;
  84. }
  85. }