mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 06:45:44 +01:00
Rename library/ to src/
We've always considered renaming this directory, as it's not a common standard to name `library` the directory where the source code of a library it. Having it as `src/` is a common pattern we find in several PHP libraries these days. Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
This commit is contained in:
parent
a372a0b90b
commit
140bd36aa3
273 changed files with 17 additions and 17 deletions
72
src/Validators/SubdivisionCode.php
Normal file
72
src/Validators/SubdivisionCode.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue