Skip to content

Commit

Permalink
Merge pull request #9 from jkobus/service-replacement-for-symfony-val…
Browse files Browse the repository at this point in the history
…idator

Use DI to replace validator service used in symfony validator
  • Loading branch information
dannyvankooten authored Dec 3, 2023
2 parents b1c34c9 + 1c79eac commit bc9eba5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"symfony/validator": "^5.4|^6.0"
},
"require-dev": {
"ext-intl": "*",
"ext-soap": "*",
"phpunit/phpunit": "^10.0"
},
"autoload": {
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<service id="ibericode_vat.geolocator" class="Ibericode\Vat\Geolocator">
<argument key="$service">%ibericode_vat.geolocator.service%</argument>
</service>
<service id="Ibericode\Vat\Bundle\Validator\Constraints\VatNumberValidator">
<argument key="$validator">%ibericode_vat.validator%</argument>
</service>

<!-- autowiring class names -->
<service id="Ibericode\Vat\Countries" alias="ibericode_vat.countries" />
Expand Down
9 changes: 6 additions & 3 deletions src/Validator/Constraints/VatNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -28,18 +32,17 @@ 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,
// but it's (probably) better than a hard-error
$valid = true;
}
} else {
$valid = $validator->validateVatNumberFormat($value);
$valid = $this->validator->validateVatNumberFormat($value);
}

if (false === $valid) {
Expand Down
2 changes: 2 additions & 0 deletions tests/DependencyInjection/VatExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
}

0 comments on commit bc9eba5

Please sign in to comment.