2015-09-21 18:28:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-09-21 18:28:59 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-09-21 18:28:59 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2021-02-06 14:19:35 +01:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Sokil\IsoCodes\IsoCodesFactory;
|
|
|
|
use Sokil\IsoCodes\TranslationDriver\DummyDriver;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2021-02-06 14:19:35 +01:00
|
|
|
use function is_string;
|
|
|
|
use function sprintf;
|
2019-03-11 00:40:14 +01:00
|
|
|
|
2015-09-21 18:28:59 +02:00
|
|
|
/**
|
|
|
|
* Validates country subdivision codes according to ISO 3166-2.
|
|
|
|
*
|
2017-11-19 19:01:31 +01:00
|
|
|
* @see http://en.wikipedia.org/wiki/ISO_3166-2
|
|
|
|
* @see http://www.geonames.org/countries/
|
2018-01-28 13:14:15 +01:00
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2019-03-11 00:40:14 +01:00
|
|
|
* @author Mazen Touati <mazen_touati@hotmail.com>
|
2015-09-21 18:28:59 +02:00
|
|
|
*/
|
2021-02-06 14:19:35 +01:00
|
|
|
final class SubdivisionCode extends AbstractRule
|
2015-09-21 18:28:59 +02:00
|
|
|
{
|
2021-02-06 14:19:35 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $countryCode;
|
|
|
|
|
2019-03-11 00:40:14 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $countryName;
|
|
|
|
|
|
|
|
/**
|
2021-02-06 14:19:35 +01:00
|
|
|
* @var IsoCodesFactory
|
2019-03-11 00:40:14 +01:00
|
|
|
*/
|
2021-02-06 14:19:35 +01:00
|
|
|
private $factory;
|
2019-03-11 00:40:14 +01:00
|
|
|
|
|
|
|
public function __construct(string $countryCode)
|
|
|
|
{
|
2021-02-06 14:19:35 +01:00
|
|
|
$factory = new IsoCodesFactory(null, new DummyDriver());
|
|
|
|
$country = $factory->getCountries()->getByAlpha2($countryCode);
|
|
|
|
if ($country === null) {
|
|
|
|
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
|
|
|
|
}
|
2019-03-11 00:40:14 +01:00
|
|
|
|
2021-02-06 14:19:35 +01:00
|
|
|
$this->factory = $factory;
|
|
|
|
$this->countryCode = $countryCode;
|
|
|
|
$this->countryName = $country->getName();
|
2019-03-11 00:40:14 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 13:14:15 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-01-28 13:14:15 +01:00
|
|
|
*/
|
2021-02-06 14:19:35 +01:00
|
|
|
public function validate($input): bool
|
2015-09-21 18:28:59 +02:00
|
|
|
{
|
2021-02-06 14:19:35 +01:00
|
|
|
if (!is_string($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->factory->getSubdivisions()->getByCode($this->countryCode . '-' . $input) !== null;
|
2015-09-21 18:28:59 +02:00
|
|
|
}
|
|
|
|
}
|