Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

MaskGenerator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\Language;
8
9 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
10 use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
11
15 class MaskGenerator
16 {
22 protected $languageHandler;
23
29 public function __construct(LanguageHandler $languageHandler)
30 {
31 $this->languageHandler = $languageHandler;
32 }
33
45 public function generateLanguageMask(array $languages)
46 {
47 $mask = 0;
48 if (isset($languages['always-available'])) {
49 $mask |= $languages['always-available'] ? 1 : 0;
50 unset($languages['always-available']);
51 }
52
53 $languageCodes = array_keys($languages);
54 $languageList = $this->languageHandler->loadListByLanguageCodes($languageCodes);
55 foreach ($languageList as $language) {
56 $mask |= $language->id;
57 }
58
59 if ($missing = array_diff($languageCodes, array_keys($languageList))) {
60 throw new NotFoundException('Language', implode(', ', $missing));
61 }
62
63 return $mask;
64 }
65
74 public function generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable): int
75 {
76 // make sure alwaysAvailable part of bit mask always results in 1 or 0
77 $languageMask = $alwaysAvailable ? 1 : 0;
78
79 foreach ($languageIds as $languageId) {
80 $languageMask |= $languageId;
81 }
82
83 return $languageMask;
84 }
85
96 public function generateLanguageIndicator($languageCode, $alwaysAvailable)
97 {
98 return $this->languageHandler->loadByLanguageCode($languageCode)->id | ($alwaysAvailable ? 1 : 0);
99 }
100
109 public function isLanguageAlwaysAvailable($language, array $languages): bool
110 {
111 return isset($languages['always-available'])
112 && ($languages['always-available'] == $language)
113 ;
114 }
115
123 public function isAlwaysAvailable($languageMask): bool
124 {
125 return (bool)($languageMask & 1);
126 }
127
135 public function removeAlwaysAvailableFlag($languageId): int
136 {
137 return $languageId & ~1;
138 }
139
147 public function extractLanguageIdsFromMask($languageMask): array
148 {
149 $exp = 2;
150 $result = [];
151
152 // Decomposition of $languageMask into its binary components.
153 while ($exp <= $languageMask) {
154 if ($languageMask & $exp) {
155 $result[] = $exp;
156 }
157
158 $exp *= 2;
159 }
160
161 return $result;
162 }
163
171 public function extractLanguageCodesFromMask($languageMask): array
172 {
173 $languageCodes = [];
174 $languageList = $this->languageHandler->loadList(
175 $this->extractLanguageIdsFromMask($languageMask)
176 );
177 foreach ($languageList as $language) {
178 $languageCodes[] = $language->languageCode;
179 }
180
181 return $languageCodes;
182 }
183
191 public function isLanguageMaskComposite($languageMask): bool
192 {
193 // Ignore first bit
194 $languageMask = $this->removeAlwaysAvailableFlag($languageMask);
195
196 // Special case
197 if ($languageMask === 0) {
198 return false;
199 }
200
201 // Return false if power of 2
202 return (bool)($languageMask & ($languageMask - 1));
203 }
204
215 public function generateLanguageMaskFromLanguageCodes(array $languageCodes, bool $isAlwaysAvailable = false): int
216 {
217 $mask = $isAlwaysAvailable ? 1 : 0;
218
219 $languageList = $this->languageHandler->loadListByLanguageCodes($languageCodes);
220 foreach ($languageList as $language) {
221 $mask |= $language->id;
222 }
223
224 if ($missing = array_diff($languageCodes, array_keys($languageList))) {
225 throw new NotFoundException('Language', implode(', ', $missing));
226 }
227
228 return $mask;
229 }
230 }
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator
Language MaskGenerator.
Definition: MaskGenerator.php:16
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\extractLanguageCodesFromMask
extractLanguageCodesFromMask($languageMask)
Extracts Language codes contained in given $languageMask.
Definition: MaskGenerator.php:171
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\generateLanguageMask
generateLanguageMask(array $languages)
Generates a language mask from the keys of $languages.
Definition: MaskGenerator.php:45
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\removeAlwaysAvailableFlag
removeAlwaysAvailableFlag($languageId)
Removes the alwaysAvailable flag from $languageId and returns cleaned up $languageId.
Definition: MaskGenerator.php:135
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\generateLanguageMaskFromLanguageCodes
generateLanguageMaskFromLanguageCodes(array $languageCodes, bool $isAlwaysAvailable=false)
Generates a language mask from plain array of language codes and always available flag.
Definition: MaskGenerator.php:215
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\generateLanguageMaskFromLanguageIds
generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable)
Generates a language mask from pre-loaded Language Ids.
Definition: MaskGenerator.php:74
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\isLanguageAlwaysAvailable
isLanguageAlwaysAvailable($language, array $languages)
Checks if $language is always available in $languages;.
Definition: MaskGenerator.php:109
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\isLanguageMaskComposite
isLanguageMaskComposite($languageMask)
Checks if given $languageMask consists of multiple languages.
Definition: MaskGenerator.php:191
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\__construct
__construct(LanguageHandler $languageHandler)
Creates a new Language MaskGenerator.
Definition: MaskGenerator.php:29
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\extractLanguageIdsFromMask
extractLanguageIdsFromMask($languageMask)
Extracts every language Ids contained in $languageMask.
Definition: MaskGenerator.php:147
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\generateLanguageIndicator
generateLanguageIndicator($languageCode, $alwaysAvailable)
Generates a language indicator from $languageCode and $alwaysAvailable.
Definition: MaskGenerator.php:96
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\$languageHandler
$languageHandler
Definition: MaskGenerator.php:22
eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator\isAlwaysAvailable
isAlwaysAvailable($languageMask)
Checks if $languageMask contains the alwaysAvailable bit field.
Definition: MaskGenerator.php:123
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.php:15
eZ\Publish\Core\Persistence\Legacy\Content\Language
Definition: CachingHandler.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.