PHP code example of rocketfellows / country-vat-number-format-validator

1. Go to this page and download the library: Download rocketfellows/country-vat-number-format-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rocketfellows / country-vat-number-format-validator example snippets


$validatorService = new VatNumberFormatValidatorService(
    new CountryVatNumberFormatValidatorsConfigs(
        new SKVatNumberFormatValidatorsConfig(),
        new SIVatNumberFormatValidatorsConfig(),
        new SEVatNumberFormatValidatorsConfig(),
        new RUVatNumberFormatValidatorsConfig(),
        new ROVatNumberFormatValidatorsConfig(),
        new PTVatNumberFormatValidatorsConfig(),
        new PLVatNumberFormatValidatorsConfig(),
        new NOVatNumberFormatValidatorsConfig(),
        new NLVatNumberFormatValidatorsConfig(),
        new MTVatNumberFormatValidatorsConfig(),
        new LVVatNumberFormatValidatorsConfig(),
        new LUVatNumberFormatValidatorsConfig(),
        new LTVatNumberFormatValidatorsConfig(),
        new ITVatNumberFormatValidatorsConfig(),
        new IEVatNumberFormatValidatorsConfig(),
        new HUVatNumberFormatValidatorsConfig(),
        new HRVatNumberFormatValidatorsConfig(),
        new GRVatNumberFormatValidatorsConfig(),
        new GBVatNumberFormatValidatorsConfig(),
        new FRVatNumberFormatValidatorsConfig(),
        new FIVatNumberFormatValidatorsConfig(),
        new ESVatNumberFormatValidatorsConfig(),
        new EEVatNumberFormatValidatorsConfig(),
        new DKVatNumberFormatValidatorsConfig(),
        new DEVatNumberFormatValidatorsConfig(),
        new CZVatNumberFormatValidatorsConfig(),
        new CYVatNumberFormatValidatorsConfig(),
        new CHEVatNumberFormatValidatorsConfig(),
        new BGVatNumberFormatValidatorsConfig(),
        new BEVatNumberFormatValidatorsConfig(),
        new ATVatNumberFormatValidatorsConfig()
    ),
    new CountryFactory()
);

// country code case-insensitive
$result = $validatorService->validateCountryVatNumber('at', 'ATU62181819');

var_dump($result);

// country code case-insensitive
$result = $validatorService->validateCountryVatNumber('de', 'DE282308599');

var_dump($result);

// country code case-insensitive
$result = $validatorService->validateCountryVatNumber('at', 'foo');

var_dump($result);

// country code case-insensitive
$result = $validatorService->validateCountryVatNumber('de', 'foo');

var_dump($result);

$validator = new VatNumberFormatValidator(
    new VatNumberFormatValidatorService(
        new CountryVatNumberFormatValidatorsConfigs(
            new SKVatNumberFormatValidatorsConfig(),
            new SIVatNumberFormatValidatorsConfig(),
            new SEVatNumberFormatValidatorsConfig(),
            new RUVatNumberFormatValidatorsConfig(),
            new ROVatNumberFormatValidatorsConfig(),
            new PTVatNumberFormatValidatorsConfig(),
            new PLVatNumberFormatValidatorsConfig(),
            new NOVatNumberFormatValidatorsConfig(),
            new NLVatNumberFormatValidatorsConfig(),
            new MTVatNumberFormatValidatorsConfig(),
            new LVVatNumberFormatValidatorsConfig(),
            new LUVatNumberFormatValidatorsConfig(),
            new LTVatNumberFormatValidatorsConfig(),
            new ITVatNumberFormatValidatorsConfig(),
            new IEVatNumberFormatValidatorsConfig(),
            new HUVatNumberFormatValidatorsConfig(),
            new HRVatNumberFormatValidatorsConfig(),
            new GRVatNumberFormatValidatorsConfig(),
            new GBVatNumberFormatValidatorsConfig(),
            new FRVatNumberFormatValidatorsConfig(),
            new FIVatNumberFormatValidatorsConfig(),
            new ESVatNumberFormatValidatorsConfig(),
            new EEVatNumberFormatValidatorsConfig(),
            new DKVatNumberFormatValidatorsConfig(),
            new DEVatNumberFormatValidatorsConfig(),
            new CZVatNumberFormatValidatorsConfig(),
            new CYVatNumberFormatValidatorsConfig(),
            new CHEVatNumberFormatValidatorsConfig(),
            new BGVatNumberFormatValidatorsConfig(),
            new BEVatNumberFormatValidatorsConfig(),
            new ATVatNumberFormatValidatorsConfig()
        ),
        new CountryFactory()
    )
);

$result = $validator->isValid('at', 'ATU62181819');
var_dump($result);

$result = $validator->isValid('at', 'ATU61819');
var_dump($result);

namespace rocketfellows\CountryVatNumberFormatValidator\qa;

use rocketfellows\CountryVatFormatValidatorInterface\CountryVatFormatValidator;

class QAVatFormatValidator extends CountryVatFormatValidator
{
    private const VAT_NUMBER_PATTERN = '/^\d{3}$/';

    protected function isValidFormat(string $vatNumber): bool
    {
        return (bool) preg_match(self::VAT_NUMBER_PATTERN, $vatNumber);
    }
}

namespace rocketfellows\CountryVatNumberFormatValidator\qa;

use arslanimamutdinov\ISOStandard3166\Country;
use arslanimamutdinov\ISOStandard3166\ISO3166;
use rocketfellows\CountryVatFormatValidatorInterface\CountryVatFormatValidatorInterface;
use rocketfellows\CountryVatFormatValidatorInterface\CountryVatFormatValidators;
use rocketfellows\CountryVatNumberFormatValidatorsConfig\CountryVatNumberFormatValidatorsConfigInterface;

class QAVatNumberFormatValidatorsConfig implements CountryVatNumberFormatValidatorsConfigInterface
{
    private $validators;
    
    public function __construct(CountryVatFormatValidatorInterface $validator)
    {
        $this->validators = new CountryVatFormatValidators($validator);
    }

    public function getCountry(): Country
    {
        return ISO3166::QA();
    }

    public function getValidators(): CountryVatFormatValidators
    {
        return $this->validators;
    }
}

$validatorService = new VatNumberFormatValidatorService(
    new CountryVatNumberFormatValidatorsConfigs(
        new QAVatNumberFormatValidatorsConfig(new QAVatFormatValidator()),
        new ATVatNumberFormatValidatorsConfig(),
        new DEVatNumberFormatValidatorsConfig()
    ),
    new CountryFactory()
);

$result = $validatorService->validateCountryVatNumber('qa', '123');
var_dump($result);

$result = $validatorService->validateCountryVatNumber('qa', 'foo');
var_dump($result);