Debian Codegraph @

lib/3rdParty/composer/symfony/translation/TranslatorBag.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation;
13
14 use Symfony\Component\Translation\Catalogue\AbstractOperation;
15 use Symfony\Component\Translation\Catalogue\TargetOperation;
16
17 final class TranslatorBag implements TranslatorBagInterface
18 {
19 /** @var MessageCatalogue[] */
20 private array $catalogues = [];
21
22 public function addCatalogue(MessageCatalogue $catalogue): void
23 {
24 if (null !== $existingCatalogue = $this->getCatalogue($catalogue->getLocale())) {
25 $catalogue->addCatalogue($existingCatalogue);
26 }
27
28 $this->catalogues[$catalogue->getLocale()] = $catalogue;
29 }
30
31 public function addBag(TranslatorBagInterface $bag): void
32 {
33 foreach ($bag->getCatalogues() as $catalogue) {
34 $this->addCatalogue($catalogue);
35 }
36 }
37
38 public function getCatalogue(?string $locale = null): MessageCatalogueInterface
39 {
40 if (null === $locale || !isset($this->catalogues[$locale])) {
41 $this->catalogues[$locale] = new MessageCatalogue($locale);
42 }
43
44 return $this->catalogues[$locale];
45 }
46
47 public function getCatalogues(): array
48 {
49 return array_values($this->catalogues);
50 }
51
52 public function diff(TranslatorBagInterface $diffBag): self
53 {
54 $diff = new self();
55
56 foreach ($this->catalogues as $locale => $catalogue) {
57 if (null === $diffCatalogue = $diffBag->getCatalogue($locale)) {
58 $diff->addCatalogue($catalogue);
59
60 continue;
61 }
62
63 $operation = new TargetOperation($diffCatalogue, $catalogue);
64 $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::NEW_BATCH);
65 $newCatalogue = new MessageCatalogue($locale);
66
67 foreach ($catalogue->getDomains() as $domain) {
68 $newCatalogue->add($operation->getNewMessages($domain), $domain);
69 }
70
71 $diff->addCatalogue($newCatalogue);
72 }
73
74 return $diff;
75 }
76
77 public function intersect(TranslatorBagInterface $intersectBag): self
78 {
79 $diff = new self();
80
81 foreach ($this->catalogues as $locale => $catalogue) {
82 if (null === $intersectCatalogue = $intersectBag->getCatalogue($locale)) {
83 continue;
84 }
85
86 $operation = new TargetOperation($catalogue, $intersectCatalogue);
87 $operation->moveMessagesToIntlDomainsIfPossible(AbstractOperation::OBSOLETE_BATCH);
88 $obsoleteCatalogue = new MessageCatalogue($locale);
89
90 foreach ($operation->getDomains() as $domain) {
91 $obsoleteCatalogue->add(
92 array_diff($operation->getMessages($domain), $operation->getNewMessages($domain)),
93 $domain
94 );
95 }
96
97 $diff->addCatalogue($obsoleteCatalogue);
98 }
99
100 return $diff;
101 }
102 }