From c8e17a06208dd6bd27bd8dc3c28790170438906a Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 14 Nov 2023 16:20:20 +0100 Subject: [PATCH] Add option to disable existence check --- src/Validator/Constraints/VatNumber.php | 2 ++ .../Constraints/VatNumberValidator.php | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Validator/Constraints/VatNumber.php b/src/Validator/Constraints/VatNumber.php index 7ccbc3a..30a67d2 100644 --- a/src/Validator/Constraints/VatNumber.php +++ b/src/Validator/Constraints/VatNumber.php @@ -10,4 +10,6 @@ class VatNumber extends Constraint public const INVALID_ERROR_CODE = '59421d43-d474-489c-b18c-7701329d51a0'; public string $message = '"{{ string }}" does not look like a valid VAT number.'; + + public bool $checkExistence = true; } diff --git a/src/Validator/Constraints/VatNumberValidator.php b/src/Validator/Constraints/VatNumberValidator.php index d241538..95b1caa 100644 --- a/src/Validator/Constraints/VatNumberValidator.php +++ b/src/Validator/Constraints/VatNumberValidator.php @@ -29,13 +29,17 @@ public function validate(mixed $value, Constraint $constraint) : void } $validator = new Validator(); - try { - $valid = $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; + if ($constraint->checkExistence) { + try { + $valid = $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); } if (false === $valid) {