Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the way of generating Egyptian National Ids #651

Merged
merged 5 commits into from
May 28, 2023
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
64 changes: 37 additions & 27 deletions src/Faker/Provider/ar_EG/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,35 @@ class Address extends \Faker\Provider\Address

/**
* @see https://ar.wikipedia.org/wiki/%D9%82%D8%A7%D8%A6%D9%85%D8%A9_%D9%85%D8%AD%D8%A7%D9%81%D8%B8%D8%A7%D8%AA_%D9%85%D8%B5%D8%B1
* @see https://ar.wikipedia.org/wiki/%D8%A8%D8%B7%D8%A7%D9%82%D8%A9_%D8%A7%D9%84%D8%B1%D9%82%D9%85_%D8%A7%D9%84%D9%82%D9%88%D9%85%D9%8A_%D8%A7%D9%84%D9%85%D8%B5%D8%B1%D9%8A%D8%A9
*/
protected static $governorates = [
'الإسكندرية',
'الإسماعيلية',
'أسوان',
'أسيوط',
'الأقصر',
'البحر الأحمر',
'البحيرة',
'بني سويف',
'بورسعيد',
'جنوب سيناء',
'الجيزة',
'الدقهلية',
'دمياط',
'سوهاج',
'السويس',
'الشرقية',
'شمال سيناء',
'الغربية',
'الفيوم',
'القاهرة',
'القليوبية',
'قنا',
'كفر الشيخ',
'مطروح',
'المنوفية',
'المنيا',
'الوادي الجديد',
'الإسكندرية' => '02',
'الإسماعيلية' => '19',
'أسوان' => '28',
'أسيوط' => '25',
'الأقصر' => '29',
'البحر الأحمر' => '31',
'البحيرة' => '18',
'بني سويف' => '22',
'بورسعيد' => '03',
'جنوب سيناء' => '35',
'القاهرة' => '01',
'الدقهلية' => '12',
'دمياط' => '11',
'سوهاج' => '26',
'السويس' => '04',
'الشرقية' => '13',
'شمال سيناء' => '34',
'الغربية' => '16',
'الفيوم' => '23',
'القليوبية' => '14',
'قنا' => '27',
'كفر الشيخ' => '15',
'مطروح' => '33',
'المنوفية' => '17',
'المنيا' => '24',
'الوادي الجديد' => '32',
];

protected static $buildingNumber = ['%####', '%###', '%#'];
Expand Down Expand Up @@ -201,6 +201,16 @@ public static function secondaryAddress()
* @example 'الإسكندرية'
*/
public static function governorate()
{
return static::randomKey(static::$governorates);
}

/**
* @example '01'
*
* @return string
*/
public static function governorateId()
{
return static::randomElement(static::$governorates);
}
Expand Down
26 changes: 19 additions & 7 deletions src/Faker/Provider/ar_EG/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Faker\Provider\ar_EG;

use Faker\Calculator\Luhn;

class Person extends \Faker\Provider\Person
{
protected static $maleNameFormats = [
Expand Down Expand Up @@ -80,16 +78,30 @@ public static function prefix()
}

/**
* @see https://ar.wikipedia.org/wiki/%D8%A8%D8%B7%D8%A7%D9%82%D8%A9_%D8%A7%D9%84%D8%B1%D9%82%D9%85_%D8%A7%D9%84%D9%82%D9%88%D9%85%D9%8A_%D8%A7%D9%84%D9%85%D8%B5%D8%B1%D9%8A%D8%A9
*
* @example 27512310101010
*
* @return string
*/
public static function nationalIdNumber()
public static function nationalIdNumber($gender = null)
{
$timestamp = self::numberBetween(1, time());
$randomBirthDateTimestamp = mt_rand(strtotime('1950-Jan-10'), strtotime('2005-Dec-25'));

$centuryId = ((int) date('Y', $randomBirthDateTimestamp)) >= 2000 ? 3 : 2;
$fullBirthDate = date('ymd', $randomBirthDateTimestamp);
$governorateId = Address::governorateId();
$birthRegistrationSequence = mt_rand(1, 500);

$date = explode(':', date('y:m:d', $timestamp));
if ($gender === static::GENDER_MALE) {
$birthRegistrationSequence = $birthRegistrationSequence | 1; // Convert to the nearest odd number
} elseif ($gender === static::GENDER_FEMALE) {
$birthRegistrationSequence = $birthRegistrationSequence & ~1; // Convert to the nearest even number
}

$partialValue = static::numerify(2 . $date[0] . $date[1] . $date[2] . str_repeat('#', 6));
$birthRegistrationSequence = str_pad((string) $birthRegistrationSequence, 4, '0', STR_PAD_LEFT);
$randomCheckDigit = mt_rand(1, 9);

return Luhn::generateLuhnNumber($partialValue);
return $centuryId . $fullBirthDate . $governorateId . $birthRegistrationSequence . $randomCheckDigit;
}
}
4 changes: 1 addition & 3 deletions test/Faker/Provider/ar_EG/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Faker\Test\Provider\ar_EG;

use Faker\Calculator\Luhn;
use Faker\Provider\ar_EG\Person;
use Faker\Test\TestCase;

Expand All @@ -14,8 +13,7 @@ final class PersonTest extends TestCase
public function testNationalIdNumber(): void
{
$nationalIdNumber = $this->faker->nationalIdNumber();
self::assertMatchesRegularExpression('/^2\d{13}$/', $nationalIdNumber);
self::assertTrue(Luhn::isValid($nationalIdNumber));
self::assertMatchesRegularExpression('/^\d{14}$/', $nationalIdNumber);
}

protected function getProviders(): iterable
Expand Down