Skip to content

Commit

Permalink
Example vat
Browse files Browse the repository at this point in the history
  • Loading branch information
David Vandemaele committed Apr 3, 2018
1 parent 3dd599b commit 6fe1701
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/Skeleton/Vat/Check/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class Check {
public static function validate($vat_number, \Country $country, &$reason = '', $ignore_cache = false) {
// 1. Check syntax
if (!self::validate_syntax($vat_number, $country)) {
$reason = 'invalid syntax';
$reason = 'invalid_syntax';
return false;
}

// 2. Check if the vat is a european vat-number
if ($country->european != 1) {
$reason = 'country is not european';
$reason = 'not_european';
return true;
}

Expand All @@ -38,10 +38,10 @@ public static function validate($vat_number, \Country $country, &$reason = '', $
try {
$vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
if ($vat_cache->valid == 1) {
$reason = 'from cache';
$reason = 'valid_from_cache';
return true;
} else {
$reason = 'from cache';
$reason = 'invalid_from_cache';
return false;
}
} catch (\Exception $e) {}
Expand All @@ -61,9 +61,9 @@ public static function validate($vat_number, \Country $country, &$reason = '', $
$vat_cache->valid = $result['result'];
$vat_cache->save();
} elseif ($result['reachable'] === false) {
$reason = 'VIES service not reachable';
$reason = 'vies_service_not_reachable';
} else {
$reason = 'invalid vat number';
$reason = 'invalid_number';
}

return $result['result'];
Expand Down
33 changes: 33 additions & 0 deletions lib/Skeleton/Vat/Check/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,37 @@ class Config {
'SK' => '/^[0-9]{10}$/',
];

/**
* VAT examples
*/
public static $example_vat = [
'AT' => 'U12345678',
'BE' => '0123456789',
'BG' => '123456789',
'CY' => '12345678L',
'CZ' => '123456789',
'DE' => '123456789',
'DK' => '12345678',
'EE' => '123456789',
'EL' => '123456789',
'ES' => 'X9999999X',
'FI' => '12345678',
'FR' => '12123456789',
'GB' => '123123412',
'HU' => '12345678',
'IE' => '1S23456L',
'IT' => '12345678901',
'LT' => '123456789',
'LU' => '12345678',
'LV' => '12345678901',
'MT' => '12345678',
'NL' => '123412123B12',
'PL' => '1234567890',
'PT' => '123456789',
'RO' => '1234567890',
'SE' => '123456789012',
'SI' => '12345678',
'SK' => '1234567890',
];

}
46 changes: 46 additions & 0 deletions lib/Skeleton/Vat/Check/Twig/Extension/Vat/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Additional functions and filters for Twig
*
* @author David Vandemaele <[email protected]>
*/

namespace Skeleton\Vat\Check\Twig\Extension\Vat;

class Check extends \Twig_Extension {

/**
* Returns a list of functions
*
* @return array
*/
public function getFunctions() {
return array(
new \Twig_SimpleFunction('example_vat', array($this, 'get_example_vat'), array('is_safe' => array('html')))
);
}

/**
* Function get example vat
*
* @param string $vat_code
* @return string $output
*/
public function get_example_vat($vat_code) {
$vat_code = strtoupper($vat_code);
if (!isset(\Skeleton\Vat\Check\Config::$example_vat[$vat_code])) {
return 'unknown';
}

return \Skeleton\Vat\Check\Config::$example_vat[$vat_code];
}

/**
* Name of this extension
*
* @return string
*/
public function getName() {
return 'Twig_Extension_Vat_Check';
}
}

0 comments on commit 6fe1701

Please sign in to comment.