blob: 682b5752fcf297c8c60601b7e4035bf0f501f319 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Translation\Catalogue;
13
14use Symfony\Component\Translation\MessageCatalogueInterface;
15
16/**
17 * Target operation between two catalogues:
18 * intersection = source ∩ target = {x: x ∈ source ∧ x ∈ target}
19 * all = intersection ∪ (target ∖ intersection) = target
20 * new = all ∖ source = {x: x ∈ target ∧ x ∉ source}
21 * obsolete = source ∖ all = source ∖ target = {x: x ∈ source ∧ x ∉ target}
22 * Basically, the result contains messages from the target catalogue.
23 *
24 * @author Michael Lee <michael.lee@zerustech.com>
25 */
26class TargetOperation extends AbstractOperation
27{
28 /**
29 * {@inheritdoc}
30 */
31 protected function processDomain(string $domain)
32 {
33 $this->messages[$domain] = [
34 'all' => [],
35 'new' => [],
36 'obsolete' => [],
37 ];
38 $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
39
40 // For 'all' messages, the code can't be simplified as ``$this->messages[$domain]['all'] = $target->all($domain);``,
41 // because doing so will drop messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
42 //
43 // For 'new' messages, the code can't be simplified as ``array_diff_assoc($this->target->all($domain), $this->source->all($domain));``
44 // because doing so will not exclude messages like {x: x ∈ target ∧ x ∉ source.all ∧ x ∈ source.fallback}
45 //
46 // For 'obsolete' messages, the code can't be simplified as ``array_diff_assoc($this->source->all($domain), $this->target->all($domain))``
47 // because doing so will not exclude messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
48
49 foreach ($this->source->all($domain) as $id => $message) {
50 if ($this->target->has($id, $domain)) {
51 $this->messages[$domain]['all'][$id] = $message;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010052 $d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020053 $this->result->add([$id => $message], $d);
54 if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
55 $this->result->setMetadata($id, $keyMetadata, $d);
56 }
57 } else {
58 $this->messages[$domain]['obsolete'][$id] = $message;
59 }
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}