From cee5ae1519f5467a6ce806e351514eb03601f378 Mon Sep 17 00:00:00 2001 From: Jacek Kobus Date: Sun, 3 Dec 2023 14:12:20 +0100 Subject: [PATCH] Make VatNumberValidator use dependency injection instead of creating validator service instance in-place ("new") --- composer.json | 2 ++ src/Resources/config/services.xml | 4 ++++ src/Validator/Constraints/VatNumberValidator.php | 9 ++++++--- tests/DependencyInjection/VatExtensionTest.php | 2 ++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b29057e..5e9d016 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,8 @@ "symfony/validator": "^5.4|^6.0" }, "require-dev": { + "ext-intl": "*", + "ext-soap": "*", "phpunit/phpunit": "^10.0" }, "autoload": { diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 45c4a32..42b3ece 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -25,5 +25,9 @@ + + + %ibericode_vat.validator% + \ No newline at end of file diff --git a/src/Validator/Constraints/VatNumberValidator.php b/src/Validator/Constraints/VatNumberValidator.php index 95b1caa..920c83a 100644 --- a/src/Validator/Constraints/VatNumberValidator.php +++ b/src/Validator/Constraints/VatNumberValidator.php @@ -12,6 +12,10 @@ class VatNumberValidator extends ConstraintValidator { + public function __construct(private Validator $validator = new Validator()) + { + } + public function validate(mixed $value, Constraint $constraint) : void { if (!$constraint instanceof VatNumber) { @@ -28,10 +32,9 @@ public function validate(mixed $value, Constraint $constraint) : void throw new UnexpectedValueException($value, 'string'); } - $validator = new Validator(); if ($constraint->checkExistence) { try { - $valid = $validator->validateVatNumber($value); + $valid = $this->validator->validateVatNumber($value); } catch (ViesException $e) { // ignore VIES VAT exceptions (when the service is down) // this could mean that an unexisting VAT number passes validation, @@ -39,7 +42,7 @@ public function validate(mixed $value, Constraint $constraint) : void $valid = true; } } else { - $valid = $validator->validateVatNumberFormat($value); + $valid = $this->validator->validateVatNumberFormat($value); } if (false === $valid) { diff --git a/tests/DependencyInjection/VatExtensionTest.php b/tests/DependencyInjection/VatExtensionTest.php index 8dbe42c..d0f8482 100644 --- a/tests/DependencyInjection/VatExtensionTest.php +++ b/tests/DependencyInjection/VatExtensionTest.php @@ -2,6 +2,7 @@ namespace Ibericode\Vat\Bundle\Tests\DependencyInjection; +use Ibericode\Vat\Bundle\Validator\Constraints\VatNumberValidator; use Ibericode\Vat\Countries; use Ibericode\Vat\Geolocator; use Ibericode\Vat\Rates; @@ -26,5 +27,6 @@ public function testDefault() $this->assertTrue($container->has(Rates::class), 'Rates class is wired'); $this->assertTrue($container->has(Validator::class), 'Validator class is wired'); $this->assertTrue($container->has(Geolocator::class), 'Geolocator class is wired'); + $this->assertTrue($container->has(VatNumberValidator::class), 'VatNumberValidator class is wired'); } } \ No newline at end of file