Skip to content

Commit

Permalink
Merge branch 'krzaczek-issue-89'
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonBe committed Jan 2, 2020
2 parents c843203 + c9118aa commit 04b1cdf
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
build/
composer.lock
.scannerwork/
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ When you have implemented this service package in your own project, be sure that
- Recommended PHP version: 7.2
- Extension: soap
- Extension: pcntl
- Extension: ctype

Please read the [release notes](https://github.com/DragonBe/vies/releases) for details.

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"require": {
"php": ">=7.1",
"ext-soap": "*"
"ext-soap": "*",
"ext-ctype": "*"
},
"require-dev": {
"ext-pcntl": "*",
Expand Down
104 changes: 89 additions & 15 deletions src/Vies/Validator/ValidatorNL.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,45 @@
* @package DragonBe\Vies\Validator
*
* VAT format: [C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12]
*
* Range:
* C1 ... C9 Numeric from 0 to 9
* C10 Alphabetic “B”
* C11 ... C12 Numeric from 0 to 9
*
* Rules:
* C9
* A1 = C1*9 + C2*8 + C3*7 + C4*6 + C5*5 + C6*4 + C7*3 + C8*2
* A2 = A1 modulo 11
* If A2 = 10 then number is invalid
* else C9 = A2
*
* [C11 C12]
* >00
*/
class ValidatorNL extends ValidatorAbstract
{
/**
* Check Character: 10-A, 11-B .... 35-Z, 36-+, 37-*
*
* @var array
*/
protected $checkCharacter = [
'A' => 10,
'B' => 11,
'C' => 12,
'D' => 13,
'E' => 14,
'F' => 15,
'G' => 16,
'H' => 17,
'I' => 18,
'J' => 19,
'K' => 20,
'L' => 21,
'M' => 22,
'N' => 23,
'O' => 24,
'P' => 25,
'Q' => 26,
'R' => 27,
'S' => 28,
'T' => 29,
'U' => 30,
'V' => 31,
'W' => 32,
'X' => 33,
'Y' => 34,
'Z' => 35,
'+' => 36,
'*' => 37,
];

/**
* {@inheritdoc}
*/
Expand All @@ -47,6 +68,30 @@ public function validate(string $vatNumber): bool
return false;
}

return $this->validateCommercial($vatNumber) || $this->validateSoleProprietor($vatNumber);
}

/**
* Range:
* C1 ... C9 Numeric from 0 to 9
* C10 Alphabetic “B”
* C11 ... C12 Numeric from 0 to 9
*
* Rules:
* C9
* A1 = C1*9 + C2*8 + C3*7 + C4*6 + C5*5 + C6*4 + C7*3 + C8*2
* A2 = A1 modulo 11
* If A2 = 10 then number is invalid
* else C9 = A2
*
* [C11 C12]
* >00
*
* @param string $vatNumber
* @return bool
*/
protected function validateCommercial(string $vatNumber): bool
{
if ((int)substr($vatNumber, -2) == 0) {
return false;
}
Expand All @@ -58,4 +103,33 @@ public function validate(string $vatNumber): bool

return $checkval == $checksum;
}

/**
* Range:
* C1 ... C9 0-9 A-Z + *
* C10 Alphabetic “B”
* C11 ... C12 Numeric from 0 to 9
*
* [C11 C12]
* 02 - 98
*
* @param string $vatNumber
* @return bool
*/
protected function validateSoleProprietor(string $vatNumber): bool
{
if (! preg_match("#^[A-Z0-9+*]{9}B[0-9]{2}$#u", $vatNumber)) {
return false;
}

$sumBase = (int)array_reduce(str_split($vatNumber), function ($acc, $e) {
if (ctype_digit($e)) {
return $acc.$e;
}

return $acc.$this->checkCharacter[$e];
}, '2321');

return ($sumBase % 97) === 1;
}
}
1 change: 1 addition & 0 deletions tests/Vies/Validator/ValidatorNLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function vatNumberProvider()
['12345678901', false],
['123456789A12', false],
['123456789B00', false],
['002342672B42', true],
];
}
}
4 changes: 3 additions & 1 deletion tests/Vies/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function vatNumberProvider()
'LU' => ['10000356', ['10000355', '1234567']],
'LV' => ['40003009497', ['40013009497', '40003009496', '1234567890', '00212345678']],
'MT' => ['15121333', ['15121332', '1234567', '05121333']],
'NL' => ['010000446B01', ['010000436B01', '12345678901', '123456789A12', '123456789B00']],
'NL' => [
['010000446B01', '000099998B57'],
['010000436B01', '12345678901', '123456789A12', '123456789B00', '0$0099998B57']],
'PL' => ['5260001246', ['12342678090', '1212121212']],
'PT' => ['502757191', ['502757192', '12345678']],
'RO' => [['11198699', '14186770'], ['11198698', '1', '12345678902']],
Expand Down

0 comments on commit 04b1cdf

Please sign in to comment.