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

Commit cb01380

Browse files
committed
Add Valid National Code Generator to fa_IR
1 parent 76b1c25 commit cb01380

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

readme.md

+9
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,15 @@ echo $faker->vat; // "A35864370"
10231023
echo $faker->dni; // '83367512'
10241024
```
10251025

1026+
### `Faker\Provider\fa_IR\Person`
1027+
1028+
```php
1029+
<?php
1030+
1031+
// Generates a valid nationalCode
1032+
echo $faker->nationalCode; // "0078475759"
1033+
```
1034+
10261035
### `Faker\Provider\fa_IR\Address`
10271036

10281037
```php

src/Faker/Provider/fa_IR/Person.php

+64
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,68 @@ class Person extends \Faker\Provider\Person
134134

135135
protected static $titleMale = array('آقای', 'استاد', 'دکتر', 'مهندس');
136136
protected static $titleFemale = array('خانم', 'استاد', 'دکتر', 'مهندس');
137+
138+
/**
139+
* This method returns a valid Iranian nationalCode
140+
* @example '8075859741'
141+
* @link https://fa.wikipedia.org/wiki/%DA%A9%D8%A7%D8%B1%D8%AA_%D8%B4%D9%86%D8%A7%D8%B3%D8%A7%DB%8C%DB%8C_%D9%85%D9%84%DB%8C#%D8%AD%D8%B3%D8%A7%D8%A8_%DA%A9%D8%B1%D8%AF%D9%86_%DA%A9%D8%AF_%DA%A9%D9%86%D8%AA%D8%B1%D9%84
142+
* @return string
143+
*/
144+
public static function nationalCode()
145+
{
146+
$area = self::createAreaCode();
147+
$core = self::createCoreCode();
148+
$control = self::createControlCode($area, $core);
149+
150+
return sprintf("%03d%06d%01d", $area, $core, $control);
151+
}
152+
153+
/**
154+
* This method generates a 3-digit valid area code to be used in nationalCode
155+
* @return int|string
156+
*/
157+
private static function createAreaCode()
158+
{
159+
$area = "000";
160+
161+
while ($area == "000") {
162+
$area = static::numerify("###");
163+
}
164+
165+
return $area;
166+
}
167+
168+
/**
169+
* This method randomly generates a 6-digit core code for nationalCode
170+
* @return string
171+
*/
172+
private static function createCoreCode()
173+
{
174+
return static::numerify("######");
175+
}
176+
177+
/**
178+
* This method uses the Iranian nationalCode validation algorithm to generate a valid 10-digit code
179+
* @param $area
180+
* @param $core
181+
* @link https://fa.wikipedia.org/wiki/%DA%A9%D8%A7%D8%B1%D8%AA_%D8%B4%D9%86%D8%A7%D8%B3%D8%A7%DB%8C%DB%8C_%D9%85%D9%84%DB%8C#%D8%AD%D8%B3%D8%A7%D8%A8_%DA%A9%D8%B1%D8%AF%D9%86_%DA%A9%D8%AF_%DA%A9%D9%86%D8%AA%D8%B1%D9%84
182+
* @return int
183+
*/
184+
private static function createControlCode($area, $core)
185+
{
186+
$subNationalCodeString = $area . $core;
187+
188+
$sum = 0;
189+
$count = 0;
190+
191+
for ($i = 10; $i > 1; $i--) {
192+
$sum += $subNationalCodeString[$count] * ($i);
193+
$count++;
194+
}
195+
196+
if (($sum % 11) < 2) {
197+
return $sum % 11;
198+
}
199+
return 11 - ($sum % 11);
200+
}
137201
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Faker\Test\Provider\fa_IR;
4+
5+
use Faker\Provider\fa_IR\Person;
6+
use Faker\Generator;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class PersonTest extends TestCase
10+
{
11+
12+
/**
13+
* @var Generator
14+
*/
15+
private $faker;
16+
17+
public function setUp()
18+
{
19+
$faker = new Generator();
20+
$faker->addProvider(new Person($faker));
21+
$this->faker = $faker;
22+
}
23+
24+
public function testNationalCode()
25+
{
26+
for ($i = 0; $i < 100; $i++) {
27+
$nationalCode = $this->faker->nationalCode;
28+
29+
// nationalCode should be in the format ##########
30+
$this->assertRegExp('/^[0-9]{10}$/', $nationalCode);
31+
32+
$areaCode = substr($nationalCode, 0, 3);
33+
$controlCode = substr($nationalCode, 9, 1);
34+
35+
// the areaCode must in the format ###, excluding '000'
36+
$this->assertNotEquals('000', $areaCode);
37+
38+
// the controlCode should comply with the Iranian National Code validation algorithm
39+
$sum = 0;
40+
$count = 0;
41+
42+
for ($j = 10; $j > 1; $j--) {
43+
$sum += $nationalCode[$count] * ($j);
44+
$count++;
45+
}
46+
47+
if (($sum % 11) < 2) {
48+
$this->assertEquals($sum % 11, (int)$controlCode);
49+
} else {
50+
$this->assertEquals(11 - ($sum % 11), (int)$controlCode);
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)