Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Language.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon;
15
16 use JsonSerializable;
17
18 class Language implements JsonSerializable
19 {
20 protected static ?array $languagesNames = null;
21
22 protected static ?array $regionsNames = null;
23
24 protected string $id;
25
26 protected string $code;
27
28 protected ?string $variant = null;
29
30 protected ?string $region = null;
31
32 protected ?array $names = null;
33
34 protected ?string $isoName = null;
35
36 protected ?string $nativeName = null;
37
38 public function __construct(string $id)
39 {
40 $this->id = str_replace('-', '_', $id);
41 $parts = explode('_', $this->id);
42 $this->code = $parts[0];
43
44 if (isset($parts[1])) {
45 if (!preg_match('/^[A-Z]+$/', $parts[1])) {
46 $this->variant = $parts[1];
47 $parts[1] = $parts[2] ?? null;
48 }
49 if ($parts[1]) {
50 $this->region = $parts[1];
51 }
52 }
53 }
54
55 /**
56 * Get the list of the known languages.
57 *
58 * @return array
59 */
60 public static function all(): array
61 {
62 static::$languagesNames ??= require __DIR__.'/List/languages.php';
63
64 return static::$languagesNames;
65 }
66
67 /**
68 * Get the list of the known regions.
69 *
70 * ⚠ ISO 3166-2 short name provided with no warranty, should not
71 * be used for any purpose to show official state names.
72 */
73 public static function regions(): array
74 {
75 static::$regionsNames ??= require __DIR__.'/List/regions.php';
76
77 return static::$regionsNames;
78 }
79
80 /**
81 * Get both isoName and nativeName as an array.
82 */
83 public function getNames(): array
84 {
85 $this->names ??= static::all()[$this->code] ?? [
86 'isoName' => $this->code,
87 'nativeName' => $this->code,
88 ];
89
90 return $this->names;
91 }
92
93 /**
94 * Returns the original locale ID.
95 */
96 public function getId(): string
97 {
98 return $this->id;
99 }
100
101 /**
102 * Returns the code of the locale "en"/"fr".
103 */
104 public function getCode(): string
105 {
106 return $this->code;
107 }
108
109 /**
110 * Returns the variant code such as cyrl/latn.
111 */
112 public function getVariant(): ?string
113 {
114 return $this->variant;
115 }
116
117 /**
118 * Returns the variant such as Cyrillic/Latin.
119 */
120 public function getVariantName(): ?string
121 {
122 if ($this->variant === 'Latn') {
123 return 'Latin';
124 }
125
126 if ($this->variant === 'Cyrl') {
127 return 'Cyrillic';
128 }
129
130 return $this->variant;
131 }
132
133 /**
134 * Returns the region part of the locale.
135 */
136 public function getRegion(): ?string
137 {
138 return $this->region;
139 }
140
141 /**
142 * Returns the region name for the current language.
143 *
144 * ⚠ ISO 3166-2 short name provided with no warranty, should not
145 * be used for any purpose to show official state names.
146 */
147 public function getRegionName(): ?string
148 {
149 return $this->region ? (static::regions()[$this->region] ?? $this->region) : null;
150 }
151
152 /**
153 * Returns the long ISO language name.
154 */
155 public function getFullIsoName(): string
156 {
157 $this->isoName ??= $this->getNames()['isoName'];
158
159 return $this->isoName;
160 }
161
162 /**
163 * Set the ISO language name.
164 */
165 public function setIsoName(string $isoName): static
166 {
167 $this->isoName = $isoName;
168
169 return $this;
170 }
171
172 /**
173 * Return the full name of the language in this language.
174 */
175 public function getFullNativeName(): string
176 {
177 $this->nativeName ??= $this->getNames()['nativeName'];
178
179 return $this->nativeName;
180 }
181
182 /**
183 * Set the name of the language in this language.
184 */
185 public function setNativeName(string $nativeName): static
186 {
187 $this->nativeName = $nativeName;
188
189 return $this;
190 }
191
192 /**
193 * Returns the short ISO language name.
194 */
195 public function getIsoName(): string
196 {
197 $name = $this->getFullIsoName();
198
199 return trim(strstr($name, ',', true) ?: $name);
200 }
201
202 /**
203 * Get the short name of the language in this language.
204 */
205 public function getNativeName(): string
206 {
207 $name = $this->getFullNativeName();
208
209 return trim(strstr($name, ',', true) ?: $name);
210 }
211
212 /**
213 * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
214 */
215 public function getIsoDescription(): string
216 {
217 $region = $this->getRegionName();
218 $variant = $this->getVariantName();
219
220 return $this->getIsoName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
221 }
222
223 /**
224 * Get a string with short native name, region in parentheses if applicable, variant in parentheses if applicable.
225 */
226 public function getNativeDescription(): string
227 {
228 $region = $this->getRegionName();
229 $variant = $this->getVariantName();
230
231 return $this->getNativeName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
232 }
233
234 /**
235 * Get a string with long ISO name, region in parentheses if applicable, variant in parentheses if applicable.
236 */
237 public function getFullIsoDescription(): string
238 {
239 $region = $this->getRegionName();
240 $variant = $this->getVariantName();
241
242 return $this->getFullIsoName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
243 }
244
245 /**
246 * Get a string with long native name, region in parentheses if applicable, variant in parentheses if applicable.
247 */
248 public function getFullNativeDescription(): string
249 {
250 $region = $this->getRegionName();
251 $variant = $this->getVariantName();
252
253 return $this->getFullNativeName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
254 }
255
256 /**
257 * Returns the original locale ID.
258 */
259 public function __toString(): string
260 {
261 return $this->getId();
262 }
263
264 /**
265 * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
266 */
267 public function jsonSerialize(): string
268 {
269 return $this->getIsoDescription();
270 }
271 }