HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/hem-mechatronics.komma.pro/app/Komma/Globalization/RegionInfo.php
<?php


namespace App\Komma\Globalization;

/**
 * Class RegionInfo
 *
 * Contains information about the country/region.
 * Inspired on the RegionInfo class from C#
 *
 * @see https://docs.microsoft.com/en-us/dotnet/api/system.globalization.regioninfo?view=netframework-4.7.2
 * @package App\Komma\Globalization
 */
class RegionInfo extends AbstractCultureParser
{
    /** @var RegionInfo[] */
    private static $cache = [];

    /** @var string $displayName */
    private $displayName = '';

    /** @var string $englishName */
    private $englishName = '';

    /** @var string $nativeName */
    private $nativeName = '';

    /** @var string $currencyEnglishName */
    private $currencyEnglishName = '';

    /** @var string $currencyNativeName */
    private $currencyNativeName = '';

    /** @var string $currencySymbol */
    private $currencySymbol = '';

    /** @var string $ISOCurrencySymbol */
    private $ISOCurrencySymbol = '';

    /** @var bool $isMetric */
    private $isMetric = '';

    /** @var string $threeLetterISORegionName */
    private $threeLetterISORegionName = '';

    /** @var string $twoLetterISORegionName */
    private $twoLetterISORegionName = '';

    /** @var string $name */
    private $name = '';

    /** @var int $countryRegionId */
    private $countryRegionId = -1;

    /** @var NumberFormatInfo $numberFormat */
    private $numberFormat;

    /**
     * Contains information about the country/region.
     *
     * The culture name must be either a two-letter region name, such as "US" for the United States,
     * or the name of a specific culture, such as "en-US" for English (United States)
     *
     * CultureInfo constructor.
     * @param string $name
     */
    public function __construct(string $name)
    {
        if(in_array('name', array_keys(self::$cache), true)) return self::$cache['name']; //Return the region info if it was initiated before

        parent::__construct($name);
        if($this->countryRegionId == -1) return;

        $this->name = $this->currentName;
        $this->englishName                  = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['EnglishName'] ??  '';
        $this->displayName                  = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['DisplayName'] ??  '';
        $this->nativeName                   = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['NativeName'] ??  '';
        $this->twoLetterISORegionName       = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['TwoLetterISORegionName'] ?? '';
        $this->threeLetterISORegionName     = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['ThreeLetterISORegionName'] ??  Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['TwoLetterISORegionName'] ?? '';
        $this->currencyEnglishName          = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['CurrencyEnglishName'] ?? '';
        $this->currencyNativeName           = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['CurrencyNativeName'] ?? '';
        $this->currencySymbol               = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['CurrencySymbol'] ?? '';
        $this->ISOCurrencySymbol            = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['ISOCurrencySymbol'] ?? '';
        $this->isMetric                     = Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['IsMetric'] ?? '';

        $this->setupNumberFormat();
    }

    /**
     * Sets up the number format info instance.
     */
    private function setupNumberFormat()
    {
        $this->numberFormat = new NumberFormatInfo();
        $this->numberFormat->setCurrencyDecimalDigits(Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['numberFormatting']['currencyDecimalDigits']);
        $this->numberFormat->setCurrencyDecimalSeparator(Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['numberFormatting']['currencyDecimalSeparator']);
        $this->numberFormat->setCurrencyGroupSeparator(Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['numberFormatting']['currencyGroupSeparator']);
        $this->numberFormat->setCurrencyGroupSizes(Culture::DATA[$this->cultureId]['country/regions'][$this->countryRegionId]['numberFormatting']['currencyGroupSizes']);
    }

    /**
     * Parses the region name. It must either be a two-letter region name, such as "US" for the United States,
     * or the name of a specific culture, such as "en-US" for English (United States)
     *
     * @param string $name
     */
    function parseName(string $name)
    {
        if ($name !== "") {
            $explodedName = explode('-', $name);
            $count = count($explodedName);
            if ($count == 2) {
                $this->languageCode2 = strtolower($explodedName[0]);
                $this->countryOrRegionCode2 = strtolower($explodedName[1]);
                return;
            } elseif ($count == 1) {
                $this->countryOrRegionCode2 = strtolower($explodedName[0]);
                return;
            }
        } else {
            return;
        }

        throw new \RuntimeException('The name was invalid it must either be a two-letter region name, such as "US" for the United States, or the name of a specific culture, such as "en-US" for English (United States). Was: '.$name);
    }

    /**
     * Looks up the region code in the culture data arrays country/region arrays.
     * If the language code was specified in the name it wil set the culture and country region id to
     * the culture and region id which have both the language code and region name.
     * Else it will set the culture and region id to the culture which has the specified region name,
     * without taking the language code into account. Simply because it was not defined.
     *
     * AbstractCultureData and stores the ID
     */
    protected function setId()
    {
        foreach(Culture::DATA as $cultureId => $cultureData) {
            foreach(Culture::DATA[$cultureId]['country/regions'] as $countryRegionId => $cultureRegionData) {
                if (isset($cultureRegionData['TwoLetterISORegionName']) && strtoupper($cultureRegionData['TwoLetterISORegionName']) == strtoupper($this->countryOrRegionCode2)) {
                    if(isset($cultureData['ISO-639-1']) && strtoupper($cultureData['ISO-639-1']) == strtoupper($this->languageCode2)) {
                        $this->cultureId = $cultureId;
                        $this->countryRegionId = $countryRegionId;
                    } else {
                        $this->cultureId = $cultureId;
                        $this->countryRegionId = $countryRegionId;
                    }
                }
            }
        }
    }

    /**
     * @return string
     */
    public function getDisplayName(): string
    {
        return $this->displayName;
    }

    /**
     * @return string
     */
    public function getEnglishName(): string
    {
        return $this->englishName;
    }

    /**
     * @return string
     */
    public function getNativeName(): string
    {
        return $this->nativeName;
    }

    /**
     * @return string
     */
    public function getCurrencyEnglishName(): string
    {
        return $this->currencyEnglishName;
    }

    /**
     * @return string
     */
    public function getCurrencyNativeName(): string
    {
        return $this->currencyNativeName;
    }

    /**
     * @return string
     */
    public function getCurrencySymbol(): string
    {
        return $this->currencySymbol;
    }

    /**
     * @return string
     */
    public function getISOCurrencySymbol(): string
    {
        return $this->ISOCurrencySymbol;
    }

    /**
     * @return bool
     */
    public function isMetric(): bool
    {
        return $this->isMetric;
    }

    /**
     * @return string
     */
    public function getThreeLetterISORegionName(): string
    {
        return $this->threeLetterISORegionName;
    }

    /**
     * @return string
     */
    public function getTwoLetterISORegionName(): string
    {
        return $this->twoLetterISORegionName;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @return int
     */
    public function getCountryRegionId(): int
    {
        return $this->countryRegionId;
    }

    /** @return NumberFormatInfo */
    public function getNumberFormat(): NumberFormatInfo
    {
        return $this->numberFormat;
    }
}