TargetOperation.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. protected function processDomain(string $domain)
  25. {
  26. $this->messages[$domain] = [
  27. 'all' => [],
  28. 'new' => [],
  29. 'obsolete' => [],
  30. ];
  31. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  32. foreach ($this->target->getCatalogueMetadata('', $domain) ?? [] as $key => $value) {
  33. if (null === $this->result->getCatalogueMetadata($key, $domain)) {
  34. $this->result->setCatalogueMetadata($key, $value, $domain);
  35. }
  36. }
  37. foreach ($this->target->getCatalogueMetadata('', $intlDomain) ?? [] as $key => $value) {
  38. if (null === $this->result->getCatalogueMetadata($key, $intlDomain)) {
  39. $this->result->setCatalogueMetadata($key, $value, $intlDomain);
  40. }
  41. }
  42. // For 'all' messages, the code can't be simplified as ``$this->messages[$domain]['all'] = $target->all($domain);``,
  43. // because doing so will drop messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
  44. //
  45. // For 'new' messages, the code can't be simplified as ``array_diff_assoc($this->target->all($domain), $this->source->all($domain));``
  46. // because doing so will not exclude messages like {x: x ∈ target ∧ x ∉ source.all ∧ x ∈ source.fallback}
  47. //
  48. // For 'obsolete' messages, the code can't be simplified as ``array_diff_assoc($this->source->all($domain), $this->target->all($domain))``
  49. // because doing so will not exclude messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
  50. foreach ($this->source->all($domain) as $id => $message) {
  51. if ($this->target->has($id, $domain)) {
  52. $this->messages[$domain]['all'][$id] = $message;
  53. $d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
  54. $this->result->add([$id => $message], $d);
  55. if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
  56. $this->result->setMetadata($id, $keyMetadata, $d);
  57. }
  58. } else {
  59. $this->messages[$domain]['obsolete'][$id] = $message;
  60. }
  61. }
  62. foreach ($this->target->all($domain) as $id => $message) {
  63. if (!$this->source->has($id, $domain)) {
  64. $this->messages[$domain]['all'][$id] = $message;
  65. $this->messages[$domain]['new'][$id] = $message;
  66. $d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
  67. $this->result->add([$id => $message], $d);
  68. if (null !== $keyMetadata = $this->target->getMetadata($id, $d)) {
  69. $this->result->setMetadata($id, $keyMetadata, $d);
  70. }
  71. }
  72. }
  73. }
  74. }