TargetOperation.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * Target operation between two catalogues:
  14. * intersection = source ∩ target = {x: x ∈ source ∧ x ∈ target}
  15. * all = intersection ∪ (target ∖ intersection) = target
  16. * new = all ∖ source = {x: x ∈ target ∧ x ∉ source}
  17. * obsolete = source ∖ all = source ∖ target = {x: x ∈ source ∧ x ∉ target}
  18. * Basically, the result contains messages from the target catalogue.
  19. *
  20. * @author Michael Lee <michael.lee@zerustech.com>
  21. */
  22. class TargetOperation extends AbstractOperation
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function processDomain(string $domain)
  28. {
  29. $this->messages[$domain] = [
  30. 'all' => [],
  31. 'new' => [],
  32. 'obsolete' => [],
  33. ];
  34. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  35. // For 'all' messages, the code can't be simplified as ``$this->messages[$domain]['all'] = $target->all($domain);``,
  36. // because doing so will drop messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
  37. //
  38. // For 'new' messages, the code can't be simplified as ``array_diff_assoc($this->target->all($domain), $this->source->all($domain));``
  39. // because doing so will not exclude messages like {x: x ∈ target ∧ x ∉ source.all ∧ x ∈ source.fallback}
  40. //
  41. // For 'obsolete' messages, the code can't be simplified as ``array_diff_assoc($this->source->all($domain), $this->target->all($domain))``
  42. // because doing so will not exclude messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
  43. foreach ($this->source->all($domain) as $id => $message) {
  44. if ($this->target->has($id, $domain)) {
  45. $this->messages[$domain]['all'][$id] = $message;
  46. $d = $this->target->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. } else {
  52. $this->messages[$domain]['obsolete'][$id] = $message;
  53. }
  54. }
  55. foreach ($this->target->all($domain) as $id => $message) {
  56. if (!$this->source->has($id, $domain)) {
  57. $this->messages[$domain]['all'][$id] = $message;
  58. $this->messages[$domain]['new'][$id] = $message;
  59. $d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
  60. $this->result->add([$id => $message], $d);
  61. if (null !== $keyMetadata = $this->target->getMetadata($id, $d)) {
  62. $this->result->setMetadata($id, $keyMetadata, $d);
  63. }
  64. }
  65. }
  66. }
  67. }