https://github.com/ddeboer/vatin-bundle
Symfony bundle for the VATIN library
https://github.com/ddeboer/vatin-bundle
Last synced: 8 months ago
JSON representation
Symfony bundle for the VATIN library
- Host: GitHub
- URL: https://github.com/ddeboer/vatin-bundle
- Owner: ddeboer
- License: mit
- Created: 2012-05-31T13:39:12.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2025-02-27T22:17:39.000Z (10 months ago)
- Last Synced: 2025-03-30T05:03:31.251Z (9 months ago)
- Language: PHP
- Homepage:
- Size: 46.9 KB
- Stars: 28
- Watchers: 6
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-symfony - vatin-bundle - A Symfony2 bundle for the VATIN library (validate VAT identification numbers). (Validation)
README
VATIN bundle
============

A Symfony bundle for the [VATIN library](https://github.com/ddeboer/vatin).
Installation
------------
This library is available on [Packagist](https://packagist.org/packages/ddeboer/vatin-bundle):
```bash
composer require ddeboer/vatin-bundle
```
Then add the bundle to your application:
```php
// app/AppKernel.php
public function registerBundles()
{
return [
...
new Ddeboer\VatinBundle\DdeboerVatinBundle(),
...
];
}
```
Usage
-----
### Validate number format
Use the validator to validate a property on your models.
```php
use Ddeboer\VatinBundle\Validator\Constraints\Vatin;
class Company
{
#[Vatin]
protected string $vatNumber;
```
Symfony’s validator will now check whether `$vatNumber` has a valid VAT number
format. For more information, see [Symfony’s documentation](http://symfony.com/doc/current/book/validation.html).
### Validate number existence
Additionally, you can check whether the VAT number is in use:
```php
use Ddeboer\VatinBundle\Validator\Constraints\Vatin;
#[Vatin(checkExistence=true)]
protected string $vatNumber;
```
The validator will now check the VAT number against the
[VAT Information Exchange System (VIES)](http://ec.europa.eu/taxation_customs/vies/faq.html)
SOAP web service. This service’s availability is rather unreliable, so it’s a
good idea to catch the case where it’s unreachable:
```php
use Symfony\Component\Validator\Exception\ValidatorException;
try {
if ($validator->isValid()) {
// Happy flow
}
} catch (ValidatorException $e) {
// VAT could not be validated because VIES service is unreachable
}
```
### Using the services directly
You can also use this bundle’s services directly. Validate a VAT number’s format:
```php
$validator = $container->get('ddeboer_vatin.vatin_validator');
$bool = $validator->isValid('NL123456789B01');
```
Additionally, check whether the VAT number is in use:
```php
$bool = $validator->isValid('NL123456789B01', true);
```
To interact with the VIES webservice:
```php
$vies = $container->get('ddeboer_vatin.vies.client');
$checkVatResponse = $vies->checkVat('NL', '123456789B01');
```
More information
----------------
For more information, see the [VATIN library’s documentation](https://github.com/ddeboer/vatin).