MergeOperation.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. protected function processDomain(string $domain)
  24. {
  25. $this->messages[$domain] = [
  26. 'all' => [],
  27. 'new' => [],
  28. 'obsolete' => [],
  29. ];
  30. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  31. foreach ($this->target->getCatalogueMetadata('', $domain) ?? [] as $key => $value) {
  32. if (null === $this->result->getCatalogueMetadata($key, $domain)) {
  33. $this->result->setCatalogueMetadata($key, $value, $domain);
  34. }
  35. }
  36. foreach ($this->target->getCatalogueMetadata('', $intlDomain) ?? [] as $key => $value) {
  37. if (null === $this->result->getCatalogueMetadata($key, $intlDomain)) {
  38. $this->result->setCatalogueMetadata($key, $value, $intlDomain);
  39. }
  40. }
  41. foreach ($this->source->all($domain) as $id => $message) {
  42. $this->messages[$domain]['all'][$id] = $message;
  43. $d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
  44. $this->result->add([$id => $message], $d);
  45. if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
  46. $this->result->setMetadata($id, $keyMetadata, $d);
  47. }
  48. }
  49. foreach ($this->target->all($domain) as $id => $message) {
  50. if (!$this->source->has($id, $domain)) {
  51. $this->messages[$domain]['all'][$id] = $message;
  52. $this->messages[$domain]['new'][$id] = $message;
  53. $d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
  54. $this->result->add([$id => $message], $d);
  55. if (null !== $keyMetadata = $this->target->getMetadata($id, $d)) {
  56. $this->result->setMetadata($id, $keyMetadata, $d);
  57. }
  58. }
  59. }
  60. }
  61. }