1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
<?php
namespace Faker\Test\Provider\es_ES;
use Faker\Provider\es_ES\Payment;
use Faker\Test\TestCase;
/**
* @group legacy
*/
final class PaymentTest extends TestCase
{
public function testVAT()
{
$vat = $this->faker->vat();
self::assertTrue($this->isValidCIF($vat));
}
/**
* Validation taken from https://github.com/amnesty/drupal-nif-nie-cif-validator/
*
* @see https://github.com/amnesty/drupal-nif-nie-cif-validator/blob/master/includes/nif-nie-cif.php
*/
public function isValidCIF($docNumber)
{
$fixedDocNumber = strtoupper($docNumber);
return $this->isValidCIFFormat($fixedDocNumber);
}
public function isValidCIFFormat($docNumber)
{
return $this->respectsDocPattern($docNumber, '/^[PQSNWR][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z0-9]/')
|| $this->respectsDocPattern($docNumber, '/^[ABCDEFGHJUV][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/');
}
public function respectsDocPattern($givenString, $pattern)
{
$isValid = false;
$fixedString = strtoupper($givenString);
if (is_int($fixedString[0])) {
$fixedString = substr('000000000' . $givenString, -9);
}
if (preg_match($pattern, $fixedString)) {
$isValid = true;
}
return $isValid;
}
protected function getProviders(): iterable
{
yield new Payment($this->faker);
}
}
|