2021-03-19 14:59:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2021-03-19 14:59:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Helpers;
|
|
|
|
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
|
|
|
|
use function file_exists;
|
|
|
|
use function file_get_contents;
|
|
|
|
use function json_decode;
|
|
|
|
use function sprintf;
|
|
|
|
|
2023-02-18 22:09:26 +01:00
|
|
|
final class CountryInfo
|
2021-03-19 14:59:31 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var mixed[]
|
|
|
|
*/
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
public function __construct(string $countryCode)
|
|
|
|
{
|
|
|
|
$filename = __DIR__ . '/../../data/iso_3166-2/' . $countryCode . '.json';
|
|
|
|
if (!file_exists($filename)) {
|
|
|
|
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data = (array) json_decode((string) file_get_contents($filename), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCountry(): string
|
|
|
|
{
|
|
|
|
return $this->data['country'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getSubdivisions(): array
|
|
|
|
{
|
|
|
|
return $this->data['subdivisions'];
|
|
|
|
}
|
|
|
|
}
|