Skip to content

Commit 1e6b861

Browse files
committed
# 2.0.0 - 2018-04-18
### BREAKING CHANGES: ##### validation: - isPiva and IsVatNumber was removed in favour to new functions: isEuVatNumber, isVATRegisteredInVies, isITVat. - 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.
1 parent 0d14531 commit 1e6b861

File tree

5 files changed

+153
-36
lines changed

5 files changed

+153
-36
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

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

5+
## 2.0.0 - 2018-04-18
6+
7+
### BREAKING CHANGES:
8+
##### validation:
9+
- isPiva and IsVatNumber was removed in favour to new functions: isEuVatNumber, isVATRegisteredInVies, isITVat.
10+
- 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.
11+
- isITVat() : Check Italian Vat Number (Partita IVA).
12+
- 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.
13+
514
## 1.17.4 - 2018-04-18
615

716
### FIX:
@@ -309,7 +318,7 @@ ADD functions:
309318
- ADD IP helpers.
310319
- ADD getClientIp() by small refactor of Synfony functions with unit test.
311320
- CHANGE mark OBSOLETE getIPVisitor().
312-
- ADD isVATNumber().Validate a European VAT number using the EU commission VIES service with soap.
321+
- ADD isVATRegisteredInVies().Validate a European VAT number using the EU commission VIES service with soap.
313322
- ADD xml2array() and array2xml().
314323
- ADD generateRandomPassword().
315324
- ADD some new helpers.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,9 @@ var_dump(str_random(16));
294294
- isUrl
295295
- isHostname
296296
- urlW3c
297-
- isPiva
298-
- isVATNumber
297+
- 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.
298+
- isITVat() : Check Italian Vat Number (Partita IVA).
299+
- 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.
299300
- isCf
300301
- isAlpha
301302
- isAlphaNumeric

src/validation.php

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -687,20 +687,76 @@ function urlW3c($check, bool $strict = false): bool
687687
}
688688

689689
/**
690-
* Controlla partita IVA Italiana.
690+
* Check if a valid EU vat given.
691+
* @param string $pi required eu vat number with or without country code prefix.
692+
* If you don't pass country code prefix, 'IT' will be assumed.
693+
* @param bool $validateOnVIES default false. if se to true, first check formal EU country algorithm,
694+
* then if it valid and country code isn't 'IT' try to check by API VIES service.
695+
* If VIES return false or soap exception was thrown, return false.
696+
* @return bool
697+
*/
698+
function isEuVatNumber(string $pi, bool $validateOnVIES = false): bool
699+
{
700+
if ($pi === null || $pi === '' || strlen($pi) < 2) {
701+
return false;
702+
}
703+
704+
//try to find country code
705+
$countryCode = strtoupper(substr($pi, 0, 2));
706+
if (preg_match('/^[A-Za-z]{2}$/', $countryCode) === 1) {
707+
$pi = substr($pi, 2);
708+
}else{
709+
$countryCode='IT';
710+
}
711+
712+
$result = true;
713+
if (function_exists('is'.$countryCode.'Vat')){
714+
$funcname = 'is'.$countryCode.'Vat';
715+
$result = $funcname($pi);
716+
}
717+
if(!$result){
718+
return false;
719+
}
720+
if($countryCode=='IT' || !$validateOnVIES){
721+
return $result;
722+
}
723+
724+
//check vies
725+
try {
726+
return isVATRegisteredInVies($pi);
727+
} catch (SoapFault $e) {
728+
return false;
729+
}
730+
}
731+
732+
/**
733+
* Check Italian Vat Number (Partita IVA).
691734
* @author Umberto Salsi <salsi@icosaedro.it>
692735
* @author Lorenzo Padovani modified.
693736
* @version 2012-05-12
694-
* @param string $pi Partita IVA Italiana è costituita da 11 cifre. Non sono ammessi
695-
* caratteri di spazio, per cui i campi di input dell'utente dovrebbero
696-
* essere trimmati preventivamente. La stringa vuota e' ammessa, cioe'
697-
* il dato viene considerato opzionale.
698-
* @param bool $validateOnVIES default false. if se to true, first check algorithm then if it valid,
699-
* try to check VIES service. If VIES return false or soap exception was thrown, return false.
737+
* @param string $pi Partita IVA Italiana è costituita da 11 cifre o 13 caratteri (prefisso 2 lettere IT).
738+
* Non sono ammessi caratteri di spazio, per cui i campi di input dell'utente dovrebbero
739+
* essere trimmati preventivamente.
700740
* @return bool
701741
*/
702-
function isPiva(string $pi, bool $validateOnVIES = false): bool
742+
function isITVat(string $pi): bool
703743
{
744+
if ($pi === null || $pi === '' || strlen($pi) < 2) {
745+
return false;
746+
}
747+
748+
//try to find country code
749+
$countryCode = strtoupper(substr($pi, 0, 2));
750+
if (preg_match('/^[A-Za-z]{2}$/', $countryCode) === 1) {
751+
$pi = substr($pi, 2);
752+
}else{
753+
$countryCode='IT';
754+
}
755+
756+
if($countryCode!='IT'){
757+
return false;
758+
}
759+
704760
if ($pi === null || $pi === '' || strlen($pi) != 11 || preg_match("/^[0-9]+\$/", $pi) != 1) {
705761
return false;
706762
}
@@ -718,26 +774,20 @@ function isPiva(string $pi, bool $validateOnVIES = false): bool
718774
if ((10 - $s % 10) % 10 != ord($pi[10]) - ord('0')) {
719775
return false;
720776
}
721-
if (!$validateOnVIES) {
722-
return true;
723-
}
724-
//check vies
725-
try {
726-
return isVATNumber($pi);
727-
} catch (SoapFault $e) {
728-
return false;
729-
}
777+
778+
return true;
730779
}
731780

