mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
Currently, we convert the properties of a rule into parameters and pass them to the exceptions. That complicates things for a few reasons: 1. The exception knows too much: there's a lot of information in an object, and the exception would only need a few parameters to work correctly. 2. Any variable change becomes a backward compatibility break: if we change the name of the variable type in a rule, even if it's a private one, we may need to change the template, which is a backward compatibility break. 3. The factory is bloated because of introspection tricks: it reads the properties from the class, even from the parent, and then passes it to the exception. Of course, that means we introduce another method to `Validatable`, but in most cases, extending `AbstractRule` is enough to create a new rule. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
52 lines
1.1 KiB
PHP
52 lines
1.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Respect\Validation\Helpers\CountryInfo;
|
|
|
|
use function array_keys;
|
|
|
|
/**
|
|
* @see http://en.wikipedia.org/wiki/ISO_3166-2
|
|
* @see http://www.geonames.org/countries/
|
|
*/
|
|
final class SubdivisionCode extends AbstractSearcher
|
|
{
|
|
private readonly string $countryName;
|
|
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private readonly array $countryInfo;
|
|
|
|
public function __construct(string $countryCode)
|
|
{
|
|
$countryInfo = new CountryInfo($countryCode);
|
|
|
|
$this->countryName = $countryInfo->getCountry();
|
|
$this->countryInfo = array_keys($countryInfo->getSubdivisions());
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function getParams(): array
|
|
{
|
|
return ['countryName' => $this->countryName];
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
protected function getDataSource(mixed $input = null): array
|
|
{
|
|
return $this->countryInfo;
|
|
}
|
|
}
|