diff --git a/src/Faker/Provider/es_ES/Person.php b/src/Faker/Provider/es_ES/Person.php index c433376da5..3be8ce37e8 100644 --- a/src/Faker/Provider/es_ES/Person.php +++ b/src/Faker/Provider/es_ES/Person.php @@ -4,6 +4,8 @@ class Person extends \Faker\Provider\Person { + private static $crcMap=array('T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E', 'T'); + protected static $maleNameFormats = array( '{{firstNameMale}} {{lastName}}', '{{firstNameMale}} {{lastName}}', @@ -68,4 +70,12 @@ public static function suffix() { return static::randomElement(static::$suffix); } + + public static function dni() + { + $number=static::numerify('########'); + + $letter=self::$crcMap[$number%23]; + return $number.$letter; + } } diff --git a/test/Faker/Provider/es_ES/PersonTest.php b/test/Faker/Provider/es_ES/PersonTest.php new file mode 100644 index 0000000000..55360bac77 --- /dev/null +++ b/test/Faker/Provider/es_ES/PersonTest.php @@ -0,0 +1,38 @@ +seed(1); + $faker->addProvider(new Person($faker)); + $this->faker = $faker; + } + + public function testDNI() + { + $dni = $this->faker->dni; + $this->assertTrue($this->isValidDNI($dni)); + } + + // validation taken from http://kiwwito.com/php-function-for-spanish-dni-nie-validation/ + public function isValidDNI($string) + { + if (strlen($string) != 9 || + preg_match('/^[XYZ]?([0-9]{7,8})([A-Z])$/i', $string, $matches) !== 1) { + return false; + } + + $map = 'TRWAGMYFPDXBNJZSQVHLCKE'; + + list(, $number, $letter) = $matches; + + return strtoupper($letter) === $map[((int) $number) % 23]; + } +}