[TASK] Change Site Base Handling to PSR-7 URI Interface
[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 $attributes = [];
114
115 /**
116 * SiteLanguage constructor.
117 *
118 * @param int $languageId
119 * @param string $locale
120 * @param UriInterface $base
121 * @param array $attributes
122 */
123 public function __construct(int $languageId, string $locale, UriInterface $base, array $attributes)
124 {
125 $this->languageId = $languageId;
126 $this->locale = $locale;
127 $this->base = $base;
128 $this->attributes = $attributes;
129 if (!empty($attributes['title'])) {
130 $this->title = $attributes['title'];
131 }
132 if (!empty($attributes['navigationTitle'])) {
133 $this->navigationTitle = $attributes['navigationTitle'];
134 }
135 if (!empty($attributes['flag'])) {
136 $this->flagIdentifier = $attributes['flag'];
137 }
138 if (!empty($attributes['typo3Language'])) {
139 $this->typo3Language = $attributes['typo3Language'];
140 }
141 if (!empty($attributes['iso-639-1'])) {
142 $this->twoLetterIsoCode = $attributes['iso-639-1'];
143 }
144 if (!empty($attributes['hreflang'])) {
145 $this->hreflang = $attributes['hreflang'];
146 }
147 if (!empty($attributes['direction'])) {
148 $this->direction = $attributes['direction'];
149 }
150 if (!empty($attributes['fallbackType'])) {
151 $this->fallbackType = $attributes['fallbackType'];
152 }
153 if (!empty($attributes['fallbacks'])) {
154 $this->fallbackLanguageIds = $attributes['fallbacks'];
155 }
156 if (isset($attributes['enabled'])) {
157 $this->enabled = (bool)$attributes['enabled'];
158 }
159 }
160
161 /**
162 * Returns the SiteLanguage in an array representation for e.g. the usage
163 * in TypoScript.
164 *
165 * @return array
166 */
167 public function toArray(): array
168 {
169 return [
170 'languageId' => $this->getLanguageId(),
171 'locale' => $this->getLocale(),
172 'base' => (string)$this->getBase(),
173 'title' => $this->getTitle(),
174 'navigationTitle' => $this->getNavigationTitle(),
175 'twoLetterIsoCode' => $this->getTwoLetterIsoCode(),
176 'hreflang' => $this->getHreflang(),
177 'direction' => $this->getDirection(),
178 'typo3Language' => $this->getTypo3Language(),
179 'flagIdentifier' => $this->getFlagIdentifier(),
180 'fallbackType' => $this->getFallbackType(),
181 'enabled' => $this->enabled(),
182 'fallbackLanguageIds' => $this->getFallbackLanguageIds(),
183 ];
184 }
185
186 /**
187 * @return int
188 */
189 public function getLanguageId(): int
190 {
191 return $this->languageId;
192 }
193
194 /**
195 * @return string
196 */
197 public function getLocale(): string
198 {
199 return $this->locale;
200 }
201
202 /**
203 * @return UriInterface
204 */
205 public function getBase(): UriInterface
206 {
207 return $this->base;
208 }
209
210 /**
211 * @return string
212 */
213 public function getTitle(): string
214 {
215 return $this->title;
216 }
217
218 /**
219 * @return string
220 */
221 public function getNavigationTitle(): string
222 {
223 return $this->navigationTitle ?: $this->getTitle();
224 }
225
226 /**
227 * @return string
228 */
229 public function getFlagIdentifier(): string
230 {
231 return $this->flagIdentifier;
232 }
233
234 /**
235 * @return string
236 */
237 public function getTypo3Language(): string
238 {
239 return $this->typo3Language;
240 }
241
242 /**
243 * Returns the ISO-639-1 language ISO code
244 *
245 * @return string
246 */
247 public function getTwoLetterIsoCode(): string
248 {
249 return $this->twoLetterIsoCode;
250 }
251
252 /**
253 * Returns the RFC 1766 / 3066 language tag
254 *
255 * @return string
256 */
257 public function getHreflang(): string
258 {
259 return $this->hreflang;
260 }
261
262 /**
263 * Returns the language direction
264 *
265 * @return string
266 */
267 public function getDirection(): string
268 {
269 return $this->direction;
270 }
271
272 /**
273 * Returns true if the language is available in frontend usage
274 *
275 * @return bool
276 */
277 public function enabled(): bool
278 {
279 return $this->enabled;
280 }
281
282 /**
283 * Helper so fluid can work with this as well.
284 *
285 * @return bool
286 */
287 public function isEnabled(): bool
288 {
289 return $this->enabled;
290 }
291
292 /**
293 * @return string
294 */
295 public function getFallbackType(): string
296 {
297 return $this->fallbackType;
298 }
299
300 /**
301 * @return array
302 */
303 public function getFallbackLanguageIds(): array
304 {
305 return $this->fallbackLanguageIds;
306 }
307 }