ibericode / vat

PHP library for dealing with European VAT
https://packagist.org/packages/ibericode/vat
MIT License
425 stars 61 forks source link

add hasPattern to Validator #40

Open gemal opened 3 years ago

gemal commented 3 years ago

being able to check if here is a pattern for a country would it possible to add fx VAT number format check for Norway

so I could first do a check for "hasPattern" and then call the validateVatNumberFormat if hasPattern returned true

Today all numbers from Norway return false due to this check:

        if (! isset($this->patterns[$country])) {
            return false;
        }
gemal commented 3 years ago

Let me know if you accept pull requests? Dont want to waste time... :)

dannyvankooten commented 2 years ago

Perhaps we should throw an Exception if calling the validate method using an unsupported country code prefix?

Currently the library assumes that the validation method is only called after the isCountryCodeInEU( $countryCode ) method on an instance of the Countries class is called. I agree that we should probably design this in a better way... Open to any ideas you might have!

VincentLanglet commented 1 year ago

Perhaps we should throw an Exception if calling the validate method using an unsupported country code prefix?

This would require a new major version since it's a BC break.

Also, every support of new countries could be considered as a BC break like https://github.com/ibericode/vat/pull/57

Maybe we should let the behavior be decided by the user, with

public function validateVatNumberFormat(string $vatNumber, bool $failIfUnknown = true): bool
{
    // ...
    if (! isset($this->patterns[$country])) {
        return !$failIfUnknown;
    }
    // ...
}

Still, exposing a method to know if the Vat country is supported could be useful

Currently the library assumes that the validation method is only called after the isCountryCodeInEU( $countryCode ) method on an instance of the Countries class is called.

If so, there is currently no call in the VatBundle https://github.com/ibericode/vat-bundle/blob/32ab2529929952b5921e71763390f7453ca03423/src/Validator/Constraints/VatNumberValidator.php#L15-L51

And the methods/constraints are called validVatNumber and not validEUVatNumber which is an issue.

indykoning commented 10 months ago

Somewhat within the same scope i would love to be able to get the patterns themselves per country in some way. Usecase being the ability to add quick pre-validation using the html "pattern" attribute before making the call to validate the vat id