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