-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAbstractLanguageResolver.php
More file actions
177 lines (154 loc) · 5.62 KB
/
AbstractLanguageResolver.php
File metadata and controls
177 lines (154 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Core\Repository\SiteAccessAware\Language;
use Ibexa\Contracts\Core\Repository\Exceptions\OutOfBoundsException;
use Ibexa\Contracts\Core\Repository\LanguageResolver as APILanguageResolver;
use Ibexa\Contracts\Core\Repository\Values\Content\Language;
/**
* Common abstract implementation of Language resolver.
*
* @internal
*/
abstract class AbstractLanguageResolver implements APILanguageResolver
{
/** @var bool */
private $defaultUseAlwaysAvailable;
/** @var bool */
private $defaultShowAllTranslations;
/**
* Values typically provided by user context, will need to be set depending on your own custom logic using setter.
*
* E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both
* UI strings as well as default languages to prioritize for repository objects.
*
* If set, this will have priority over configured languages.
*
* @var string|null
*/
private $contextLanguage;
/**
* @param bool $defaultUseAlwaysAvailable
* @param bool $defaultShowAllTranslations
*/
public function __construct(
bool $defaultUseAlwaysAvailable = true,
bool $defaultShowAllTranslations = false
) {
$this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
$this->defaultShowAllTranslations = $defaultShowAllTranslations;
}
/**
* Get list of languages configured via dedicated layer.
*
* @return string[]
*/
abstract protected function getConfiguredLanguages(): array;
/**
* For use by custom events / logic setting language for all retrieved objects from repository.
*
* User language will, if set, will have prepended before configured languages. But in cases PHP API consumer
* specifies languages to retrieve repository objects in it will instead be appended as a fallback.
*
* If set, this will have priority over configured languages.
*
* @param string|null $contextLanguage
*/
final public function setContextLanguage(?string $contextLanguage): void
{
$this->contextLanguage = $contextLanguage;
}
/**
* Get context language currently set by custom logic.
*
* @return string|null
*/
final public function getContextLanguage(): ?string
{
return $this->contextLanguage;
}
/**
* Get prioritized languages taking into account forced and context languages.
*
* @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set.
*
* @return array
*/
final public function getPrioritizedLanguages(?array $forcedLanguages = null): array
{
// Skip if languages param has been set by API user
if ($forcedLanguages !== null) {
return $forcedLanguages;
}
// Detect if we should load all languages by default
if ($this->defaultShowAllTranslations) {
return Language::ALL;
}
// create language based on context and configuration, where context language is made most important one
$languages = [];
if ($this->contextLanguage !== null) {
$languages[] = $this->contextLanguage;
}
return array_values(array_unique(array_merge($languages, $this->getConfiguredLanguages())));
}
public function getFirstPrioritizedLanguage(?array $forcedLanguages = null): string
{
$languages = $this->getPrioritizedLanguages();
$firstLanguage = reset($languages);
if ($firstLanguage === false) {
throw new OutOfBoundsException('No prioritized language found.');
}
return $firstLanguage;
}
/**
* For use by event listening to config resolver scope changes (or other event changing configured languages).
*
* @param bool $defaultUseAlwaysAvailable
*/
final public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void
{
$this->defaultUseAlwaysAvailable = $defaultUseAlwaysAvailable;
}
/**
* Get currently set UseAlwaysAvailable.
*
* @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value,
* typically arguments provided to API.
*
* @return bool
*/
final public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool
{
if ($forcedUseAlwaysAvailable !== null) {
return $forcedUseAlwaysAvailable;
}
return $this->defaultUseAlwaysAvailable;
}
/**
* For use by event listening to config resolver scope changes (or other event changing configured languages).
*
* @param bool $defaultShowAllTranslations
*/
final public function setShowAllTranslations(bool $defaultShowAllTranslations): void
{
$this->defaultShowAllTranslations = $defaultShowAllTranslations;
}
/**
* Get currently set showAllTranslations.
*
* @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value,
* typically arguments provided to API.
*
* @return bool
*/
final public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool
{
if ($forcedShowAllTranslations !== null) {
return $forcedShowAllTranslations;
}
return $this->defaultShowAllTranslations;
}
}