732781
/**
733782
* Validate a European VAT number using the EU commission VIES service.
783+
* To verify if VAT number is authorized to carry out intra-Community operations must use the service
734784
* If not $vatNumber starts with country code, a default $countryCodeDefault applied.
735785
* @param string $vatNumber
736786
* @param string $countryCodeDefault default 'IT'
737787
* @return bool
738788
* @throws SoapFault
739789
*/
740-
function isVATNumber(string $vatNumber, string $countryCodeDefault = 'IT'): bool
790+
function isVATRegisteredInVies(string $vatNumber, string $countryCodeDefault = 'IT'): bool
741791
{
742792
if (!isAlphaNumericWhiteSpaces($vatNumber) || strlen(trim($vatNumber)) < 3) {
743793
return false;
@@ -1152,7 +1202,7 @@ function hasFileExtension($filePath, array $allowed_extensions): bool
11521202
*
11531203
* Examples:
11541204
*
1155-
* 555-555-5555: valid
1205+
* 555-555-5555: valid
11561206
* 5555425555: valid
11571207
* 555 555 5555: valid
11581208
* 1(519) 555-4444: valid

tests/ValidationDataProvider.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,15 +1604,52 @@ public function isHostnameProvider()
16041604
/**
16051605
* @return array
16061606
*/
1607-
public function isPivaProvider()
1607+
public function isEuVatNumberProvider()
1608+
{
1609+
return [
1610+
'"", false' => ['', false, false],
1611+
'"", true' => ['', true, false],
1612+
'" ", false ' => [' ', false, false],
1613+
'" ", true' => [' ', true, false],
1614+
'null, false' => [null, false, 'TypeError'],
1615+
'null, true' => [null, true, 'TypeError'],
1616+
'00000000000, false' => ['00000000000', false, true],
1617+
'00000000000, true' => ['00000000000', true, true],
1618+
'IT00000000000, false' => ['IT00000000000', false, true],
1619+
'IT00000000000, true' => ['IT00000000000', true, true],
1620+
'02361141209, false' => ['02361141209', false, true],
1621+
'02361141209, true' => ['02361141209', true, true],
1622+
'IT02361141209, false' => ['IT02361141209', false, true],
1623+
'IT02361141209, true' => ['IT02361141209', true, true],
1624+
'00000000001, false' => ['00000000001', false, false],
1625+
'00000000001, true' => ['00000000001', true, false],
1626+
'IT00000000001, false' => ['IT00000000001', false, false],
1627+
'IT00000000001, true' => ['IT00000000001', true, false],
1628+
'DE00000000001, false' => ['DE00000000001', false, true],//finchè non c'è un validatore per ogni stato se passo un vat estero e non valido via vies torna true
1629+
'DE00000000001, true' => ['DE00000000001', true, false],
1630+
'0, false' => ['0', false, false],
1631+
'0, true' => ['0', true, false],
1632+
'123456789012345678990, false' => ['123456789012345678990', false, false],
1633+
'123456789012345678990, true' => ['123456789012345678990', true, false],
1634+
];
1635+
}
1636+
1637+
/**
1638+
* @return array
1639+
*/
1640+
public function isITVatProvider()
16081641
{
16091642
return [
16101643
'' => ['', false],
16111644
' ' => [' ', false],
16121645
'null' => [null, 'TypeError'],
16131646
'00000000000' => ['00000000000', true],
1647+
'IT00000000000' => ['IT00000000000', true],
16141648
'02361141209' => ['02361141209', true],
1649+
'IT02361141209' => ['IT02361141209', true],
16151650
'00000000001' => ['00000000001', false],
1651+
'IT00000000001' => ['IT00000000001', false],
1652+
'DE00000000001' => ['DE00000000001', false],
16161653
'0' => ['0', false],
16171654
'123456789012345678990' => ['123456789012345678990', false],
16181655
];
@@ -1621,7 +1658,7 @@ public function isPivaProvider()
16211658
/**
16221659
* @return array
16231660
*/
1624-
public function isVATNumberProvider()
1661+
public function isVATRegisteredInViesProvider()
16251662
{
16261663
return [
16271664
'\'\', \'\'' => ['', '', false],

tests/ValidationTest.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -553,31 +553,50 @@ public function isMailTest($val, $checkMX, $expected)
553553
* @test
554554
* @param $val
555555
* @param $expected
556-
* @dataProvider isPivaProvider
556+
* @dataProvider isCfProvider
557557
*/
558-
public function isPivaTest($val, $expected)
558+
public function isCfTest($val, $expected)
559559
{
560560
if ($this->expectedIsAnException($expected)) {
561561
$this->expectException($expected);
562-
isPiva($val);
562+
isCf($val);
563563
} else {
564-
$this->assertEquals($expected, isPiva($val));
564+
$this->assertEquals($expected, isCf($val));
565565
}
566566
}
567567

568568
/**
569569
* @test
570570
* @param $val
571+
* @param $validateOnVies
571572
* @param $expected
572-
* @dataProvider isCfProvider
573+
* @dataProvider isEuVatNumberProvider
573574
*/
574-
public function isCfTest($val, $expected)
575+
public function isEuVatNumberTest($val, $validateOnVies, $expected)
575576
{
576577
if ($this->expectedIsAnException($expected)) {
577578
$this->expectException($expected);
578-
isCf($val);
579+
isEuVatNumber($val, $validateOnVies);
579580
} else {
580-
$this->assertEquals($expected, isCf($val));
581+
$this->assertEquals($expected, isEuVatNumber($val, $validateOnVies));
582+
}
583+
}
584+
585+
586+
/**
587+
* @test
588+
* @param $val
589+
* @param $expected
590+
* @dataProvider isITVatProvider
591+
*/
592+
public function isITVatTest($val, $expected)
593+
{
594+
if ($this->expectedIsAnException($expected)) {
595+
$this->expectException($expected);
596+
isITVat($val);
597+
} else {
598+
$result = isITVat($val);
599+
$this->assertEquals($expected, $result);
581600
}
582601
}
583602

@@ -586,17 +605,18 @@ public function isCfTest($val, $expected)
586605
* @param $val
587606
* @param $countryCode
588607
* @param $expected
589-
* @dataProvider isVATNumberProvider
608+
* @dataProvider isVATRegisteredInViesProvider
609+
* @throws
590610
*/
591-
public function isVATNumberTest($val, $countryCode, $expected)
611+
public function isVATRegisteredInViesTest($val, $countryCode, $expected)
592612
{
593613
if ($this->expectedIsAnException($expected)) {
594614
$this->expectException($expected);
595-
isVATNumber($val, $countryCode);
615+
isVATRegisteredInVies($val, $countryCode);
596616
} else {
597617
try
598618
{
599-
$result = isVATNumber($val, $countryCode);
619+
$result = isVATRegisteredInVies($val, $countryCode);
600620
$this->assertEquals($expected, $result);
601621
} catch (SoapFault $e) {
602622
$this->expectException('SoapFault');

0 commit comments

Comments
 (0)