From d61f5577851fb4e2fc007c70a4f597e44534c628 Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 6 Apr 2016 14:59:05 +0300 Subject: [PATCH 01/11] use Luhn to calculate ar_SA id numbers. --- src/Faker/Provider/ar_SA/Person.php | 52 ++++++++--------------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index ed1d0d2ee5..da09b130b7 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -2,6 +2,8 @@ namespace Faker\Provider\ar_SA; +use Faker\Calculator\Luhn; + class Person extends \Faker\Provider\Person { protected static $maleNameFormats = array( @@ -85,18 +87,20 @@ public static function prefix() return static::randomElement(static::$prefix); } + protected static function luhnCompatibleFromPattern($pattern) + { + while (!Luhn::isValid($number = static::numerify($pattern))); + + return $number; + } + /** * @example 1010101010 */ public static function idNumber() { - $firstDigit = static::numberBetween(1, 2); - - do { - $number = $firstDigit.static::numerify('#########'); - } while (!static::checkSum($number)); - - return $number; + $firstDigit = static::randomElement([1, 2]); + return static::luhnCompatibleFromPattern($firstDigit . '#########'); } /** @@ -104,11 +108,7 @@ public static function idNumber() */ public static function nationalIdNumber() { - do { - $number = '1'.static::numerify('#########'); - } while (!static::checkSum($number)); - - return $number; + return static::luhnCompatibleFromPattern('1#########'); } /** @@ -116,33 +116,7 @@ public static function nationalIdNumber() */ public static function foreignerIdNumber() { - do { - $number = '2'.static::numerify('#########'); - } while (!static::checkSum($number)); - - return $number; + return static::luhnCompatibleFromPattern('2#########'); } - /** - * 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); - } } From 186c75ad0fb671d9ff891363bb7f4428f6d2b0b2 Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 6 Apr 2016 15:10:25 +0300 Subject: [PATCH 02/11] added company id number for ar_SA provider. --- readme.md | 1 + src/Faker/Provider/ar_SA/Company.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/readme.md b/readme.md index cd14e2d16a..8996dac0a8 100644 --- a/readme.md +++ b/readme.md @@ -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` diff --git a/src/Faker/Provider/ar_SA/Company.php b/src/Faker/Provider/ar_SA/Company.php index 5c5d783fb9..d520991207 100644 --- a/src/Faker/Provider/ar_SA/Company.php +++ b/src/Faker/Provider/ar_SA/Company.php @@ -2,6 +2,8 @@ namespace Faker\Provider\ar_SA; +use Faker\Calculator\Luhn; + class Company extends \Faker\Provider\Company { protected static $formats = array( @@ -60,4 +62,14 @@ public function bs() return join($result, ' '); } + + /** + * example 7001010101 + **/ + public static function companyIdNumber() + { + while (!Luhn::isValid($number = static::numerify('700#######'))); + + return $number; + } } From 502835105d5871043e1dc5ce4de729342a1c3e2f Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 6 Apr 2016 15:17:50 +0300 Subject: [PATCH 03/11] fixed code style errors. --- src/Faker/Provider/ar_SA/Person.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index da09b130b7..04d834f181 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -89,7 +89,9 @@ public static function prefix() protected static function luhnCompatibleFromPattern($pattern) { - while (!Luhn::isValid($number = static::numerify($pattern))); + do { + $number = static::numerify($pattern); + } while (!Luhn::isValid($number)); return $number; } @@ -104,7 +106,7 @@ public static function idNumber() } /** - * @example + * @example 1010101010 */ public static function nationalIdNumber() { @@ -112,11 +114,10 @@ public static function nationalIdNumber() } /** - * @example + * @example 2010101010 */ public static function foreignerIdNumber() { return static::luhnCompatibleFromPattern('2#########'); } - } From d3ec0c940cc6faeff9136deacf403cf3233a9e5f Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 6 Apr 2016 15:23:02 +0300 Subject: [PATCH 04/11] fixed code style errors. --- src/Faker/Provider/ar_SA/Company.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Faker/Provider/ar_SA/Company.php b/src/Faker/Provider/ar_SA/Company.php index d520991207..234286d597 100644 --- a/src/Faker/Provider/ar_SA/Company.php +++ b/src/Faker/Provider/ar_SA/Company.php @@ -68,7 +68,9 @@ public function bs() **/ public static function companyIdNumber() { - while (!Luhn::isValid($number = static::numerify('700#######'))); + do { + $number = static::numerify('700#######'); + } while (!Luhn::isValid($number)); return $number; } From f95ec9e50626a7971c489c644bc440facf31da73 Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 6 Apr 2016 15:31:55 +0300 Subject: [PATCH 05/11] fixed array notation compat. issue for older php version. --- src/Faker/Provider/ar_SA/Person.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index 04d834f181..2a09feb252 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -101,7 +101,7 @@ protected static function luhnCompatibleFromPattern($pattern) */ public static function idNumber() { - $firstDigit = static::randomElement([1, 2]); + $firstDigit = static::randomElement(array(1, 2)); return static::luhnCompatibleFromPattern($firstDigit . '#########'); } From 757750f980a864ae0ee1ef63ef39013bf2f2624b Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 6 Apr 2016 21:41:28 +0300 Subject: [PATCH 06/11] changed id numbers generation algorithm. --- src/Faker/Provider/ar_SA/Company.php | 7 +--- src/Faker/Provider/ar_SA/Person.php | 16 ++------ src/Faker/Provider/ar_SA/Utils.php | 50 +++++++++++++++++++++++++ test/Faker/Provider/ar_SA/UtilsTest.php | 23 ++++++++++++ 4 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 src/Faker/Provider/ar_SA/Utils.php create mode 100644 test/Faker/Provider/ar_SA/UtilsTest.php diff --git a/src/Faker/Provider/ar_SA/Company.php b/src/Faker/Provider/ar_SA/Company.php index 234286d597..653cf10876 100644 --- a/src/Faker/Provider/ar_SA/Company.php +++ b/src/Faker/Provider/ar_SA/Company.php @@ -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 { @@ -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); } } diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index 2a09feb252..9daa0e2217 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -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 { @@ -87,22 +88,13 @@ 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); } /** @@ -110,7 +102,7 @@ public static function idNumber() */ public static function nationalIdNumber() { - return static::luhnCompatibleFromPattern('1#########'); + return Utils::generateLuhnNumber(1, 10); } /** @@ -118,6 +110,6 @@ public static function nationalIdNumber() */ public static function foreignerIdNumber() { - return static::luhnCompatibleFromPattern('2#########'); + return Utils::generateLuhnNumber(2, 10); } } diff --git a/src/Faker/Provider/ar_SA/Utils.php b/src/Faker/Provider/ar_SA/Utils.php new file mode 100644 index 0000000000..18633e49fe --- /dev/null +++ b/src/Faker/Provider/ar_SA/Utils.php @@ -0,0 +1,50 @@ + 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; + + } +} diff --git a/test/Faker/Provider/ar_SA/UtilsTest.php b/test/Faker/Provider/ar_SA/UtilsTest.php new file mode 100644 index 0000000000..72879b4b11 --- /dev/null +++ b/test/Faker/Provider/ar_SA/UtilsTest.php @@ -0,0 +1,23 @@ +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))); + } +} From 74219b47bb8329714d2cae81023eea96a46e01f4 Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Mon, 11 Apr 2016 21:44:19 +0300 Subject: [PATCH 07/11] use faker utility class to generate luhnien numbers. --- src/Faker/Provider/ar_SA/Company.php | 1 - src/Faker/Provider/ar_SA/Utils.php | 45 +++---------------------- test/Faker/Provider/ar_SA/UtilsTest.php | 8 ++++- 3 files changed, 12 insertions(+), 42 deletions(-) diff --git a/src/Faker/Provider/ar_SA/Company.php b/src/Faker/Provider/ar_SA/Company.php index 653cf10876..9ebf4e182f 100644 --- a/src/Faker/Provider/ar_SA/Company.php +++ b/src/Faker/Provider/ar_SA/Company.php @@ -2,7 +2,6 @@ namespace Faker\Provider\ar_SA; -use Faker\Calculator\Luhn; use Faker\Provider\ar_SA\Utils; class Company extends \Faker\Provider\Company diff --git a/src/Faker/Provider/ar_SA/Utils.php b/src/Faker/Provider/ar_SA/Utils.php index 18633e49fe..189e4c7d1d 100644 --- a/src/Faker/Provider/ar_SA/Utils.php +++ b/src/Faker/Provider/ar_SA/Utils.php @@ -1,50 +1,15 @@ 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; - + $pattern = $prefix . str_repeat('#', $length - strlen($prefix) - 1); + $partialValue = BaseProvider::numerify($pattern); + return $partialValue . Luhn::computeCheckDigit($partialValue); } } diff --git a/test/Faker/Provider/ar_SA/UtilsTest.php b/test/Faker/Provider/ar_SA/UtilsTest.php index 72879b4b11..8a69d0094b 100644 --- a/test/Faker/Provider/ar_SA/UtilsTest.php +++ b/test/Faker/Provider/ar_SA/UtilsTest.php @@ -2,7 +2,6 @@ namespace Faker\Test\Provider\ar_SA; -use Faker\Generator; use Faker\Provider\ar_SA\Utils; use Faker\Calculator\Luhn; @@ -14,6 +13,13 @@ 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))); From 39f225c89b5041fe14184841aa4af18e3f0156a8 Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Mon, 11 Apr 2016 21:49:32 +0300 Subject: [PATCH 08/11] fixed namespace cs issue. --- src/Faker/Provider/ar_SA/Utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Faker/Provider/ar_SA/Utils.php b/src/Faker/Provider/ar_SA/Utils.php index 189e4c7d1d..a666c2fe1a 100644 --- a/src/Faker/Provider/ar_SA/Utils.php +++ b/src/Faker/Provider/ar_SA/Utils.php @@ -1,6 +1,7 @@ Date: Sat, 7 May 2016 22:48:50 +0300 Subject: [PATCH 09/11] moved Luhn number generation to Luhn calculator class. --- src/Faker/Calculator/Luhn.php | 42 ++++++++++++++++++- src/Faker/Provider/ar_SA/Company.php | 4 +- src/Faker/Provider/ar_SA/Person.php | 7 ++-- src/Faker/Provider/ar_SA/Utils.php | 16 ------- test/Faker/Calculator/LuhnTest.php | 56 +++++++++++++++++++++++++ test/Faker/Provider/ar_SA/UtilsTest.php | 29 ------------- 6 files changed, 102 insertions(+), 52 deletions(-) delete mode 100644 src/Faker/Provider/ar_SA/Utils.php delete mode 100644 test/Faker/Provider/ar_SA/UtilsTest.php diff --git a/src/Faker/Calculator/Luhn.php b/src/Faker/Calculator/Luhn.php index f0a86023d5..d60540878e 100644 --- a/src/Faker/Calculator/Luhn.php +++ b/src/Faker/Calculator/Luhn.php @@ -2,8 +2,11 @@ namespace Faker\Calculator; +use Faker\Provider\Base as BaseProvider; +use InvalidArgumentException; + /** - * Utility class for generating Luhn checksum and validating a number + * Utility class for generating and validating Luhn numbers. * * Luhn algorithm is used to validate credit card numbers, IMEI numbers, and * National Provider Identifier numbers. @@ -55,4 +58,41 @@ public static function isValid($number) { return self::checksum($number) === 0; } + + /** + * Generate a Luhn compliant number. + * + * @param string $prefix + * @param int $length + * @return string + */ + public static function generateLuhnNumber($prefix, $length) + { + $length = intval($length); + $prefix = trim($prefix); + + if (!preg_match('/^\d*$/', $prefix)) { + throw new InvalidArgumentException('prefix should be all digits.'); + } + + if ($length <= 0) { + throw new InvalidArgumentException('length should be greater than zero.'); + } + + if (strlen($prefix) > $length) { + throw new InvalidArgumentException('prefix should be longer than the length.'); + } + + if (strlen($prefix) == $length) { + if (static::isValid($prefix)) { + return $prefix; + } else { + throw new InvalidArgumentException('prefix length equals length but prefix is not a Luhn number.'); + } + } + + $pattern = $prefix . str_repeat('#', $length - strlen($prefix) - 1); + $partialValue = BaseProvider::numerify($pattern); + return $partialValue . Luhn::computeCheckDigit($partialValue); + } } diff --git a/src/Faker/Provider/ar_SA/Company.php b/src/Faker/Provider/ar_SA/Company.php index 9ebf4e182f..cb4f480ee6 100644 --- a/src/Faker/Provider/ar_SA/Company.php +++ b/src/Faker/Provider/ar_SA/Company.php @@ -2,7 +2,7 @@ namespace Faker\Provider\ar_SA; -use Faker\Provider\ar_SA\Utils; +use Faker\Calculator\Luhn; class Company extends \Faker\Provider\Company { @@ -68,6 +68,6 @@ public function bs() **/ public static function companyIdNumber() { - return Utils::generateLuhnNumber(700, 10); + return Luhn::generateLuhnNumber(700, 10); } } diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index 9daa0e2217..e8f43acd9e 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -3,7 +3,6 @@ namespace Faker\Provider\ar_SA; use Faker\Calculator\Luhn; -use Faker\Provider\ar_SA\Utils; class Person extends \Faker\Provider\Person { @@ -94,7 +93,7 @@ public static function prefix() public static function idNumber() { $firstDigit = static::randomElement(array(1, 2)); - return Utils::generateLuhnNumber($firstDigit, 10); + return Luhn::generateLuhnNumber($firstDigit, 10); } /** @@ -102,7 +101,7 @@ public static function idNumber() */ public static function nationalIdNumber() { - return Utils::generateLuhnNumber(1, 10); + return Luhn::generateLuhnNumber(1, 10); } /** @@ -110,6 +109,6 @@ public static function nationalIdNumber() */ public static function foreignerIdNumber() { - return Utils::generateLuhnNumber(2, 10); + return Luhn::generateLuhnNumber(2, 10); } } diff --git a/src/Faker/Provider/ar_SA/Utils.php b/src/Faker/Provider/ar_SA/Utils.php deleted file mode 100644 index a666c2fe1a..0000000000 --- a/src/Faker/Provider/ar_SA/Utils.php +++ /dev/null @@ -1,16 +0,0 @@ -assertEquals($isValid, Luhn::isValid($number)); } + + public function generateLuhnNumberProvider() + { + return array( + array(' ', 5), + array(null, 7), + array(443, 10), + array(5, 15), + array(203, 25), + ); + } + + /** + * @dataProvider generateLuhnNumberProvider + */ + public function testGenerateLuhnNumber($prefix, $length) + { + $this->assertEquals($length, strlen(Luhn::generateLuhnNumber($prefix, $length))); + $this->assertTrue(Luhn::isValid(Luhn::generateLuhnNumber($prefix, $length))); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage prefix should be all digits. + */ + public function testGenerateLuhnNumberWithInvalidPrefix() + { + Luhn::generateLuhnNumber('abc', 10); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage length should be greater than zero. + */ + public function testGenerateLuhnNumberWithNegativeLength() + { + Luhn::generateLuhnNumber(1, -5); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage prefix should be longer than the length. + */ + public function testGenerateLuhnNumberWithPrefixGreaterThanLength() + { + Luhn::generateLuhnNumber(123456, 5); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage prefix length equals length but prefix is not a Luhn number. + */ + public function testGenerateLuhnNumberWithPrefixEqualLengthButInvalid() + { + Luhn::generateLuhnNumber(123457, 6); + } } diff --git a/test/Faker/Provider/ar_SA/UtilsTest.php b/test/Faker/Provider/ar_SA/UtilsTest.php deleted file mode 100644 index 8a69d0094b..0000000000 --- a/test/Faker/Provider/ar_SA/UtilsTest.php +++ /dev/null @@ -1,29 +0,0 @@ -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))); - } -} From ad9147d98c2386c49061646ed6095d00ffcd057a Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 5 Oct 2016 22:54:56 +0300 Subject: [PATCH 10/11] separate calculation from random generation with luhn generator in ar_SA provider. --- src/Faker/Calculator/Luhn.php | 29 ++-------------- src/Faker/Provider/ar_SA/Company.php | 3 +- src/Faker/Provider/ar_SA/Person.php | 11 +++--- test/Faker/Calculator/LuhnTest.php | 51 ++-------------------------- 4 files changed, 14 insertions(+), 80 deletions(-) diff --git a/src/Faker/Calculator/Luhn.php b/src/Faker/Calculator/Luhn.php index d60540878e..a76e42e638 100644 --- a/src/Faker/Calculator/Luhn.php +++ b/src/Faker/Calculator/Luhn.php @@ -63,36 +63,13 @@ public static function isValid($number) * Generate a Luhn compliant number. * * @param string $prefix - * @param int $length * @return string */ - public static function generateLuhnNumber($prefix, $length) + public static function generateLuhnNumber($partialValue) { - $length = intval($length); - $prefix = trim($prefix); - - if (!preg_match('/^\d*$/', $prefix)) { - throw new InvalidArgumentException('prefix should be all digits.'); - } - - if ($length <= 0) { - throw new InvalidArgumentException('length should be greater than zero.'); - } - - if (strlen($prefix) > $length) { - throw new InvalidArgumentException('prefix should be longer than the length.'); - } - - if (strlen($prefix) == $length) { - if (static::isValid($prefix)) { - return $prefix; - } else { - throw new InvalidArgumentException('prefix length equals length but prefix is not a Luhn number.'); - } + if(!preg_match('/^\d+$/', $partialValue)) { + throw new InvalidArgumentException('Argument should be an integer.'); } - - $pattern = $prefix . str_repeat('#', $length - strlen($prefix) - 1); - $partialValue = BaseProvider::numerify($pattern); return $partialValue . Luhn::computeCheckDigit($partialValue); } } diff --git a/src/Faker/Provider/ar_SA/Company.php b/src/Faker/Provider/ar_SA/Company.php index cb4f480ee6..596add8949 100644 --- a/src/Faker/Provider/ar_SA/Company.php +++ b/src/Faker/Provider/ar_SA/Company.php @@ -68,6 +68,7 @@ public function bs() **/ public static function companyIdNumber() { - return Luhn::generateLuhnNumber(700, 10); + $partialValue = static::numerify(700 . str_repeat('#', 6)); + return Luhn::generateLuhnNumber($partialValue); } } diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index e8f43acd9e..56e3b578dc 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -92,8 +92,9 @@ public static function prefix() */ public static function idNumber() { - $firstDigit = static::randomElement(array(1, 2)); - return Luhn::generateLuhnNumber($firstDigit, 10); + $partialValue = static::numerify( + static::randomElement(array(1, 2)) . str_repeat('#', 8)); + return Luhn::generateLuhnNumber($partialValue); } /** @@ -101,7 +102,8 @@ public static function idNumber() */ public static function nationalIdNumber() { - return Luhn::generateLuhnNumber(1, 10); + $partialValue = static::numerify(1 . str_repeat('#', 8)); + return Luhn::generateLuhnNumber($partialValue); } /** @@ -109,6 +111,7 @@ public static function nationalIdNumber() */ public static function foreignerIdNumber() { - return Luhn::generateLuhnNumber(2, 10); + $partialValue = static::numerify(2 . str_repeat('#', 8)); + return Luhn::generateLuhnNumber($partialValue); } } diff --git a/test/Faker/Calculator/LuhnTest.php b/test/Faker/Calculator/LuhnTest.php index 5988f5dd41..2d38c83d6d 100644 --- a/test/Faker/Calculator/LuhnTest.php +++ b/test/Faker/Calculator/LuhnTest.php @@ -60,59 +60,12 @@ public function testIsValid($number, $isValid) $this->assertEquals($isValid, Luhn::isValid($number)); } - public function generateLuhnNumberProvider() - { - return array( - array(' ', 5), - array(null, 7), - array(443, 10), - array(5, 15), - array(203, 25), - ); - } - - /** - * @dataProvider generateLuhnNumberProvider - */ - public function testGenerateLuhnNumber($prefix, $length) - { - $this->assertEquals($length, strlen(Luhn::generateLuhnNumber($prefix, $length))); - $this->assertTrue(Luhn::isValid(Luhn::generateLuhnNumber($prefix, $length))); - } - /** * @expectedException InvalidArgumentException - * @expectedExceptionMessage prefix should be all digits. + * @expectedExceptionMessage Argument should be an integer. */ public function testGenerateLuhnNumberWithInvalidPrefix() { - Luhn::generateLuhnNumber('abc', 10); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage length should be greater than zero. - */ - public function testGenerateLuhnNumberWithNegativeLength() - { - Luhn::generateLuhnNumber(1, -5); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage prefix should be longer than the length. - */ - public function testGenerateLuhnNumberWithPrefixGreaterThanLength() - { - Luhn::generateLuhnNumber(123456, 5); - } - - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage prefix length equals length but prefix is not a Luhn number. - */ - public function testGenerateLuhnNumberWithPrefixEqualLengthButInvalid() - { - Luhn::generateLuhnNumber(123457, 6); + Luhn::generateLuhnNumber('abc'); } } From e2d48f868dcaf1c373bff2f15dab4f3d373baf54 Mon Sep 17 00:00:00 2001 From: Mohamed Abidi Date: Wed, 5 Oct 2016 23:00:43 +0300 Subject: [PATCH 11/11] fixed code style issues. --- src/Faker/Calculator/Luhn.php | 2 +- src/Faker/Provider/ar_SA/Person.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Faker/Calculator/Luhn.php b/src/Faker/Calculator/Luhn.php index a76e42e638..6d35172c49 100644 --- a/src/Faker/Calculator/Luhn.php +++ b/src/Faker/Calculator/Luhn.php @@ -67,7 +67,7 @@ public static function isValid($number) */ public static function generateLuhnNumber($partialValue) { - if(!preg_match('/^\d+$/', $partialValue)) { + if (!preg_match('/^\d+$/', $partialValue)) { throw new InvalidArgumentException('Argument should be an integer.'); } return $partialValue . Luhn::computeCheckDigit($partialValue); diff --git a/src/Faker/Provider/ar_SA/Person.php b/src/Faker/Provider/ar_SA/Person.php index 56e3b578dc..9bcd542fb4 100644 --- a/src/Faker/Provider/ar_SA/Person.php +++ b/src/Faker/Provider/ar_SA/Person.php @@ -93,7 +93,8 @@ public static function prefix() public static function idNumber() { $partialValue = static::numerify( - static::randomElement(array(1, 2)) . str_repeat('#', 8)); + static::randomElement(array(1, 2)) . str_repeat('#', 8) + ); return Luhn::generateLuhnNumber($partialValue); }