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