MergeOperation.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Catalogue;
  11. use Symfony\Component\Translation\MessageCatalogueInterface;
  12. /**
  13. * Merge operation between two catalogues as follows:
  14. * all = source ∪ target = {x: x ∈ source ∨ x ∈ target}
  15. * new = all ∖ source = {x: x ∈ target ∧ x ∉ source}
  16. * obsolete = source ∖ all = {x: x ∈ source ∧ x ∉ source ∧ x ∉ target} = ∅
  17. * Basically, the result contains messages from both catalogues.
  18. *
  19. * @author Jean-François Simon <contact@jfsimon.fr>
  20. */
  21. class MergeOperation extends AbstractOperation
  22. {
  23. /**
  24. * @return void
  25. */
  26. protected function processDomain(string $domain)
  27. {
  28. $this->messages[$domain] = [
  29. 'all' => [],
  30. 'new' => [],
  31. 'obsolete' => [],
  32. ];
  33. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  34. foreach ($this->target->getCatalogueMetadata('', $domain) ?? [] as $key => $value) {
  35. if (null === $this->result->getCatalogueMetadata($key, $domain)) {
  36. $this->result->setCatalogueMetadata($key, $value, $domain);
  37. }
  38. }
  39. foreach ($this->target->getCatalogueMetadata('', $intlDomain) ?? [] as $key => $value) {
  40. if (null === $this->result->getCatalogueMetadata($key, $intlDomain)) {
  41. $this->result->setCatalogueMetadata($key, $value, $intlDomain);
  42. }
  43. }
  44. foreach ($this->source->all($domain) as $id => $message) {
  45. $this->messages[$domain]['all'][$id] = $message;
  46. $d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
  47. $this->result->add([$id => $message], $d);
  48. if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
  49. $this->result->setMetadata($id, $keyMetadata, $d);
  50. }
  51. }
  52. foreach ($this->target->all($domain) as $id => $message) {
  53. if (!$this->source->has($id, $domain)) {
  54. $this->messages[$domain]['all'][$id] = $message;
  55. $this->messages[$domain]['new'][$id] = $message;
  56. $d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
  57. $this->result->add([$id => $message], $d);
  58. if (null !== $keyMetadata = $this->target->getMetadata($id, $d)) {
  59. $this->result->setMetadata($id, $keyMetadata, $d);
  60. }
  61. }
  62. }
  63. }
  64. }