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

Commit

Permalink
changed id numbers generation algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarQuaxx committed Apr 6, 2016
1 parent f95ec9e commit 757750f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
7 changes: 2 additions & 5 deletions src/Faker/Provider/ar_SA/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Faker\Provider\ar_SA;

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

class Company extends \Faker\Provider\Company
{
Expand Down Expand Up @@ -68,10 +69,6 @@ public function bs()
**/
public static function companyIdNumber()
{
do {
$number = static::numerify('700#######');
} while (!Luhn::isValid($number));

return $number;
return Utils::generateLuhnNumber(700, 10);
}
}
16 changes: 4 additions & 12 deletions src/Faker/Provider/ar_SA/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Faker\Provider\ar_SA;

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

class Person extends \Faker\Provider\Person
{
Expand Down Expand Up @@ -87,37 +88,28 @@ public static function prefix()
return static::randomElement(static::$prefix);
}

protected static function luhnCompatibleFromPattern($pattern)
{
do {
$number = static::numerify($pattern);
} while (!Luhn::isValid($number));

return $number;
}

/**
* @example 1010101010
*/
public static function idNumber()
{
$firstDigit = static::randomElement(array(1, 2));
return static::luhnCompatibleFromPattern($firstDigit . '#########');
return Utils::generateLuhnNumber($firstDigit, 10);
}

/**
* @example 1010101010
*/
public static function nationalIdNumber()
{
return static::luhnCompatibleFromPattern('1#########');
return Utils::generateLuhnNumber(1, 10);
}

/**
* @example 2010101010
*/
public static function foreignerIdNumber()
{
return static::luhnCompatibleFromPattern('2#########');
return Utils::generateLuhnNumber(2, 10);
}
}
50 changes: 50 additions & 0 deletions src/Faker/Provider/ar_SA/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Faker\Provider\ar_SA;

class Utils
{
// ripped unashamedly from https://github.com/grahamking/darkcoding-credit-card/blob/master/gencc.php
public static function generateLuhnNumber($prefix, $length)
{

$number = $prefix;

# generate digits

while (strlen($number) < ($length - 1)) {
$number .= mt_rand(0, 9);
}

# Calculate sum

$sum = 0;
$pos = 0;

$reversedNumber = strrev($number);

while ($pos < $length - 1) {

$odd = $reversedNumber[$pos] * 2;
if ($odd > 9) {
$odd -= 9;
}

$sum += $odd;

if ($pos != ($length - 2)) {

$sum += $reversedNumber[$pos +1];
}
$pos += 2;
}

# Calculate check digit

$checkdigit = ((floor($sum/10) + 1) * 10 - $sum) % 10;
$number .= $checkdigit;

return $number;

}
}
23 changes: 23 additions & 0 deletions test/Faker/Provider/ar_SA/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Faker\Test\Provider\ar_SA;

use Faker\Generator;
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 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)));
}
}

0 comments on commit 757750f

Please sign in to comment.