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

Core/Repository/LanguageService.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Repository;
8
9 use eZ\Publish\API\Repository\LanguageService as LanguageServiceInterface;
10 use eZ\Publish\SPI\Persistence\Content\Language\Handler;
11 use eZ\Publish\API\Repository\Repository as RepositoryInterface;
12 use eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct;
13 use eZ\Publish\SPI\Persistence\Content\Language as SPILanguage;
14 use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
15 use eZ\Publish\API\Repository\Values\Content\Language;
16 use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
17 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
18 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
19 use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
20 use LogicException;
21 use Exception;
22
26 class LanguageService implements LanguageServiceInterface
27 {
29 protected $repository;
30
32 protected $languageHandler;
33
35 protected $settings;
36
44 public function __construct(RepositoryInterface $repository, Handler $languageHandler, array $settings = [])
45 {
46 $this->repository = $repository;
47 $this->languageHandler = $languageHandler;
48 // Union makes sure default settings are ignored if provided in argument
49 $this->settings = $settings + [
50 'languages' => ['eng-GB'],
51 ];
52 }
53
64 public function createLanguage(LanguageCreateStruct $languageCreateStruct)
65 {
66 if (!is_string($languageCreateStruct->name) || empty($languageCreateStruct->name)) {
67 throw new InvalidArgumentValue('name', $languageCreateStruct->name, 'LanguageCreateStruct');
68 }
69
70 if (!is_string($languageCreateStruct->languageCode) || empty($languageCreateStruct->languageCode)) {
71 throw new InvalidArgumentValue('languageCode', $languageCreateStruct->languageCode, 'LanguageCreateStruct');
72 }
73
74 if (!is_bool($languageCreateStruct->enabled)) {
75 throw new InvalidArgumentValue('enabled', $languageCreateStruct->enabled, 'LanguageCreateStruct');
76 }
77
78 if (!$this->repository->canUser('content', 'translations', $languageCreateStruct)) {
79 throw new UnauthorizedException('content', 'translations');
80 }
81
82 try {
83 if ($this->loadLanguage($languageCreateStruct->languageCode) !== null) {
84 throw new InvalidArgumentException('languageCreateStruct', 'language with specified language code already exists');
85 }
86 } catch (APINotFoundException $e) {
87 // Do nothing
88 }
89
90 $createStruct = new CreateStruct(
91 [
92 'languageCode' => $languageCreateStruct->languageCode,
93 'name' => $languageCreateStruct->name,
94 'isEnabled' => $languageCreateStruct->enabled,
95 ]
96 );
97
98 $this->repository->beginTransaction();
99 try {
100 $createdLanguage = $this->languageHandler->create($createStruct);
101 $this->repository->commit();
102 } catch (Exception $e) {
103 $this->repository->rollback();
104 throw $e;
105 }
106
107 return $this->buildDomainObject($createdLanguage);
108 }
109
122 public function updateLanguageName(Language $language, $newName)
123 {
124 if (!is_string($newName) || empty($newName)) {
125 throw new InvalidArgumentValue('newName', $newName);
126 }
127
128 if (!$this->repository->canUser('content', 'translations', $language)) {
129 throw new UnauthorizedException('content', 'translations');
130 }
131
132 $loadedLanguage = $this->loadLanguageById($language->id);
133
134 $updateLanguageStruct = new SPILanguage(
135 [
136 'id' => $loadedLanguage->id,
137 'languageCode' => $loadedLanguage->languageCode,
138 'name' => $newName,
139 'isEnabled' => $loadedLanguage->enabled,
140 ]
141 );
142
143 $this->repository->beginTransaction();
144 try {
145 $this->languageHandler->update($updateLanguageStruct);
146 $this->repository->commit();
147 } catch (Exception $e) {
148 $this->repository->rollback();
149 throw $e;
150 }
151
152 return $this->loadLanguageById($loadedLanguage->id);
153 }
154
164 public function enableLanguage(Language $language)
165 {
166 if (!$this->repository->canUser('content', 'translations', $language)) {
167 throw new UnauthorizedException('content', 'translations');
168 }
169
170 $loadedLanguage = $this->loadLanguageById($language->id);
171
172 $updateLanguageStruct = new SPILanguage(
173 [
174 'id' => $loadedLanguage->id,
175 'languageCode' => $loadedLanguage->languageCode,
176 'name' => $loadedLanguage->name,
177 'isEnabled' => true,
178 ]
179 );
180
181 $this->repository->beginTransaction();
182 try {
183 $this->languageHandler->update($updateLanguageStruct);
184 $this->repository->commit();
185 } catch (Exception $e) {
186 $this->repository->rollback();
187 throw $e;
188 }
189
190 return $this->loadLanguageById($loadedLanguage->id);
191 }
192
202 public function disableLanguage(Language $language)
203 {
204 if (!$this->repository->canUser('content', 'translations', $language)) {
205 throw new UnauthorizedException('content', 'translations');
206 }
207
208 $loadedLanguage = $this->loadLanguageById($language->id);
209
210 $updateLanguageStruct = new SPILanguage(
211 [
212 'id' => $loadedLanguage->id,
213 'languageCode' => $loadedLanguage->languageCode,
214 'name' => $loadedLanguage->name,
215 'isEnabled' => false,
216 ]
217 );
218
219 $this->repository->beginTransaction();
220 try {
221 $this->languageHandler->update($updateLanguageStruct);
222 $this->repository->commit();
223 } catch (Exception $e) {
224 $this->repository->rollback();
225 throw $e;
226 }
227
228 return $this->loadLanguageById($loadedLanguage->id);
229 }
230
242 public function loadLanguage($languageCode)
243 {
244 if (!is_string($languageCode) || empty($languageCode)) {
245 throw new InvalidArgumentException('languageCode', 'language code has an invalid value');
246 }
247
248 $language = $this->languageHandler->loadByLanguageCode($languageCode);
249
250 return $this->buildDomainObject($language);
251 }
252
258 public function loadLanguages()
259 {
260 $languages = $this->languageHandler->loadAll();
261
262 $returnArray = [];
263 foreach ($languages as $language) {
264 $returnArray[] = $this->buildDomainObject($language);
265 }
266
267 return $returnArray;
268 }
269
279 public function loadLanguageById($languageId)
280 {
281 $language = $this->languageHandler->load($languageId);
282
283 return $this->buildDomainObject($language);
284 }
285
289 public function loadLanguageListByCode(array $languageCodes): iterable
290 {
291 $languages = $this->languageHandler->loadListByLanguageCodes($languageCodes);
292
293 $returnArray = [];
294 foreach ($languages as $language) {
295 $returnArray[$language->languageCode] = $this->buildDomainObject($language);
296 }
297
298 return $returnArray;
299 }
300
304 public function loadLanguageListById(array $languageIds): iterable
305 {
306 $languages = $this->languageHandler->loadList($languageIds);
307
308 $returnArray = [];
309 foreach ($languages as $language) {
310 $returnArray[$language->id] = $this->buildDomainObject($language);
311 }
312
313 return $returnArray;
314 }
315
326 public function deleteLanguage(Language $language)
327 {
328 if (!$this->repository->canUser('content', 'translations', $language)) {
329 throw new UnauthorizedException('content', 'translations');
330 }
331
332 $loadedLanguage = $this->loadLanguageById($language->id);
333
334 $this->repository->beginTransaction();
335 try {
336 $this->languageHandler->delete($loadedLanguage->id);
337 $this->repository->commit();
338 } catch (LogicException $e) {
339 $this->repository->rollback();
340 throw new InvalidArgumentException('language', $e->getMessage(), $e);
341 } catch (Exception $e) {
342 $this->repository->rollback();
343 throw $e;
344 }
345 }
346
352 public function getDefaultLanguageCode()
353 {
354 return $this->settings['languages'][0];
355 }
356
363 public function getPrioritizedLanguageCodeList()
364 {
365 return $this->settings['languages'];
366 }
367
373 public function newLanguageCreateStruct()
374 {
375 return new LanguageCreateStruct();
376 }
377
385 protected function buildDomainObject(SPILanguage $spiLanguage)
386 {
387 return new Language(
388 [
389 'id' => $spiLanguage->id,
390 'languageCode' => $spiLanguage->languageCode,
391 'name' => $spiLanguage->name,
392 'enabled' => $spiLanguage->isEnabled,
393 ]
394 );
395 }
396 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct
This class represents a value for creating a language.
Definition: LanguageCreateStruct.php:15
eZ\Publish\API\Repository\Values\Content\Language
Definition: API/Repository/Values/Content/Language.php:20
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
Invalid Argument Type Exception implementation.
Definition: Base/Exceptions/InvalidArgumentValue.php:17
eZ\Publish\Core\Base\Exceptions\UnauthorizedException
UnauthorizedException Exception implementation.
Definition: Core/Base/Exceptions/UnauthorizedException.php:21
eZ\Publish\Core\Repository\LanguageService
Language service, used for language operations.
Definition: Core/Repository/LanguageService.php:27
eZ\Publish\Core\Repository\LanguageService\loadLanguage
loadLanguage($languageCode)
Loads a Language from its language code ($languageCode).
Definition: Core/Repository/LanguageService.php:242
eZ\Publish\Core\Repository\LanguageService\enableLanguage
enableLanguage(Language $language)
Enables a language.
Definition: Core/Repository/LanguageService.php:164
eZ\Publish\Core\Repository\LanguageService\deleteLanguage
deleteLanguage(Language $language)
Deletes a language from content repository.
Definition: Core/Repository/LanguageService.php:326
eZ\Publish\Core\Repository\LanguageService\$repository
$repository
Definition: Core/Repository/LanguageService.php:29
eZ\Publish\Core\Repository\LanguageService\loadLanguageListById
loadLanguageListById(array $languageIds)
{Bulk-load Languages by ids.Note: it does not throw exceptions on load, just ignores erroneous Langua...
Definition: Core/Repository/LanguageService.php:304
eZ\Publish\Core\Repository\LanguageService\loadLanguages
loadLanguages()
Loads all Languages.
Definition: Core/Repository/LanguageService.php:258
eZ\Publish\Core\Repository\LanguageService\buildDomainObject
buildDomainObject(SPILanguage $spiLanguage)
Builds Language domain object from ValueObject returned by Persistence API.
Definition: Core/Repository/LanguageService.php:385
eZ\Publish\Core\Repository\LanguageService\$languageHandler
$languageHandler
Definition: Core/Repository/LanguageService.php:32
eZ\Publish\Core\Repository\LanguageService\getPrioritizedLanguageCodeList
getPrioritizedLanguageCodeList()
Returns a configured list of prioritized languageCodes.
Definition: Core/Repository/LanguageService.php:363
eZ\Publish\Core\Repository\LanguageService\newLanguageCreateStruct
newLanguageCreateStruct()
Instantiates an object to be used for creating languages.
Definition: Core/Repository/LanguageService.php:373
eZ\Publish\Core\Repository\LanguageService\disableLanguage
disableLanguage(Language $language)
Disables a language.
Definition: Core/Repository/LanguageService.php:202
eZ\Publish\Core\Repository\LanguageService\$settings
$settings
Definition: Core/Repository/LanguageService.php:35
eZ\Publish\Core\Repository\LanguageService\updateLanguageName
updateLanguageName(Language $language, $newName)
Changes the name of the language in the content repository.
Definition: Core/Repository/LanguageService.php:122
eZ\Publish\Core\Repository\LanguageService\getDefaultLanguageCode
getDefaultLanguageCode()
Returns a configured default language code.
Definition: Core/Repository/LanguageService.php:352
eZ\Publish\Core\Repository\LanguageService\loadLanguageListByCode
loadLanguageListByCode(array $languageCodes)
{Bulk-load Languages by language codes.Note: it does not throw exceptions on load,...
Definition: Core/Repository/LanguageService.php:289
eZ\Publish\Core\Repository\LanguageService\__construct
__construct(RepositoryInterface $repository, Handler $languageHandler, array $settings=[])
Setups service with reference to repository object that created it & corresponding handler.
Definition: Core/Repository/LanguageService.php:44
eZ\Publish\Core\Repository\LanguageService\createLanguage
createLanguage(LanguageCreateStruct $languageCreateStruct)
Creates the a new Language in the content repository.
Definition: Core/Repository/LanguageService.php:64
eZ\Publish\Core\Repository\LanguageService\loadLanguageById
loadLanguageById($languageId)
Loads a Language by its id ($languageId).
Definition: Core/Repository/LanguageService.php:279
eZ\Publish\SPI\Persistence\Bookmark\CreateStruct
Definition: SPI/Persistence/Bookmark/CreateStruct.php:14
eZ\Publish\SPI\Persistence\Content\Language\CreateStruct
Struct containing accessible properties when creating Language entities.
Definition: SPI/Persistence/Content/Language/CreateStruct.php:15
eZ\Publish\API\Repository\LanguageService
Language service, used for language operations.
Definition: API/Repository/LanguageService.php:16
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.php:15
eZ\Publish\Core\Repository
Definition: Core/Repository/BookmarkService.php:9
eZ\Publish\SPI\Persistence\Content\Language
Definition: SPI/Persistence/Content/Language/CreateStruct.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.