| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 69 |
|
| 70 |
ot |
| 71 |
|
| 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 |
|
| 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 |
|
| 95 |
|
| 96 |
public function getId(): string |
| 97 |
{ |
| 98 |
return $this->id; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function getCode(): string |
| 105 |
{ |
| 106 |
return $this->code; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getVariant(): ?string |
| 113 |
{ |
| 114 |
return $this->variant; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 135 |
|
| 136 |
public function getRegion(): ?string |
| 137 |
{ |
| 138 |
return $this->region; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
ot |
| 145 |
|
| 146 |
|
| 147 |
public function getRegionName(): ?string |
| 148 |
{ |
| 149 |
return $this->region ? (static::regions()[$this->region] ?? $this->region) : null; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getFullIsoName(): string |
| 156 |
{ |
| 157 |
$this->isoName ??= $this->getNames()['isoName']; |
| 158 |
|
| 159 |
return $this->isoName; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function setIsoName(string $isoName): static |
| 166 |
{ |
| 167 |
$this->isoName = $isoName; |
| 168 |
|
| 169 |
return $this; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function getFullNativeName(): string |
| 176 |
{ |
| 177 |
$this->nativeName ??= $this->getNames()['nativeName']; |
| 178 |
|
| 179 |
return $this->nativeName; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function setNativeName(string $nativeName): static |
| 186 |
{ |
| 187 |
$this->nativeName = $nativeName; |
| 188 |
|
| 189 |
return $this; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function getIsoName(): string |
| 196 |
{ |
| 197 |
$name = $this->getFullIsoName(); |
| 198 |
|
| 199 |
return trim(strstr($name, ',', true) ?: $name); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function getNativeName(): string |
| 206 |
{ |
| 207 |
$name = $this->getFullNativeName(); |
| 208 |
|
| 209 |
return trim(strstr($name, ',', true) ?: $name); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 258 |
|
| 259 |
public function __toString(): string |
| 260 |
{ |
| 261 |
return $this->getId(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function jsonSerialize(): string |
| 268 |
{ |
| 269 |
return $this->getIsoDescription(); |
| 270 |
} |
| 271 |
} |