Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/Validators/CountryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
namespace Respect\Validation\Validators;

use Attribute;
use Psr\Container\NotFoundExceptionInterface;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Validator;
use Sokil\IsoCodes\Database\Countries;

use function class_exists;
use function in_array;
use function is_string;

Expand All @@ -43,14 +44,6 @@ public function __construct(
private string $set = 'alpha-2',
Countries|null $countries = null,
) {
if (!class_exists(Countries::class)) {
throw new MissingComposerDependencyException(
'SubdivisionCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}

$availableOptions = ['alpha-2', 'alpha-3', 'numeric'];
if (!in_array($set, $availableOptions, true)) {
throw new InvalidValidatorException(
Expand All @@ -60,7 +53,15 @@ public function __construct(
);
}

$this->countries = $countries ?? new Countries();
try {
$this->countries = $countries ?? ContainerRegistry::getContainer()->get(Countries::class);
} catch (NotFoundExceptionInterface) {
throw new MissingComposerDependencyException(
'CountryCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}
}

public function evaluate(mixed $input): Result
Expand Down
21 changes: 11 additions & 10 deletions src/Validators/CurrencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
namespace Respect\Validation\Validators;

use Attribute;
use Psr\Container\NotFoundExceptionInterface;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Validator;
use Sokil\IsoCodes\Database\Currencies;

use function class_exists;
use function in_array;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
Expand All @@ -39,14 +40,6 @@ public function __construct(
private string $set = 'alpha-3',
Currencies|null $currencies = null,
) {
if (!class_exists(Currencies::class)) {
throw new MissingComposerDependencyException(
'CurrencyCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}

$availableSets = ['alpha-3', 'numeric'];
if (!in_array($set, $availableSets, true)) {
throw new InvalidValidatorException(
Expand All @@ -56,7 +49,15 @@ public function __construct(
);
}

$this->currencies = $currencies ?? new Currencies();
try {
$this->currencies = $currencies ?? ContainerRegistry::getContainer()->get(Currencies::class);
} catch (NotFoundExceptionInterface) {
throw new MissingComposerDependencyException(
'CurrencyCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}
}

public function evaluate(mixed $input): Result
Expand Down
22 changes: 11 additions & 11 deletions src/Validators/LanguageCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
namespace Respect\Validation\Validators;

use Attribute;
use Psr\Container\NotFoundExceptionInterface;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Validator;
use Sokil\IsoCodes\Database\Countries;
use Sokil\IsoCodes\Database\Languages;

use function class_exists;
use function in_array;
use function is_string;

Expand All @@ -41,14 +41,6 @@ public function __construct(
private readonly string $set = 'alpha-2',
Languages|null $languages = null,
) {
if (!class_exists(Countries::class)) {
throw new MissingComposerDependencyException(
'LanguageCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}

$availableSets = ['alpha-2', 'alpha-3'];
if (!in_array($set, $availableSets, true)) {
throw new InvalidValidatorException(
Expand All @@ -58,7 +50,15 @@ public function __construct(
);
}

$this->languages = $languages ?? new Languages();
try {
$this->languages = $languages ?? ContainerRegistry::getContainer()->get(Languages::class);
} catch (NotFoundExceptionInterface) {
throw new MissingComposerDependencyException(
'LanguageCode rule requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}
}

public function evaluate(mixed $input): Result
Expand Down
7 changes: 5 additions & 2 deletions src/Validators/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Attribute;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
use Psr\Container\NotFoundExceptionInterface;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Message\Template;
Expand Down Expand Up @@ -64,15 +66,16 @@ public function __construct(string|null $countryCode = null, Countries|null $cou
return;
}

if (!class_exists(Countries::class)) {
try {
$countries ??= ContainerRegistry::getContainer()->get(Countries::class);
} catch (NotFoundExceptionInterface) {
throw new MissingComposerDependencyException(
'Phone rule with country code requires PHP ISO Codes',
'sokil/php-isocodes',
'sokil/php-isocodes-db-only',
);
}

$countries ??= new Countries();
$this->country = $countries->getByAlpha2($countryCode);
if ($this->country === null) {
throw new InvalidValidatorException('Invalid country code %s', $countryCode);
Expand Down
12 changes: 7 additions & 5 deletions src/Validators/SubdivisionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Respect\Validation\Validators;

use Attribute;
use Psr\Container\NotFoundExceptionInterface;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Helpers\CanValidateUndefined;
Expand All @@ -21,8 +23,6 @@
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}}',
Expand All @@ -41,22 +41,24 @@ public function __construct(
Countries|null $countries = null,
Subdivisions|null $subdivisions = null,
) {
if (!class_exists(Countries::class) || !class_exists(Subdivisions::class)) {
try {
$container = ContainerRegistry::getContainer();
$countries ??= $container->get(Countries::class);
$this->subdivisions = $subdivisions ?? $container->get(Subdivisions::class);
} catch (NotFoundExceptionInterface) {
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
Expand Down
69 changes: 69 additions & 0 deletions tests/benchmark/IsoCodesBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
*/

declare(strict_types=1);

namespace Respect\Validation\Benchmarks;

use PhpBench\Attributes as Bench;
use Respect\Validation\Test\SmokeTestProvider;
use Respect\Validation\ValidatorBuilder;

class IsoCodesBench
{
use SmokeTestProvider;

#[Bench\Iterations(10)]
#[Bench\RetryThreshold(5)]
#[Bench\Revs(5)]
#[Bench\Warmup(1)]
#[Bench\Subject]
public function subdivisionCode(): void
{
ValidatorBuilder::subdivisionCode('US')->evaluate('CA');
}

#[Bench\Iterations(10)]
#[Bench\RetryThreshold(5)]
#[Bench\Revs(5)]
#[Bench\Warmup(1)]
#[Bench\Subject]
public function countryCode(): void
{
ValidatorBuilder::countryCode()->evaluate('US');
}

#[Bench\Iterations(10)]
#[Bench\RetryThreshold(5)]
#[Bench\Revs(5)]
#[Bench\Warmup(1)]
#[Bench\Subject]
public function currencyCode(): void
{
ValidatorBuilder::currencyCode()->evaluate('USD');
}

#[Bench\Iterations(10)]
#[Bench\RetryThreshold(5)]
#[Bench\Revs(5)]
#[Bench\Warmup(1)]
#[Bench\Subject]
public function languageCode(): void
{
ValidatorBuilder::languageCode()->evaluate('en');
}

#[Bench\Iterations(10)]
#[Bench\RetryThreshold(5)]
#[Bench\Revs(5)]
#[Bench\Warmup(1)]
#[Bench\Subject]
public function phone(): void
{
ValidatorBuilder::phone('US')->evaluate('+1 202-555-0125');
}
}
21 changes: 21 additions & 0 deletions tests/unit/Validators/CountryCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

namespace Respect\Validation\Validators;

use DI;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Test\RuleTestCase;

#[Group('validator')]
Expand All @@ -36,6 +39,24 @@ public function itShouldThrowsExceptionWhenInvalidFormat(): void
new CountryCode('whatever');
}

#[Test]
public function shouldThrowWhenMissingComponent(): void
{
$mainContainer = ContainerRegistry::getContainer();
ContainerRegistry::setContainer((new DI\ContainerBuilder())->useAutowiring(false)->build());
try {
new CountryCode('alpha-3');
$this->fail('Expected MissingComposerDependencyException was not thrown.');
} catch (MissingComposerDependencyException $e) {
$this->assertStringContainsString(
'CountryCode rule requires PHP ISO Codes',
$e->getMessage(),
);
} finally {
ContainerRegistry::setContainer($mainContainer);
}
}

/** @return iterable<array{CountryCode, mixed}> */
public static function providerForValidInput(): iterable
{
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Validators/CurrencyCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

namespace Respect\Validation\Validators;

use DI;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\ContainerRegistry;
use Respect\Validation\Exceptions\InvalidValidatorException;
use Respect\Validation\Exceptions\MissingComposerDependencyException;
use Respect\Validation\Test\RuleTestCase;

#[Group('validator')]
Expand All @@ -36,6 +39,24 @@ public function itShouldThrowsExceptionWhenInvalidFormat(): void
new CurrencyCode('whatever');
}

#[Test]
public function shouldThrowWhenMissingComponent(): void
{
$mainContainer = ContainerRegistry::getContainer();
ContainerRegistry::setContainer((new DI\ContainerBuilder())->useAutowiring(false)->build());
try {
new CurrencyCode('alpha-3');
$this->fail('Expected MissingComposerDependencyException was not thrown.');
} catch (MissingComposerDependencyException $e) {
$this->assertStringContainsString(
'CurrencyCode rule requires PHP ISO Codes',
$e->getMessage(),
);
} finally {
ContainerRegistry::setContainer($mainContainer);
}
}

/** @return iterable<array{CurrencyCode, mixed}> */
public static function providerForValidInput(): iterable
{
Expand Down
Loading
Loading