[TASK] Add full configuration to SiteLanguage::toArray
[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 $this->fallbackLanguageIds = is_array($configuration['fallbacks']) ? $configuration['fallbacks'] : explode(',', $configuration['fallbacks']);
156 }
157 if (isset($configuration['enabled'])) {
158 $this->enabled = (bool)$configuration['enabled'];
159 }
160 }
161
162 /**
163 * Returns the SiteLanguage in an array representation for e.g. the usage
164 * in TypoScript.
165 *
166 * @return array
167 */
168 public function toArray(): array
169 {
170 return array_merge($this->configuration, [
171 'languageId' => $this->getLanguageId(),
172 'locale' => $this->getLocale(),
173 'base' => (string)$this->getBase(),
174 'title' => $this->getTitle(),
175 'navigationTitle' => $this->getNavigationTitle(),
176 'twoLetterIsoCode' => $this->getTwoLetterIsoCode(),
177 'hreflang' => $this->getHreflang(),
178 'direction' => $this->getDirection(),
179 'typo3Language' => $this->getTypo3Language(),
180 'flagIdentifier' => $this->getFlagIdentifier(),
181 'fallbackType' => $this->getFallbackType(),
182 'enabled' => $this->enabled(),
183 'fallbackLanguageIds' => $this->getFallbackLanguageIds(),
184 ]);
185 }
186
187 /**
188 * @return int
189 */
190 public function getLanguageId(): int
191 {
192 return $this->languageId;
193 }
194
195 /**
196 * @return string
197 */
198 public function getLocale(): string
199 {
200 return $this->locale;
201 }
202
203 /**
204 * @return UriInterface
205 */
206 public function getBase(): UriInterface
207 {
208 return $this->base;
209 }
210
211 /**
212 * @return string
213 */
214 public function getTitle(): string
215 {
216 return $this->title;
217 }
218
219 /**
220 * @return string
221 */
222 public function getNavigationTitle(): string
223 {
224 return $this->navigationTitle ?: $this->title;
225 }
226
227 /**
228 * @return string
229 */
230 public function getFlagIdentifier(): string
231 {
232 return $this->flagIdentifier;
233 }
234
235 /**
236 * @return string
237 */
238 public function getTypo3Language(): string
239 {
240 return $this->typo3Language;
241 }
242
243 /**
244 * Returns the ISO-639-1 language ISO code
245 *
246 * @return string
247 */
248 public function getTwoLetterIsoCode(): string
249 {
250 return $this->twoLetterIsoCode;
251 }
252
253 /**
254 * Returns the RFC 1766 / 3066 language tag
255 *
256 * @return string
257 */
258 public function getHreflang(): string
259 {
260 return $this->hreflang;
261 }
262
263 /**
264 * Returns the language direction
265 *
266 * @return string
267 */
268 public function getDirection(): string
269 {
270 return $this->direction;
271 }
272
273 /**
274 * Returns true if the language is available in frontend usage
275 *
276 * @return bool
277 */
278 public function enabled(): bool
279 {
280 return $this->enabled;
281 }
282
283 /**
284 * Helper so fluid can work with this as well.
285 *
286 * @return bool
287 */
288 public function isEnabled(): bool
289 {
290 return $this->enabled;
291 }
292
293 /**
294 * @return string
295 */
296 public function getFallbackType(): string
297 {
298 return $this->fallbackType;
299 }
300
301 /**
302 * @return array
303 */
304 public function getFallbackLanguageIds(): array
305 {
306 return $this->fallbackLanguageIds;
307 }
308 }