mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
This commit introduces REUSE compliance by annotating all files with SPDX information and placing the reused licences in the LICENSES folder. We additionally removed the docheader tool which is made obsolete by this change. The main LICENSE and copyright text of the project is now not under my personal name anymore, and it belongs to "The Respect Project Contributors" instead. This change restores author names to several files, giving the appropriate attribution for contributions.
72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use Attribute;
|
|
use Respect\Validation\Exceptions\InvalidValidatorException;
|
|
use Respect\Validation\Exceptions\MissingComposerDependencyException;
|
|
use Respect\Validation\Helpers\CanValidateUndefined;
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Validator;
|
|
use Sokil\IsoCodes\Database\Countries;
|
|
use Sokil\IsoCodes\Database\Subdivisions;
|
|
|
|
use function class_exists;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'{{subject}} must be a subdivision code of {{countryName|trans}}',
|
|
'{{subject}} must not be a subdivision code of {{countryName|trans}}',
|
|
)]
|
|
final readonly class SubdivisionCode implements Validator
|
|
{
|
|
use CanValidateUndefined;
|
|
|
|
private Countries\Country $country;
|
|
|
|
private Subdivisions $subdivisions;
|
|
|
|
public function __construct(
|
|
string $countryCode,
|
|
Countries|null $countries = null,
|
|
Subdivisions|null $subdivisions = null,
|
|
) {
|
|
if (!class_exists(Countries::class) || !class_exists(Subdivisions::class)) {
|
|
throw new MissingComposerDependencyException(
|
|
'SubdivisionCode rule requires PHP ISO Codes',
|
|
'sokil/php-isocodes',
|
|
'sokil/php-isocodes-db-only',
|
|
);
|
|
}
|
|
|
|
$countries ??= new Countries();
|
|
$country = $countries->getByAlpha2($countryCode);
|
|
if ($country === null) {
|
|
throw new InvalidValidatorException('"%s" is not a supported country code', $countryCode);
|
|
}
|
|
|
|
$this->country = $country;
|
|
$this->subdivisions = $subdivisions ?? new Subdivisions();
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$parameters = ['countryName' => $this->country->getName()];
|
|
$subdivision = $this->subdivisions->getByCode($this->country->getAlpha2() . '-' . $input);
|
|
if ($this->isUndefined($input) && $subdivision === null) {
|
|
return Result::passed($input, $this, $parameters);
|
|
}
|
|
|
|
return Result::of($subdivision !== null, $input, $this, $parameters);
|
|
}
|
|
}
|