Skip to content

Commit

Permalink
Merge pull request #2964 from briannesbitt/refactor/language-class-cl…
Browse files Browse the repository at this point in the history
…eanup

Cleanup and warranty notice
  • Loading branch information
kylekatarnls committed Mar 13, 2024
2 parents 7e76ee8 + 7684141 commit 34ccf6f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 112 deletions.
131 changes: 29 additions & 102 deletions src/Carbon/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,26 @@
namespace Carbon;

use JsonSerializable;
use ReturnTypeWillChange;

class Language implements JsonSerializable
{
/**
* @var array
*/
protected static $languagesNames;
protected static ?array $languagesNames = null;

/**
* @var array
*/
protected static $regionsNames;
protected static ?array $regionsNames = null;

/**
* @var string
*/
protected $id;
protected string $id;

/**
* @var string
*/
protected $code;
protected string $code;

/**
* @var string|null
*/
protected $variant;
protected ?string $variant = null;

/**
* @var string|null
*/
protected $region;
protected ?string $region = null;

/**
* @var array
*/
protected $names;
protected ?array $names = null;

/**
* @var string
*/
protected $isoName;
protected ?string $isoName = null;

/**
* @var string
*/
protected $nativeName;
protected ?string $nativeName = null;

public function __construct(string $id)
{
Expand All @@ -85,50 +57,41 @@ public function __construct(string $id)
*
* @return array
*/
public static function all()
public static function all(): array
{
if (!static::$languagesNames) {
static::$languagesNames = require __DIR__.'/List/languages.php';
}
static::$languagesNames ??= require __DIR__.'/List/languages.php';

return static::$languagesNames;
}

/**
* Get the list of the known regions.
*
* @return array
* ⚠ ISO 3166-2 short name provided with no warranty, should not
* be used for any purpose to show official state names.
*/
public static function regions()
public static function regions(): array
{
if (!static::$regionsNames) {
static::$regionsNames = require __DIR__.'/List/regions.php';
}
static::$regionsNames ??= require __DIR__.'/List/regions.php';

return static::$regionsNames;
}

/**
* Get both isoName and nativeName as an array.
*
* @return array
*/
public function getNames(): array
{
if (!$this->names) {
$this->names = static::all()[$this->code] ?? [
'isoName' => $this->code,
'nativeName' => $this->code,
];
}
$this->names ??= static::all()[$this->code] ?? [
'isoName' => $this->code,
'nativeName' => $this->code,
];

return $this->names;
}

/**
* Returns the original locale ID.
*
* @return string
*/
public function getId(): string
{
Expand All @@ -137,8 +100,6 @@ public function getId(): string

/**
* Returns the code of the locale "en"/"fr".
*
* @return string
*/
public function getCode(): string
{
Expand All @@ -147,8 +108,6 @@ public function getCode(): string

/**
* Returns the variant code such as cyrl/latn.
*
* @return string|null
*/
public function getVariant(): ?string
{
Expand All @@ -157,8 +116,6 @@ public function getVariant(): ?string

/**
* Returns the variant such as Cyrillic/Latin.
*
* @return string|null
*/
public function getVariantName(): ?string
{
Expand All @@ -175,8 +132,6 @@ public function getVariantName(): ?string

/**
* Returns the region part of the locale.
*
* @return string|null
*/
public function getRegion(): ?string
{
Expand All @@ -186,7 +141,8 @@ public function getRegion(): ?string
/**
* Returns the region name for the current language.
*
* @return string|null
* ⚠ ISO 3166-2 short name provided with no warranty, should not
* be used for any purpose to show official state names.
*/
public function getRegionName(): ?string
{
Expand All @@ -195,22 +151,16 @@ public function getRegionName(): ?string

/**
* Returns the long ISO language name.
*
* @return string
*/
public function getFullIsoName(): string
{
if (!$this->isoName) {
$this->isoName = $this->getNames()['isoName'];
}
$this->isoName ??= $this->getNames()['isoName'];

return $this->isoName;
}

/**
* Set the ISO language name.
*
* @param string $isoName
*/
public function setIsoName(string $isoName): static
{
Expand All @@ -221,22 +171,16 @@ public function setIsoName(string $isoName): static

/**
* Return the full name of the language in this language.
*
* @return string
*/
public function getFullNativeName(): string
{
if (!$this->nativeName) {
$this->nativeName = $this->getNames()['nativeName'];
}
$this->nativeName ??= $this->getNames()['nativeName'];

return $this->nativeName;
}

/**
* Set the name of the language in this language.
*
* @param string $nativeName
*/
public function setNativeName(string $nativeName): static
{
Expand All @@ -247,8 +191,6 @@ public function setNativeName(string $nativeName): static

/**
* Returns the short ISO language name.
*
* @return string
*/
public function getIsoName(): string
{
Expand All @@ -259,8 +201,6 @@ public function getIsoName(): string

/**
* Get the short name of the language in this language.
*
* @return string
*/
public function getNativeName(): string
{
Expand All @@ -271,10 +211,8 @@ public function getNativeName(): string

/**
* Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
*
* @return string
*/
public function getIsoDescription()
public function getIsoDescription(): string
{
$region = $this->getRegionName();
$variant = $this->getVariantName();
Expand All @@ -284,10 +222,8 @@ public function getIsoDescription()

/**
* Get a string with short native name, region in parentheses if applicable, variant in parentheses if applicable.
*
* @return string
*/
public function getNativeDescription()
public function getNativeDescription(): string
{
$region = $this->getRegionName();
$variant = $this->getVariantName();
Expand All @@ -297,10 +233,8 @@ public function getNativeDescription()

/**
* Get a string with long ISO name, region in parentheses if applicable, variant in parentheses if applicable.
*
* @return string
*/
public function getFullIsoDescription()
public function getFullIsoDescription(): string
{
$region = $this->getRegionName();
$variant = $this->getVariantName();
Expand All @@ -310,10 +244,8 @@ public function getFullIsoDescription()

/**
* Get a string with long native name, region in parentheses if applicable, variant in parentheses if applicable.
*
* @return string
*/
public function getFullNativeDescription()
public function getFullNativeDescription(): string
{
$region = $this->getRegionName();
$variant = $this->getVariantName();
Expand All @@ -323,21 +255,16 @@ public function getFullNativeDescription()

/**
* Returns the original locale ID.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->getId();
}

/**
* Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
*
* @return string
*/
#[ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): string
{
return $this->getIsoDescription();
}
Expand Down

0 comments on commit 34ccf6f

Please sign in to comment.