respect-validation/library/Helpers/CountryInfo.php
Alexandre Gomes Gaigalas cc3bf86b2f Use libphonenumber
Doing regex on phone numbers is not a great idea. This is a breaking
change, but a good one. Phone validation is now much stricter, and
allows choosing the country.
2023-02-19 00:19:10 -03:00

49 lines
1 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 file_get_contents;
use function json_decode;
use function sprintf;
final class CountryInfo
{
/**
* @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'];
}
}