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
14 changes: 14 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\Calculator\Luhn;

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

return join($result, ' ');
}

/**
* example 7001010101
**/
public static function companyIdNumber()
{
do {
$number = static::numerify('700#######');
Copy link
Owner

Choose a reason for hiding this comment

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

nah, you can do better than try until it's valid!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually this is exactly what is done on the Person provider for the same locale, but I will look for something.

Copy link
Contributor

Choose a reason for hiding this comment

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

Luhn::computeCheckDigit() will give you the correct last digit.

Copy link
Owner

Choose a reason for hiding this comment

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

Then the Person provider is wrong, too, and you can update it while you're touching Luhns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I did on commit 757750f.

Copy link
Owner

Choose a reason for hiding this comment

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

It doesn't appear in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want to sound stupid, but I see the changes on the last commit on this PR.

} while (!Luhn::isValid($number));

return $number;
}
}
55 changes: 15 additions & 40 deletions src/Faker/Provider/ar_SA/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider\ar_SA;

use Faker\Calculator\Luhn;

class Person extends \Faker\Provider\Person
{
protected static $maleNameFormats = array(
Expand Down Expand Up @@ -85,64 +87,37 @@ public static function prefix()
return static::randomElement(static::$prefix);
}

/**
* @example 1010101010
*/
public static function idNumber()
protected static function luhnCompatibleFromPattern($pattern)
{
$firstDigit = static::numberBetween(1, 2);

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

return $number;
}

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

return $number;
$firstDigit = static::randomElement(array(1, 2));
return static::luhnCompatibleFromPattern($firstDigit . '#########');
}

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

return $number;
return static::luhnCompatibleFromPattern('1#########');
}

/**
* Check sum the number.
* @param $number
*
* @return bool
* @example 2010101010
*/
protected static function checkSum($number)
public static function foreignerIdNumber()
{
$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 static::luhnCompatibleFromPattern('2#########');
}
}