Skip to content

Commit

Permalink
# 2.0.1 - 2018-04-18
Browse files Browse the repository at this point in the history
### ADD:
##### validation:
- getCoutryCodeByVatNumberTest($val, $fallback): Try to extract EU country code in Vat number return $fallback if it fails.

### CHANGES:
##### validation:
- small refactor
  • Loading branch information
lopadova committed Apr 18, 2018
1 parent 1e6b861 commit 6b76155
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 23 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All Notable changes to `support` will be documented in this file

## 2.0.1 - 2018-04-18

### ADD:
##### validation:
- getCoutryCodeByVatNumberTest($val, $fallback): Try to extract EU country code in Vat number return $fallback if it fails.

### CHANGES:
##### validation:
- small refactor

## 2.0.0 - 2018-04-18

### BREAKING CHANGES:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ var_dump(str_random(16));
- urlW3c
- isVATRegisteredInVies() : Validate a European VAT number using the EU commission VIES service to verify if VAT number is authorized to carry out intra-Community operations must use the service.
- isITVat() : Check Italian Vat Number (Partita IVA).
- isEuVatNumber() : Check if a valid EU vat given against specific EU country vat rules (at the moment only for italy) and optionally check the EU VIES service.
- isEuVatNumber() : Check if a valid EU vat given against specific EU country vat rules (at the moment only for italy) and optionally check the EU VIES service.
- getCoutryCodeByVatNumberTest($val, $fallback): Try to extract EU country code in Vat number return $fallback if it fails.
- isCf
- isAlpha
- isAlphaNumeric
Expand Down
50 changes: 28 additions & 22 deletions src/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,17 +697,7 @@ function urlW3c($check, bool $strict = false): bool
*/
function isEuVatNumber(string $pi, bool $validateOnVIES = false): bool
{
if ($pi === null || $pi === '' || strlen($pi) < 2) {
return false;
}

//try to find country code
$countryCode = strtoupper(substr($pi, 0, 2));
if (preg_match('/^[A-Za-z]{2}$/', $countryCode) === 1) {
$pi = substr($pi, 2);
}else{
$countryCode='IT';
}
$countryCode = getCoutryCodeByVatNumber($pi, 'IT');

$result = true;
if (function_exists('is'.$countryCode.'Vat')){
Expand All @@ -729,6 +719,30 @@ function isEuVatNumber(string $pi, bool $validateOnVIES = false): bool
}
}

/**
* Try to extract EU country code in Vat number
* return $fallback if it fails.
*
* @param string $pi
* @param string $fallback
* @return string
*/
function getCoutryCodeByVatNumber(string $pi, string $fallback ='IT'): string
{
if ($pi === null || $pi === '' || strlen($pi) < 2) {
return $fallback;
}

//try to find country code
$countryCode = strtoupper(substr($pi, 0, 2));
if (preg_match('/^[A-Za-z]{2}$/', $countryCode) === 1) {
$pi = substr($pi, 2);
} else {
$countryCode = $fallback;
}
return $countryCode;
}

/**
* Check Italian Vat Number (Partita IVA).
* @author Umberto Salsi <salsi@icosaedro.it>
Expand All @@ -741,20 +755,12 @@ function isEuVatNumber(string $pi, bool $validateOnVIES = false): bool
*/
function isITVat(string $pi): bool
{
if ($pi === null || $pi === '' || strlen($pi) < 2) {
$countryCode = getCoutryCodeByVatNumber($pi, '');
if($countryCode!='IT' && $countryCode!=''){
return false;
}

//try to find country code
$countryCode = strtoupper(substr($pi, 0, 2));
if (preg_match('/^[A-Za-z]{2}$/', $countryCode) === 1) {
if($countryCode!=''){
$pi = substr($pi, 2);
}else{
$countryCode='IT';
}

if($countryCode!='IT'){
return false;
}

if ($pi === null || $pi === '' || strlen($pi) != 11 || preg_match("/^[0-9]+\$/", $pi) != 1) {
Expand Down
24 changes: 24 additions & 0 deletions tests/ValidationDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,28 @@ public function isEuVatNumberProvider()
];
}

/**
* @return array
*/
public function getCoutryCodeByVatNumberProvider()
{
return [
'"", \'IT\'' => ['', 'IT', 'IT'],
'" ", \'\' ' => [' ', '', ''],
'null, \'IT\'' => [null, 'IT', 'TypeError'],
'null, \'\'' => [null, '', 'TypeError'],
'00000000000, \'IT\'' => ['00000000000', 'IT', 'IT'],
'I00000000000, \'IT\'' => ['I00000000000', 'IT', 'IT'],
'I00000000000, \'\'' => ['I00000000000', '', ''],
'ITT0000000000, \'IT\'' => ['ITT0000000000', 'IT', 'IT'],
'ITT0000000000, \'\'' => ['ITT0000000000', '', 'IT'],
'IT00000000000, \'\'' => ['IT00000000000', '', 'IT'],
'IT00000000000, \'DE\'' => ['IT00000000000', 'DE', 'IT'],
'DE00000000001, \'IT\'' => ['DE00000000001', 'IT', 'DE'],
'DE00000000001, \'\'' => ['DE00000000001', '', 'DE'],
];
}

/**
* @return array
*/
Expand All @@ -1647,6 +1669,8 @@ public function isITVatProvider()
'IT00000000000' => ['IT00000000000', true],
'02361141209' => ['02361141209', true],
'IT02361141209' => ['IT02361141209', true],
'ITT2361141209' => ['ITT2361141209', false],
'I02361141209' => ['I02361141209', false],
'00000000001' => ['00000000001', false],
'IT00000000001' => ['IT00000000001', false],
'DE00000000001' => ['DE00000000001', false],
Expand Down
18 changes: 18 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,24 @@ public function isITVatTest($val, $expected)
}
}

/**
* @test
* @param $val
* @param $fallback
* @param $expected
* @dataProvider getCoutryCodeByVatNumberProvider
*/
public function getCoutryCodeByVatNumberTest($val, $fallback, $expected)
{
if ($this->expectedIsAnException($expected)) {
$this->expectException($expected);
getCoutryCodeByVatNumber($val, $fallback);
} else {
$result = getCoutryCodeByVatNumber($val, $fallback);
$this->assertEquals($expected, $result);
}
}

/**
* @test
* @param $val
Expand Down

0 comments on commit 6b76155

Please sign in to comment.