TargetOperation.php 3.1 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. * 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. $this->result->add([$id => $message], $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
  47. if (null !== $keyMetadata = $this->source->getMetadata($id, $domain)) {
  48. $this->result->setMetadata($id, $keyMetadata, $domain);
  49. }
  50. } else {
  51. $this->messages[$domain]['obsolete'][$id] = $message;
  52. }
  53. }
  54. foreach ($this->target->all($domain) as $id => $message) {
  55. if (!$this->source->has($id, $domain)) {
  56. $this->messages[$domain]['all'][$id] = $message;
  57. $this->messages[$domain]['new'][$id] = $message;
  58. $this->result->add([$id => $message], $this->target->defines($id, $intlDomain) ? $intlDomain : $domain);
  59. if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
  60. $this->result->setMetadata($id, $keyMetadata, $domain);
  61. }
  62. }
  63. }
  64. }
  65. }