Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable existence check #8

Merged
merged 1 commit into from
Nov 14, 2023
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
2 changes: 2 additions & 0 deletions src/Validator/Constraints/VatNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 11 additions & 7 deletions src/Validator/Constraints/VatNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down