[BUGFIX] Ensure "0" is treated as proper fallback in site language
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Site / Entity / SiteLanguage.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Site\Entity;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\UriInterface;
20
21 /**
22 * Entity representing a site_language configuration of a site object.
23 */
24 class SiteLanguage
25 {
26 /**
27 * The language mapped to the sys_language DB entry.
28 *
29 * @var int
30 */
31 protected $languageId;
32
33 /**
34 * Locale, like 'de_CH' or 'en_GB'
35 *
36 * @var string
37 */
38 protected $locale;
39
40 /**
41 * The Base URL for this language
42 *
43 * @var UriInterface
44 */
45 protected $base;
46
47 /**
48 * Label to be used within TYPO3 to identify the language
49 * @var string
50 */
51 protected $title = 'Default';
52
53 /**
54 * Label to be used within language menus
55 * @var string
56 */
57 protected $navigationTitle = '';
58
59 /**
60 * The flag key (like "gb" or "fr") used to be used in TYPO3's Backend.
61 * @var string
62 */
63 protected $flagIdentifier = '';
64
65 /**
66 * The iso code for this language (two letter) ISO-639-1
67 * @var string
68 */
69 protected $twoLetterIsoCode = 'en';
70
71 /**
72 * Language tag for this language defined by RFC 1766 / 3066 for "lang"
73 * and "hreflang" attributes
74 *
75 * @var string
76 */
77 protected $hreflang = 'en-US';
78
79 /**
80 * The direction for this language
81 * @var string
82 */
83 protected $direction = '';
84
85 /**
86 * Prefix for TYPO3's language files
87 * "default" for english, otherwise one of TYPO3's internal language keys.
88 * Previously configured via TypoScript config.language = fr
89 *
90 * @var string
91 */
92 protected $typo3Language = 'default';
93
94 /**
95 * @var string
96 */
97 protected $fallbackType = 'strict';
98
99 /**
100 * @var array
101 */
102 protected $fallbackLanguageIds = [];
103
104 /**
105 * @var bool
106 */
107 protected $enabled = true;
108
109 /**
110 * Additional parameters configured for this site language
111 * @var array
112 */
113 protected $configuration = [];
114
115 /**
116 * SiteLanguage constructor.
117 *
118 * @param int $languageId
119 * @param string $locale
120 * @param UriInterface $base
121 * @param array $configuration
122 */
123 public function __construct(int $languageId, string $locale, UriInterface $base, array $configuration)
124 {
125 $this->languageId = $languageId;
126 $this->locale = $locale;
127 $this->base = $base;
128 $this->configuration = $configuration;
129
130 if (!empty($configuration['title'])) {
131 $this->title = $configuration['title'];
132 }
133 if (!empty($configuration['navigationTitle'])) {
134 $this->navigationTitle = $configuration['navigationTitle'];
135 }
136 if (!empty($configuration['flag'])) {
137 $this->flagIdentifier = $configuration['flag'];
138 }
139 if (!empty($configuration['typo3Language'])) {
140 $this->typo3Language = $configuration['typo3Language'];
141 }
142 if (!empty($configuration['iso-639-1'])) {
143 $this->twoLetterIsoCode = $configuration['iso-639-1'];
144 }
145 if (!empty($configuration['hreflang'])) {
146 $this->hreflang = $configuration['hreflang'];
147 }
148 if (!empty($configuration['direction'])) {
149 $this->direction = $configuration['direction'];
150 }
151 if (!empty($configuration['fallbackType'])) {
152 $this->fallbackType = $configuration['fallbackType'];
153 }
154 if (isset($configuration['fallbacks'])) {
155 $fallbackLanguageIds = $configuration['fallbacks'];
156
157 // It is important to distinct between "0" and "" so, empty() should not be used here
158 if (is_string($fallbackLanguageIds)) {
159 if ($fallbackLanguageIds !== '') {
160 $fallbackLanguageIds = explode(',', $fallbackLanguageIds);
161 } else {
162 $fallbackLanguageIds = [];
163 }
164 } elseif (is_scalar($fallbackLanguageIds)) {
165 $fallbackLanguageIds = [$fallbackLanguageIds];
166 }
167 $this->fallbackLanguageIds = array_map('intval', $fallbackLanguageIds);
168 }
169 if (isset($configuration['enabled'])) {
170 $this->enabled = (bool)$configuration['enabled'];
171 }
172 }
173
174 /**
175 * Returns the SiteLanguage in an array representation for e.g. the usage
176 * in TypoScript.
177 *
178 * @return array
179 */
180 public function toArray(): array
181 {
182 return array_merge($this->configuration, [
183 'languageId' => $this->getLanguageId(),
184 'locale' => $this->getLocale(),
185 'base' => (string)$this->getBase(),
186 'title' => $this->getTitle(),
187 'navigationTitle' => $this->getNavigationTitle(),
188 'twoLetterIsoCode' => $this->getTwoLetterIsoCode(),
189 'hreflang' => $this->getHreflang(),
190 'direction' => $this->getDirection(),
191 'typo3Language' => $this->getTypo3Language(),
192 'flagIdentifier' => $this->getFlagIdentifier(),
193 'fallbackType' => $this->getFallbackType(),
194 'enabled' => $this->enabled(),
195 'fallbackLanguageIds' => $this->getFallbackLanguageIds(),
196 ]);
197 }
198
199 /**
200 * @return int
201 */
202 public function getLanguageId(): int
203 {
204 return $this->languageId;
205 }
206
207 /**
208 * @return string
209 */
210 public function getLocale(): string
211 {
212 return $this->locale;
213 }
214
215 /**
216 * @return UriInterface
217 */
218 public function getBase(): UriInterface
219 {
220 return $this->base;
221 }
222
223 /**
224 * @return string
225 */
226 public function getTitle(): string
227 {
228 return $this->title;
229 }
230
231 /**
232 * @return string
233 */
234 public function getNavigationTitle(): string
235 {
236 return $this->navigationTitle ?: $this->title;
237 }
238
239 /**
240 * @return string
241 */
242 public function getFlagIdentifier(): string
243 {
244 return $this->flagIdentifier;
245 }
246
247 /**
248 * @return string
249 */
250 public function getTypo3Language(): string
251 {
252 return $this->typo3Language;
253 }
254
255 /**
256 * Returns the ISO-639-1 language ISO code
257 *
258 * @return string
259 */
260 public function getTwoLetterIsoCode(): string
261 {
262 return $this->twoLetterIsoCode;
263 }
264
265 /**
266 * Returns the RFC 1766 / 3066 language tag
267 *
268 * @return string
269 */
270 public function getHreflang(): string
271 {
272 return $this->hreflang;
273 }
274
275 /**
276 * Returns the language direction
277 *
278 * @return string
279 */
280 public function getDirection(): string
281 {
282 return $this->direction;
283 }
284
285 /**
286 * Returns true if the language is available in frontend usage
287 *
288 * @return bool
289 */
290 public function enabled(): bool
291 {
292 return $this->enabled;
293 }
294
295 /**
296 * Helper so fluid can work with this as well.
297 *
298 * @return bool
299 */
300 public function isEnabled(): bool
301 {
302 return $this->enabled;
303 }
304
305 /**
306 * @return string
307 */
308 public function getFallbackType(): string
309 {
310 return $this->fallbackType;
311 }
312
313 /**
314 * @return array
315 */
316 public function getFallbackLanguageIds(): array
317 {
318 return $this->fallbackLanguageIds;
319 }
320 }