Skip to content

Commit

Permalink
Added norwegian validation, updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ToX82 committed Jan 15, 2022
1 parent 5235ea6 commit 232a222
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
## A formal VAT number validator

It's not a full validation tool, it just checks that the VAT number is formally valid: eg. 11 chars, digits only, etc.
It's built using these rules: https://www.gov.uk/guidance/vat-eu-country-codes-vat-numbers-and-vat-in-other-languages
It's built using these rules:
* https://www.gov.uk/guidance/vat-eu-country-codes-vat-numbers-and-vat-in-other-languages
* https://en.wikipedia.org/wiki/VAT_identification_number

It requires PHP >= 7.0.

Expand All @@ -17,7 +19,10 @@ Usage
Just load the Validate class and execute a check, passing the country code and VAT code as parameters:

```php
$validator = new Tox82\Validate\ValidateVat;
use Tox82\VatNumber\Validate;

...

echo Validate::check('FR', '12345678901'); // true
echo Validate::check('HU', '12345678'); // true
echo Validate::check('PT', '123456789'); // true
Expand Down
25 changes: 25 additions & 0 deletions src/VatNumber/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @author Emanuele "ToX" Toscano <[email protected]>
* @license GNU General Public License; <https://www.gnu.org/licenses/gpl-3.0.en.html>
* @link https://www.gov.uk/guidance/vat-eu-country-codes-vat-numbers-and-vat-in-other-languages
* @link https://en.wikipedia.org/wiki/VAT_identification_number
*/
class Validate
{
Expand Down Expand Up @@ -96,6 +97,9 @@ public static function check(string $country, string $code): bool
case "NL":
// 12 characters. The tenth character is always B
return self::checkLength($code, 12, 12) && self::checkNetherlands($code);
case "NO":
// 9 characters. 12 when it contains the letters MVA
return self::checkLength($code, 9, 12) && self::checkNorway($code);
case "PL":
// 10 characters.
return self::checkLength($code, 10, 10) && self::numbersOnly($code);
Expand Down Expand Up @@ -293,6 +297,27 @@ public static function checkNetherlands(string $code): bool
return true;
}

/**
* Additional validations for Norway
*
* @param string $code Regional VAT code
*
* @return bool
*/
public static function checkNorway(string $code): bool
{
$return = false;

// the string must be 9 digits, or 9 digits followed by MVA
if (preg_match('/^[0-9]{9}$/i', $code)) {
$return = true;
} elseif (preg_match('/^[0-9]{9}[MVA]{3}$/i', $code)) {
$return = true;
}

return $return;
}

/**
* Additional validations for Spain
*
Expand Down
3 changes: 3 additions & 0 deletions tests/Validate/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function testCheckValids()
$this->assertTrue(Validate::check('LU', '12345678'));
$this->assertTrue(Validate::check('MT', '12345678'));
$this->assertTrue(Validate::check('NL', '123456789B01'));
$this->assertTrue(Validate::check('NO', '123456789'));
$this->assertTrue(Validate::check('NO', '123456789MVA'));
$this->assertTrue(Validate::check('PL', '1234567890'));
$this->assertTrue(Validate::check('PT', '123456789'));
$this->assertTrue(Validate::check('RO', '12'));
Expand Down Expand Up @@ -104,5 +106,6 @@ public function testCheckWithInvalidCharacters()
{
$this->assertFalse(Validate::check('AT', 'A12345678'));
$this->assertFalse(Validate::check('IT', 'IT00154189997'));
$this->assertFalse(Validate::check('NO', '123456789MV'));
}
}

0 comments on commit 232a222

Please sign in to comment.