mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
6173757f63
Previously, we were loading country info from a JSON file. This changes it to use PHP files instead. It also caches these resources across calls avoiding these files to be loaded more than once per process.
55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Helpers;
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use function file_exists;
|
|
use function sprintf;
|
|
|
|
final class CountryInfo
|
|
{
|
|
/**
|
|
* @var mixed[]
|
|
*/
|
|
private $data;
|
|
|
|
/**
|
|
* @var mixed[]
|
|
*/
|
|
private static $runtimeCache = [];
|
|
|
|
public function __construct(string $countryCode)
|
|
{
|
|
if (!isset(static::$runtimeCache[$countryCode])) {
|
|
$filename = __DIR__ . '/../../data/iso_3166-2/' . $countryCode . '.php';
|
|
if (!file_exists($filename)) {
|
|
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
|
|
}
|
|
static::$runtimeCache[$countryCode] = require $filename;
|
|
}
|
|
|
|
$this->data = static::$runtimeCache[$countryCode];
|
|
}
|
|
|
|
public function getCountry(): string
|
|
{
|
|
return $this->data['country'];
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
public function getSubdivisions(): array
|
|
{
|
|
return $this->data['subdivisions'];
|
|
}
|
|
}
|