Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

+1 VAT Provider w/ 40+ countries rules, fix #591 #594

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @property string $creditCardDetails
* @property string $bankAccountNumber
* @property string $swiftBicNumber
* @property string $vat
* @method string vat($country, $spacedNationalPrefix = true)
*
* @property string $word
* @property string|array $words
Expand Down
81 changes: 81 additions & 0 deletions src/Faker/Provider/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,87 @@

class Payment extends Base
{
/**
* Regular expression patterns per country code.
*
* @var array
*
* @link http://ec.europa.eu/taxation_customs/vies/faq.html?locale=lt#item_11
* @link http://www.iecomputersystems.com/ordering/eu_vat_numbers.htm
* @link http://en.wikipedia.org/wiki/VAT_identification_number
* @link https://github.com/ronanguilloux/IsoCodes/blob/master/src/IsoCodes/Vat.php
*/
public static $patterns = array(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable name is too generic IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true!

'AT' => 'U\d{8}',
'BE' => '0\d{9}',
'BG' => '\d{9,10}',
'CY' => '\d{8}[A-Z]',
'CZ' => '\d{8,10}',
'DE' => '\d{9}',
'DK' => '(\d{2} ?){3}\d{2}',
'EE' => '\d{9}',
'EL' => '\d{9}',
// ES: The first and last characters may be alpha or numeric; but they may not both be numeric:
'ES' => '[A-Z]\d{7}[A-Z]|\d{8}[A-Z]|[A-Z]\d{8}',
'FI' => '\d{8}',
'FR' => '([A-Z]{2}|\d{2})\d{9}',
'GB' => '\d{9}|\d{12}|(GD|HA)\d{3}',
'HU' => '\d{8}',
// IE: Seven digits and one last letter or six digits and two letters (second & last)
'IE' => '\d{7}[A-Z]|\d[A-Z]\d{5}[A-Z]',
'IT' => '\d{11}',
'LT' => '(\d{9}|\d{12})',
'LU' => '\d{8}',
'LV' => '\d{11}',
'MT' => '\d{8}',
// NL: The 10th position following the prefix is always "B".
'NL' => '\d{9}B\d{2}',
'PL' => '\d{10}',
'PT' => '\d{9}',
'RO' => '\d{2,10}',
'SE' => '\d{12}',
'SI' => '\d{8}',
'SK' => '\d{10}',
'AL' => '[KJ]\d{8}L',
'AU' => '\d{9}',
'BY' => '\d{9}',
'HR' => '\d{11}',
'NO' => '\d{9}MVA',
'PH' => '\d{12}',
'RU' => '(\d{10}|\d{12})',
'TR' => '\d{10}',
'UA' => '\d{12}',
'AR' => '\d{11}',
'CL' => '\d{8}-\d',
'CO' => '\d{10}',
'EC' => '\d{13}',
'GT' => '\d{7}-\d',
'MX' => '\d{3} \d{6} \d{3}',
'VE' => '[JGVE]-\d{8}-?\d',
);

/**
* Generate a national VAT number.
*
* The Value Added Tax, or VAT, is a general, broadly based consumption tax
* assessed on the value added to goods and services.
*
* @link http://ec.europa.eu/taxation_customs/vies/faq.html for European VAT numbers rules
* @example (belgian) BE0629078028, (polish, spaced) PL 5206136094
* @param bool $spacedNationalPrefix
* @param string $country country code (ex: 'BE')
* @return string a fake valid VAT number
*/
public static function vat($spacedNationalPrefix = true, $country = null)
{
$prefix = ($spacedNationalPrefix) ? " " : "";
if (!array_key_exists($country, self::$patterns)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to choose a random country if none is provided

throw new \InvalidArgumentException(sprintf("There is no VAT generator for %s so far.", $country));
}

return self::regexify(sprintf("^%s%s%s$", $country, $prefix, self::$patterns[$country]));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regexify is the slowest of all generators. Can't you achieve the same result with ### and ***?

}

public static $expirationDateFormat = "m/y";

protected static $cardVendors = array(
Expand Down
12 changes: 7 additions & 5 deletions src/Faker/Provider/at_AT/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ class Payment extends \Faker\Provider\Payment
/**
* Value Added Tax (VAT)
*
* (this method is kept for backwards compatibility purpose only, until 1.6.0)
* @deprecated Deprecated since version 1.6.0, please use \Faker\Provider\Payment::vat() instead
*
* @example 'ATU12345678', ('spaced') 'AT U12345678'
*
* @see http://ec.europa.eu/taxation_customs/vies/faq.html?locale=en#item_11
* @see http://www.iecomputersystems.com/ordering/eu_vat_numbers.htm
* @see http://en.wikipedia.org/wiki/VAT_identification_number
*
* @param bool $spacedNationalPrefix
* @param bool $spacedNationalPrefix
* @param string $country country code (ex: 'BE')
*
* @return string VAT Number
*/
public static function vat($spacedNationalPrefix = true)
public static function vat($spacedNationalPrefix = true, $country = 'AT')
{
$prefix = ($spacedNationalPrefix) ? "AT U" : "ATU";

return sprintf("%s%d", $prefix, self::randomNumber(8, true));
return parent::vat($spacedNationalPrefix, $country);
}
}
31 changes: 0 additions & 31 deletions src/Faker/Provider/be_BE/Payment.php

This file was deleted.

17 changes: 7 additions & 10 deletions src/Faker/Provider/bg_BG/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ public static function bankAccountNumber($prefix = '', $countryCode = 'BG', $len
/**
* Value Added Tax (VAT)
*
* (this method is kept for backwards compatibility purpose only, until 1.6.0)
* @deprecated Deprecated since 1.6.0, please use \Faker\Provider\Payment::vat() instead
*
* @example 'BG1234567890', ('spaced') 'BG 1234567890'
*
* @see http://ec.europa.eu/taxation_customs/vies/faq.html?locale=en#item_11
* @see http://en.wikipedia.org/wiki/VAT_identification_number
*
* @param bool $spacedNationalPrefix
* @param bool $spacedNationalPrefix
* @param string $country country code (ex: 'BE')
*
* @return string VAT Number
*/
public static function vat($spacedNationalPrefix = true)
public static function vat($spacedNationalPrefix = true, $country = 'BG')
{
$prefix = ($spacedNationalPrefix) ? "BG " : "BG";

return sprintf(
"%s%d%d",
$prefix,
self::randomNumber(5, true), // workaround for mt_getrandmax() limitation
self::randomNumber(self::randomElement(array(4, 5)), true)
);
return parent::vat($spacedNationalPrefix, $country);
}
}
21 changes: 21 additions & 0 deletions test/Faker/Provider/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Faker\Provider\Base as BaseProvider;
use Faker\Provider\DateTime as DateTimeProvider;
use Faker\Provider\Payment as PaymentProvider;
use Faker\Provider\Payment;
use Faker\Provider\Person as PersonProvider;

class PaymentTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -65,4 +66,24 @@ public function testRandomCard()
$this->assertEquals(count($cardDetails), 4);
$this->assertEquals(array('type', 'number', 'name', 'expirationDate'), array_keys($cardDetails));
}

public function countriesProvider()
{
$data = array();

foreach(Payment::$patterns as $country => $regexp) {
$data[] = array($country, sprintf("/^%s%s$/",$country, Payment::$patterns[$country]));
}

return $data;
}

/**
* @dataProvider countriesProvider
*/
public function testVat($country, $regexp)
{
$vat = Payment::vat(false, $country);
$this->assertRegExp($regexp, $vat);
}
}
30 changes: 0 additions & 30 deletions test/Faker/Provider/be_BE/PaymentTest.php

This file was deleted.