TargetOperation.php 2.9 KB

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