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

add DNI generator to spanis person #763

Merged
merged 3 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/Faker/Provider/es_ES/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}}',
Expand Down Expand Up @@ -68,4 +70,12 @@ public static function suffix()
{
return static::randomElement(static::$suffix);
}

public static function dni()
Copy link
Owner

Choose a reason for hiding this comment

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

please add a link to the dni specification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

Please add it as a @link annotation in the method phpDoc

{
$number=static::numerify('########');

$letter=self::$crcMap[$number%23];
return $number.$letter;
}
}
38 changes: 38 additions & 0 deletions test/Faker/Provider/es_ES/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Faker\Test\Provider\es_ES;

use Faker\Generator;
use Faker\Provider\es_ES\Person;

class PersonTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$faker = new Generator();
$faker->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];
}
}