TranslatorBag.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 array $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. public function getCatalogue(?string $locale = null): MessageCatalogueInterface
  31. {
  32. if (null === $locale || !isset($this->catalogues[$locale])) {
  33. $this->catalogues[$locale] = new MessageCatalogue($locale);
  34. }
  35. return $this->catalogues[$locale];
  36. }
  37. public function getCatalogues(): array
  38. {
  39. return array_values($this->catalogues);
  40. }
  41. public function diff(TranslatorBagInterface $diffBag): self
  42. {
  43. $diff = new self();
  44. foreach ($this->catalogues as $locale => $catalogue) {
  45. if (null === $diffCatalogue = $diffBag->getCatalogue($locale)) {
  46. $diff->addCatalogue($catalogue);
  47. continue;
  48. }
  49. $operation = new TargetOperation($diffCatalogue, $catalogue);
  50. $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::NEW_BATCH);
  51. $newCatalogue = new MessageCatalogue($locale);
  52. foreach ($catalogue->getDomains() as $domain) {
  53. $newCatalogue->add($operation->getNewMessages($domain), $domain);
  54. }
  55. $diff->addCatalogue($newCatalogue);
  56. }
  57. return $diff;
  58. }
  59. public function intersect(TranslatorBagInterface $intersectBag): self
  60. {
  61. $diff = new self();
  62. foreach ($this->catalogues as $locale => $catalogue) {
  63. if (null === $intersectCatalogue = $intersectBag->getCatalogue($locale)) {
  64. continue;
  65. }
  66. $operation = new TargetOperation($catalogue, $intersectCatalogue);
  67. $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::OBSOLETE_BATCH);
  68. $obsoleteCatalogue = new MessageCatalogue($locale);
  69. foreach ($operation->getDomains() as $domain) {
  70. $obsoleteCatalogue->add(
  71. array_diff($operation->getMessages($domain), $operation->getNewMessages($domain)),
  72. $domain
  73. );
  74. }
  75. $diff->addCatalogue($obsoleteCatalogue);
  76. }
  77. return $diff;
  78. }
  79. }