mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 05:12:50 +02:00
718bacad04
There is no much benefit from having individual rules for each country's subdivision, quite the opposite. It increases the amount of code and makes it hard to change the implementation of these rules. Right now, the only sane way to change those rules is with a customized script. This commit will remove the Subdivision Code rules per country and instead will put that information into JSON files. We both wouldn't like to keep this in this library anymore, and we are considering having another library to deal with this data [1], but since it seems like it may take some time, looks better to do it temporarily here. [1]: https://github.com/sokil/php-isocodes/issues/12 Co-authored-by: Mazen Touati <mazen_touati@hotmail.com> Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Respect/Validation.
|
|
*
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
*
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Respect\Validation\Helpers\Subdivisions;
|
|
use function array_keys;
|
|
|
|
/**
|
|
* Validates country subdivision codes according to ISO 3166-2.
|
|
*
|
|
* @see http://en.wikipedia.org/wiki/ISO_3166-2
|
|
* @see http://www.geonames.org/countries/
|
|
*
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
* @author Mazen Touati <mazen_touati@hotmail.com>
|
|
*/
|
|
final class SubdivisionCode extends AbstractSearcher
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $countryName;
|
|
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private $subdivisions;
|
|
|
|
public function __construct(string $countryCode)
|
|
{
|
|
$subdivisions = new Subdivisions($countryCode);
|
|
|
|
$this->countryName = $subdivisions->getCountry();
|
|
$this->subdivisions = array_keys($subdivisions->getSubdivisions());
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected function getDataSource(): array
|
|
{
|
|
return $this->subdivisions;
|
|
}
|
|
}
|