MergeOperation.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. * {@inheritdoc}
  25. */
  26. protected function processDomain($domain)
  27. {
  28. $this->messages[$domain] = [
  29. 'all' => [],
  30. 'new' => [],
  31. 'obsolete' => [],
  32. ];
  33. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  34. foreach ($this->source->all($domain) as $id => $message) {
  35. $this->messages[$domain]['all'][$id] = $message;
  36. $this->result->add([$id => $message], $this->source->defines($id, $intlDomain) ? $intlDomain : $domain);
  37. if (null !== $keyMetadata = $this->source->getMetadata($id, $domain)) {
  38. $this->result->setMetadata($id, $keyMetadata, $domain);
  39. }
  40. }
  41. foreach ($this->target->all($domain) as $id => $message) {
  42. if (!$this->source->has($id, $domain)) {
  43. $this->messages[$domain]['all'][$id] = $message;
  44. $this->messages[$domain]['new'][$id] = $message;
  45. $this->result->add([$id => $message], $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
  46. if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
  47. $this->result->setMetadata($id, $keyMetadata, $domain);
  48. }
  49. }
  50. }
  51. }
  52. }