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

use Luhn to calculate ar_SA id numbers. #875

Merged
merged 11 commits into from
Oct 7, 2016
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ Fugiat non in itaque sunt nobis totam. Sed nesciunt est deleniti cumque alias. R
echo $faker->idNumber; // ID number
echo $faker->nationalIdNumber // Citizen ID number
echo $faker->foreignerIdNumber // Foreigner ID number
echo $faker->companyIdNumber // Company ID number
```

### `Faker\Provider\at_AT\Payment`
Expand Down
10 changes: 10 additions & 0 deletions src/Faker/Provider/ar_SA/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider\ar_SA;

use Faker\Provider\ar_SA\Utils;

class Company extends \Faker\Provider\Company
{
protected static $formats = array(
Expand Down Expand Up @@ -60,4 +62,12 @@ public function bs()

return join($result, ' ');
}

/**
* example 7001010101
**/
public static function companyIdNumber()
{
return Utils::generateLuhnNumber(700, 10);
}
}
51 changes: 9 additions & 42 deletions src/Faker/Provider/ar_SA/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Faker\Provider\ar_SA;

use Faker\Calculator\Luhn;
use Faker\Provider\ar_SA\Utils;

class Person extends \Faker\Provider\Person
{
protected static $maleNameFormats = array(
Expand Down Expand Up @@ -90,59 +93,23 @@ public static function prefix()
*/
public static function idNumber()
{
$firstDigit = static::numberBetween(1, 2);

do {
$number = $firstDigit.static::numerify('#########');
} while (!static::checkSum($number));

return $number;
$firstDigit = static::randomElement(array(1, 2));
return Utils::generateLuhnNumber($firstDigit, 10);
}

/**
* @example
* @example 1010101010
*/
public static function nationalIdNumber()
{
do {
$number = '1'.static::numerify('#########');
} while (!static::checkSum($number));

return $number;
return Utils::generateLuhnNumber(1, 10);
}

/**
* @example
* @example 2010101010
*/
public static function foreignerIdNumber()
{
do {
$number = '2'.static::numerify('#########');
} while (!static::checkSum($number));

return $number;
}

/**
* Check sum the number.
* @param $number
*
* @return bool
*/
protected static function checkSum($number)
{
$sum = 0;
$nums = str_split($number);

for ($i = 0; $i < 10; $i++) {
if ($i % 2 == 0) {
$s = $nums[$i] * 2;
$sum += $s % 10 + floor($s / 10);
} else {
$sum += $nums[$i];
}
}

return ($sum % 10 == 0);
return Utils::generateLuhnNumber(2, 10);
}
}
16 changes: 16 additions & 0 deletions src/Faker/Provider/ar_SA/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Faker\Provider\ar_SA;

use Faker\Calculator\Luhn;
use Faker\Provider\Base as BaseProvider;

class Utils
{
public static function generateLuhnNumber($prefix, $length)
Copy link
Owner

Choose a reason for hiding this comment

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

You already have everything in Faker\Calculator\Luhn. No need to reimplement it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it good enough now?

Copy link
Owner

Choose a reason for hiding this comment

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

this would be better placed in Faker\Calculator\Luhn

{
$pattern = $prefix . str_repeat('#', $length - strlen($prefix) - 1);
$partialValue = BaseProvider::numerify($pattern);
return $partialValue . Luhn::computeCheckDigit($partialValue);
}
}
29 changes: 29 additions & 0 deletions test/Faker/Provider/ar_SA/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Faker\Test\Provider\ar_SA;

use Faker\Provider\ar_SA\Utils;
use Faker\Calculator\Luhn;

class UtilsTest extends \PHPUnit_Framework_TestCase
{

public function testGeneretedNumber()
{
$this->assertNotEmpty(Utils::generateLuhnNumber(1, 5));
}

public function testCorrectLuhnNumberLength()
{
$this->assertEquals(10, strlen(Utils::generateLuhnNumber(443, 10)));
$this->assertEquals(15, strlen(Utils::generateLuhnNumber(5, 15)));
$this->assertEquals(25, strlen(Utils::generateLuhnNumber(203, 25)));
}

public function testIfGeneretedNumberIsLuhnien()
{
$this->assertTrue(Luhn::isValid(Utils::generateLuhnNumber(443, 10)));
$this->assertTrue(Luhn::isValid(Utils::generateLuhnNumber(5, 15)));
$this->assertTrue(Luhn::isValid(Utils::generateLuhnNumber(203, 25)));
}
}