File: D:/HostingSpaces/SBogers10/boomdekwekerij.komma.nl/vendor/komma/kms/src/Globalization/Language.php
<?php declare(strict_types=1);
namespace Komma\KMS\Globalization;
use Komma\KMS\Globalization\Datasets\Languages;
use Illuminate\Contracts\Support\Arrayable;
class Language implements \JsonSerializable, Arrayable
{
/** @var Language[] */
private static $cache = [];
/** @var string */
private $threeLetterISOLanguageName = '';
/** @var string */
private $twoLetterISOLanguageName = '';
/** @var string */
private $name = '';
/** @var string */
private $displayName = '';
/** @var string */
private $nativeName = '';
/**
* Language constructor. Does not allow regular instantiation. Use get instance instead
*/
protected function __construct() { }
/**
* @param $name
* @return static
*/
public static function getInstance($name): Language
{
//Create a new instance and parse its name.
$instance = new static();
$instance->parseName($name);
if(!in_array($instance->name, array_keys(self::$cache), true)) { //Cache a new instance if it not already is.
//First find the country by its ISO2 name
$found = false;
foreach(Languages::DATA as $index => $data) {
if(strtolower($data['ISO-639-1']) === strtolower($instance->name)) {
//Then set data
$instance->displayName = $data['DisplayName'];
$instance->nativeName = $data['NativeName'];
$instance->twoLetterISOLanguageName = $data['ISO-639-1'];
$instance->threeLetterISOLanguageName = $data['ISO-639-3'] ?? $data['ISO-639-2'];
$found = true;
break;
}
}
self::$cache[$instance->name] = $instance; //Cache this instance if not cached.
}
return self::$cache[$instance->name];
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return [
'name' => $this->name,
'displayName' => $this->displayName,
'nativeName' => $this->nativeName,
'twoLetterIsoLanguageName' => $this->twoLetterISOLanguageName,
'threeLetterIsoLanguageName' => $this->threeLetterISOLanguageName
];
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* @param string $name
*/
private function parseName(string $name) {
$this->name = strtolower($name);
}
/**
* @return string
*/
public function getThreeLetterISOLanguageName(): string
{
return $this->threeLetterISOLanguageName;
}
/**
* @return string
*/
public function getTwoLetterISOLanguageName(): string
{
return $this->twoLetterISOLanguageName;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string
*/
public function getDisplayName(): string
{
return $this->displayName;
}
/**
* @return string
*/
public function getNativeName(): string
{
return $this->nativeName;
}
/**
* @return bool
*/
public function isComplete():bool
{
return (
$this->threeLetterISOLanguageName !== '' &&
$this->twoLetterISOLanguageName !== '' &&
$this->name !== '' &&
$this->displayName !== '' &&
$this->nativeName !== ''
);
}
}