From 93557c2cf695d7b0bd55a2d8968d1e311102ec42 Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Tue, 14 Mar 2017 11:47:33 +0600 Subject: [PATCH 1/9] Generate valid Individual identification number --- src/Faker/Provider/kk_KZ/Person.php | 121 ++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 16 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index 0009d91b59..1f3ba300e6 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -2,33 +2,76 @@ namespace Faker\Provider\kk_KZ; +use \Faker\Provider\DateTime; + class Person extends \Faker\Provider\Person { + const GENDER_MALE = 0; + const GENDER_FEMALE = 1; + + const CENTURY_19th = 0; + const CENTURY_20th = 1; + const CENTURY_21st = 2; + + const MALE_CENTURY_19th = 1; + const MALE_CENTURY_20th = 3; + const MALE_CENTURY_21th = 5; + + const FEMALE_CENTURY_19th = 2; + const FEMALE_CENTURY_20th = 4; + const FEMALE_CENTURY_21th = 6; + + /** + * @var array + */ + protected static $firstSequenceBitWeights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; + + /** + * @var array + */ + protected static $secondSequenceBitWeights = [3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2]; + + /** + * @var array + */ + protected static $genderCenturyMap = [ + self::GENDER_MALE => [ + self::CENTURY_19th => self::MALE_CENTURY_19th, + self::CENTURY_20th => self::MALE_CENTURY_20th, + self::CENTURY_21st => self::MALE_CENTURY_21th, + ], + self::GENDER_FEMALE => [ + self::CENTURY_19th => self::FEMALE_CENTURY_19th, + self::CENTURY_20th => self::FEMALE_CENTURY_20th, + self::CENTURY_21st => self::FEMALE_CENTURY_21th, + ], + ]; + /** * @see https://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B7%D0%B0%D1%85%D1%81%D0%BA%D0%B0%D1%8F_%D1%84%D0%B0%D0%BC%D0%B8%D0%BB%D0%B8%D1%8F * * @var array */ - protected static $maleNameFormats = array( + protected static $maleNameFormats = [ '{{lastName}}ұлы {{firstNameMale}}', - ); + ]; /** * @see https://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B7%D0%B0%D1%85%D1%81%D0%BA%D0%B0%D1%8F_%D1%84%D0%B0%D0%BC%D0%B8%D0%BB%D0%B8%D1%8F * * @var array */ - protected static $femaleNameFormats = array( + protected static $femaleNameFormats = [ '{{lastName}}қызы {{firstNameFemale}}', - ); + ]; /** * @see http://koshpendi.kz/index.php/nomad/imena/ * * @var array */ - protected static $firstNameMale = array( + protected static $firstNameMale = [ 'Аылғазы', 'Әбдіқадыр', 'Бабағожа', @@ -57,14 +100,14 @@ class Person extends \Faker\Provider\Person 'Шаттық', 'Ыстамбақы', 'Ібни', - ); + ]; /** * @see http://koshpendi.kz/index.php/nomad/imena/ * * @var array */ - protected static $firstNameFemale = array( + protected static $firstNameFemale = [ 'Асылтас', 'Әужа', 'Бүлдіршін', @@ -94,7 +137,7 @@ class Person extends \Faker\Provider\Person 'Шырынгүл', 'Ырысты', 'Іңкәр', - ); + ]; /** * @see http://koshpendi.kz/index.php/nomad/imena/ @@ -102,7 +145,7 @@ class Person extends \Faker\Provider\Person * * @var array */ - protected static $lastName = array( + protected static $lastName = [ 'Адырбай', 'Әжібай', 'Байбөрі', @@ -131,25 +174,71 @@ class Person extends \Faker\Provider\Person 'Шілдебай', 'Ыстамбақы', 'Ісмет', - ); + ]; + + /** + * @param integer $year + * + * @return integer + */ + private static function getCenturyByYear($year) + { + if ($year >= 1800 && $year <= 1899) { + return self::CENTURY_19th; + } elseif ($year >= 1900 && $year <= 1999) { + return self::CENTURY_20th; + } elseif ($year >= 2000 && $year <= DateTime::year()) { + return self::CENTURY_21st; + } + } /** * National Individual Identification Numbers * * @link http://egov.kz/wps/portal/Content?contentPath=%2Fegovcontent%2Fcitizen_migration%2Fpassport_id_card%2Farticle%2Fiin_info&lang=en + * @link https://ru.wikipedia.org/wiki/%D0%98%D0%BD%D0%B4%D0%B8%D0%B2%D0%B8%D0%B4%D1%83%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B9_%D0%B8%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80 + * * @param \DateTime $birthDate + * @param integer $gender + * * @return string 12 digits, like 780322300455 */ - public static function individualIdentificationNumber(\DateTime $birthDate = null) + public static function individualIdentificationNumber(\DateTime $birthDate = null, $gender = self::GENDER_MALE) { if (!$birthDate) { - $birthDate = \Faker\Provider\DateTime::dateTimeBetween(); + $birthDate = DateTime::dateTimeBetween(); } - $dateAsString = $birthDate->format('ymd'); - $genderAndCenturyId = (string) static::numberBetween(1, 6); - $randomDigits = (string) static::numerify('#####'); + $dateAsString = $birthDate->format('ymd'); + $totalPopulation = rand(1000, 2000); + $century = self::getCenturyByYear((int)$birthDate->format('Y')); + $iin = $dateAsString; + $sum = 0; + $calculatedResult = []; + + $iin .= (string)self::$genderCenturyMap[$gender][$century]; + $iin .= (string)$totalPopulation; + + for ($i = 0; $i <= 10; $i++) { + $calculatedResult[$i] = (int)substr($iin, $i, 1); + $sum += $calculatedResult[$i] * self::$firstSequenceBitWeights[$i]; + } + + $controlDigit = $sum % 11; + $calculatedResult[11] = $controlDigit; + + if ($controlDigit === 10) { + $sum = 0; + + for ($i = 0; $i <= 10; $i++) { + $calculatedResult[$i] = (int)substr($iin, $i, 1); + $sum += $calculatedResult[$i] * self::$secondSequenceBitWeights[$i]; + } + + $controlDigit = $sum % 11; + $calculatedResult[11] = $controlDigit; + } - return $dateAsString . $genderAndCenturyId . $randomDigits; + return implode('', $calculatedResult); } } From f24aa1a5ce6d5a80003134ebdf2a08b21f86da6b Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Tue, 14 Mar 2017 11:53:28 +0600 Subject: [PATCH 2/9] Switch array short notation for PHP5.3 --- src/Faker/Provider/kk_KZ/Person.php | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index 1f3ba300e6..1fa7a35647 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -16,62 +16,62 @@ class Person extends \Faker\Provider\Person const MALE_CENTURY_19th = 1; const MALE_CENTURY_20th = 3; - const MALE_CENTURY_21th = 5; + const MALE_CENTURY_21st = 5; const FEMALE_CENTURY_19th = 2; const FEMALE_CENTURY_20th = 4; - const FEMALE_CENTURY_21th = 6; + const FEMALE_CENTURY_21st = 6; /** * @var array */ - protected static $firstSequenceBitWeights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; + protected static $firstSequenceBitWeights = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); /** * @var array */ - protected static $secondSequenceBitWeights = [3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2]; + protected static $secondSequenceBitWeights = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2); /** * @var array */ - protected static $genderCenturyMap = [ - self::GENDER_MALE => [ + protected static $genderCenturyMap = array( + self::GENDER_MALE => array( self::CENTURY_19th => self::MALE_CENTURY_19th, self::CENTURY_20th => self::MALE_CENTURY_20th, - self::CENTURY_21st => self::MALE_CENTURY_21th, - ], - self::GENDER_FEMALE => [ + self::CENTURY_21st => self::MALE_CENTURY_21st, + ), + self::GENDER_FEMALE => array( self::CENTURY_19th => self::FEMALE_CENTURY_19th, self::CENTURY_20th => self::FEMALE_CENTURY_20th, - self::CENTURY_21st => self::FEMALE_CENTURY_21th, - ], - ]; + self::CENTURY_21st => self::FEMALE_CENTURY_21st, + ), + ); /** * @see https://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B7%D0%B0%D1%85%D1%81%D0%BA%D0%B0%D1%8F_%D1%84%D0%B0%D0%BC%D0%B8%D0%BB%D0%B8%D1%8F * * @var array */ - protected static $maleNameFormats = [ + protected static $maleNameFormats = array( '{{lastName}}ұлы {{firstNameMale}}', - ]; + ); /** * @see https://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B7%D0%B0%D1%85%D1%81%D0%BA%D0%B0%D1%8F_%D1%84%D0%B0%D0%BC%D0%B8%D0%BB%D0%B8%D1%8F * * @var array */ - protected static $femaleNameFormats = [ + protected static $femaleNameFormats = array( '{{lastName}}қызы {{firstNameFemale}}', - ]; + ); /** * @see http://koshpendi.kz/index.php/nomad/imena/ * * @var array */ - protected static $firstNameMale = [ + protected static $firstNameMale = array( 'Аылғазы', 'Әбдіқадыр', 'Бабағожа', @@ -100,14 +100,14 @@ class Person extends \Faker\Provider\Person 'Шаттық', 'Ыстамбақы', 'Ібни', - ]; + ); /** * @see http://koshpendi.kz/index.php/nomad/imena/ * * @var array */ - protected static $firstNameFemale = [ + protected static $firstNameFemale = array( 'Асылтас', 'Әужа', 'Бүлдіршін', @@ -137,7 +137,7 @@ class Person extends \Faker\Provider\Person 'Шырынгүл', 'Ырысты', 'Іңкәр', - ]; + ); /** * @see http://koshpendi.kz/index.php/nomad/imena/ @@ -145,7 +145,7 @@ class Person extends \Faker\Provider\Person * * @var array */ - protected static $lastName = [ + protected static $lastName = array( 'Адырбай', 'Әжібай', 'Байбөрі', @@ -174,7 +174,7 @@ class Person extends \Faker\Provider\Person 'Шілдебай', 'Ыстамбақы', 'Ісмет', - ]; + ); /** * @param integer $year From b4a63ff576a3898774584b50beb371dbea158a5d Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Tue, 14 Mar 2017 11:54:54 +0600 Subject: [PATCH 3/9] Switch array short notation for PHP5.3 WIP --- src/Faker/Provider/kk_KZ/Person.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index 1fa7a35647..dfc9d3902a 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -214,7 +214,7 @@ public static function individualIdentificationNumber(\DateTime $birthDate = nul $century = self::getCenturyByYear((int)$birthDate->format('Y')); $iin = $dateAsString; $sum = 0; - $calculatedResult = []; + $calculatedResult = array(); $iin .= (string)self::$genderCenturyMap[$gender][$century]; $iin .= (string)$totalPopulation; From 651cd6f7b3eff3a2425c0e03c477413c680a077e Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Tue, 14 Mar 2017 13:04:09 +0600 Subject: [PATCH 4/9] Fix constant names --- src/Faker/Provider/kk_KZ/Person.php | 36 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index dfc9d3902a..1e606c4fb0 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -10,17 +10,17 @@ class Person extends \Faker\Provider\Person const GENDER_MALE = 0; const GENDER_FEMALE = 1; - const CENTURY_19th = 0; - const CENTURY_20th = 1; - const CENTURY_21st = 2; + const CENTURY_19TH = 0; + const CENTURY_20TH = 1; + const CENTURY_21ST = 2; - const MALE_CENTURY_19th = 1; - const MALE_CENTURY_20th = 3; - const MALE_CENTURY_21st = 5; + const MALE_CENTURY_19TH = 1; + const MALE_CENTURY_20TH = 3; + const MALE_CENTURY_21ST = 5; - const FEMALE_CENTURY_19th = 2; - const FEMALE_CENTURY_20th = 4; - const FEMALE_CENTURY_21st = 6; + const FEMALE_CENTURY_19TH = 2; + const FEMALE_CENTURY_20TH = 4; + const FEMALE_CENTURY_21ST = 6; /** * @var array @@ -37,14 +37,14 @@ class Person extends \Faker\Provider\Person */ protected static $genderCenturyMap = array( self::GENDER_MALE => array( - self::CENTURY_19th => self::MALE_CENTURY_19th, - self::CENTURY_20th => self::MALE_CENTURY_20th, - self::CENTURY_21st => self::MALE_CENTURY_21st, + self::CENTURY_19TH => self::MALE_CENTURY_19TH, + self::CENTURY_20TH => self::MALE_CENTURY_20TH, + self::CENTURY_21ST => self::MALE_CENTURY_21ST, ), self::GENDER_FEMALE => array( - self::CENTURY_19th => self::FEMALE_CENTURY_19th, - self::CENTURY_20th => self::FEMALE_CENTURY_20th, - self::CENTURY_21st => self::FEMALE_CENTURY_21st, + self::CENTURY_19TH => self::FEMALE_CENTURY_19TH, + self::CENTURY_20TH => self::FEMALE_CENTURY_20TH, + self::CENTURY_21ST => self::FEMALE_CENTURY_21ST, ), ); @@ -184,11 +184,11 @@ class Person extends \Faker\Provider\Person private static function getCenturyByYear($year) { if ($year >= 1800 && $year <= 1899) { - return self::CENTURY_19th; + return self::CENTURY_19TH; } elseif ($year >= 1900 && $year <= 1999) { - return self::CENTURY_20th; + return self::CENTURY_20TH; } elseif ($year >= 2000 && $year <= DateTime::year()) { - return self::CENTURY_21st; + return self::CENTURY_21ST; } } From 9dea2ddaf9137a7341462c527f31179fb006c634 Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Tue, 14 Mar 2017 13:21:59 +0600 Subject: [PATCH 5/9] Fix test --- src/Faker/Provider/kk_KZ/Person.php | 6 ++--- test/Faker/Provider/kk_KZ/PersonTest.php | 28 +++++++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index 1e606c4fb0..3a3702e1f8 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -25,17 +25,17 @@ class Person extends \Faker\Provider\Person /** * @var array */ - protected static $firstSequenceBitWeights = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); + public static $firstSequenceBitWeights = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); /** * @var array */ - protected static $secondSequenceBitWeights = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2); + public static $secondSequenceBitWeights = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2); /** * @var array */ - protected static $genderCenturyMap = array( + public static $genderCenturyMap = array( self::GENDER_MALE => array( self::CENTURY_19TH => self::MALE_CENTURY_19TH, self::CENTURY_20TH => self::MALE_CENTURY_20TH, diff --git a/test/Faker/Provider/kk_KZ/PersonTest.php b/test/Faker/Provider/kk_KZ/PersonTest.php index 48e65683e5..c34d058d32 100644 --- a/test/Faker/Provider/kk_KZ/PersonTest.php +++ b/test/Faker/Provider/kk_KZ/PersonTest.php @@ -2,6 +2,7 @@ namespace Faker\Test\Provider\kk_KZ; use Faker\Generator; +use Faker\Provider\DateTime; use Faker\Provider\kk_KZ\Person; class PersonTest extends \PHPUnit_Framework_TestCase @@ -19,13 +20,28 @@ public function setUp() public function testIndividualIdentificationNumberIsValid() { - $birthDate = new \DateTime('now'); + $birthDate = DateTime::dateTimeBetween('-30 years', '-10 years'); $individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate); - $birthDateAsString = $birthDate->format('ymd'); + $sum = 0; - $this->assertRegExp( - "/^(" . $birthDateAsString . ")([1-6]{1})(\\d{5})$/", - $individualIdentificationNumber - ); + for ($i = 0; $i <= 10; $i++) { + $calculatedResult[$i] = (int)substr($individualIdentificationNumber, $i, 1); + $sum += $calculatedResult[$i] * Person::$firstSequenceBitWeights[$i]; + } + + $controlDigit = $sum % 11; + + if ($controlDigit === 10) { + $sum = 0; + + for ($i = 0; $i <= 10; $i++) { + $calculatedResult[$i] = (int)substr($individualIdentificationNumber, $i, 1); + $sum += $calculatedResult[$i] * Person::$secondSequenceBitWeights[$i]; + } + + $controlDigit = $sum % 11; + } + + $this->assertTrue($controlDigit === (int)substr($individualIdentificationNumber, 11, 1)); } } From 96377e57ed3ebbe4abde915895e8730855aba5be Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Tue, 14 Mar 2017 14:47:29 +0600 Subject: [PATCH 6/9] Remove unnecessary && --- src/Faker/Provider/kk_KZ/Person.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index 3a3702e1f8..87f0fe47d2 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -179,16 +179,16 @@ class Person extends \Faker\Provider\Person /** * @param integer $year * - * @return integer + * @return integer|null */ private static function getCenturyByYear($year) { - if ($year >= 1800 && $year <= 1899) { - return self::CENTURY_19TH; - } elseif ($year >= 1900 && $year <= 1999) { - return self::CENTURY_20TH; - } elseif ($year >= 2000 && $year <= DateTime::year()) { + if ($year >= 2000 && $year <= DateTime::year()) { return self::CENTURY_21ST; + } elseif ($year >= 1900) { + return self::CENTURY_20TH; + } elseif ($year >= 1800) { + return self::CENTURY_19TH; } } From d939425797199c3494a8a3110c5d925dc85db0d7 Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Wed, 15 Mar 2017 09:57:39 +0600 Subject: [PATCH 7/9] checkSum separate method --- src/Faker/Provider/kk_KZ/Person.php | 38 ++++++++++++++++-------- test/Faker/Provider/kk_KZ/PersonTest.php | 20 +------------ 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index 87f0fe47d2..e6e477934f 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -209,36 +209,48 @@ public static function individualIdentificationNumber(\DateTime $birthDate = nul $birthDate = DateTime::dateTimeBetween(); } - $dateAsString = $birthDate->format('ymd'); - $totalPopulation = rand(1000, 2000); + $totalPopulation = mt_rand(1000, 2000); $century = self::getCenturyByYear((int)$birthDate->format('Y')); - $iin = $dateAsString; - $sum = 0; + $iin = $birthDate->format('ymd'); $calculatedResult = array(); $iin .= (string)self::$genderCenturyMap[$gender][$century]; $iin .= (string)$totalPopulation; + $controlDigit = self::checkSum($iin, $calculatedResult); + $calculatedResult[11] = $controlDigit; + + return implode('', $calculatedResult); + } + + /** + * @param string $iinValue + * @param array $calculatedResult + * + * @return integer + */ + public static function checkSum($iinValue, &$calculatedResult = array()) + { + $sum = 0; + for ($i = 0; $i <= 10; $i++) { - $calculatedResult[$i] = (int)substr($iin, $i, 1); - $sum += $calculatedResult[$i] * self::$firstSequenceBitWeights[$i]; + $calculatedResult[$i] = (int)substr($iinValue, $i, 1); + $sum += $calculatedResult[$i] * Person::$firstSequenceBitWeights[$i]; } - $controlDigit = $sum % 11; - $calculatedResult[11] = $controlDigit; + $controlDigit = $sum % 11; if ($controlDigit === 10) { $sum = 0; for ($i = 0; $i <= 10; $i++) { - $calculatedResult[$i] = (int)substr($iin, $i, 1); - $sum += $calculatedResult[$i] * self::$secondSequenceBitWeights[$i]; + $calculatedResult[$i] = (int)substr($iinValue, $i, 1); + $sum += $calculatedResult[$i] * Person::$secondSequenceBitWeights[$i]; } - $controlDigit = $sum % 11; - $calculatedResult[11] = $controlDigit; + $controlDigit = $sum % 11; } - return implode('', $calculatedResult); + return $controlDigit; } } diff --git a/test/Faker/Provider/kk_KZ/PersonTest.php b/test/Faker/Provider/kk_KZ/PersonTest.php index c34d058d32..0f2f981cd3 100644 --- a/test/Faker/Provider/kk_KZ/PersonTest.php +++ b/test/Faker/Provider/kk_KZ/PersonTest.php @@ -22,25 +22,7 @@ public function testIndividualIdentificationNumberIsValid() { $birthDate = DateTime::dateTimeBetween('-30 years', '-10 years'); $individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate); - $sum = 0; - - for ($i = 0; $i <= 10; $i++) { - $calculatedResult[$i] = (int)substr($individualIdentificationNumber, $i, 1); - $sum += $calculatedResult[$i] * Person::$firstSequenceBitWeights[$i]; - } - - $controlDigit = $sum % 11; - - if ($controlDigit === 10) { - $sum = 0; - - for ($i = 0; $i <= 10; $i++) { - $calculatedResult[$i] = (int)substr($individualIdentificationNumber, $i, 1); - $sum += $calculatedResult[$i] * Person::$secondSequenceBitWeights[$i]; - } - - $controlDigit = $sum % 11; - } + $controlDigit = Person::checkSum($individualIdentificationNumber); $this->assertTrue($controlDigit === (int)substr($individualIdentificationNumber, 11, 1)); } From df2726ce8a2934c71585697f5816195309f3b27d Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Wed, 15 Mar 2017 14:31:36 +0600 Subject: [PATCH 8/9] update kk_KZ person readme [ci-skip] --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 5bafa20df1..93b79eb912 100644 --- a/readme.md +++ b/readme.md @@ -829,6 +829,9 @@ echo $faker->businessIdentificationNumber; // "150140000019" // Generates an individual identification number echo $faker->individualIdentificationNumber; // "780322300455" +// Generates an individual identification number based on his/her birth date +echo $faker->individualIdentificationNumber(new \DateTime('1999-03-01')); // "990301300455" + ``` ### `Faker\Provider\ko_KR\Address` From 63aecf81f163a98c550eb18ff768283d754eaccf Mon Sep 17 00:00:00 2001 From: Yerlen Zhubangaliyev Date: Wed, 15 Mar 2017 20:04:54 +0600 Subject: [PATCH 9/9] method refactoring --- src/Faker/Provider/kk_KZ/Person.php | 52 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/Faker/Provider/kk_KZ/Person.php b/src/Faker/Provider/kk_KZ/Person.php index e6e477934f..7e52f74bdc 100644 --- a/src/Faker/Provider/kk_KZ/Person.php +++ b/src/Faker/Provider/kk_KZ/Person.php @@ -209,48 +209,46 @@ public static function individualIdentificationNumber(\DateTime $birthDate = nul $birthDate = DateTime::dateTimeBetween(); } - $totalPopulation = mt_rand(1000, 2000); - $century = self::getCenturyByYear((int)$birthDate->format('Y')); - $iin = $birthDate->format('ymd'); - $calculatedResult = array(); + $population = mt_rand(1000, 2000); + $century = self::getCenturyByYear((int) $birthDate->format('Y')); - $iin .= (string)self::$genderCenturyMap[$gender][$century]; - $iin .= (string)$totalPopulation; + $iin = $birthDate->format('ymd'); + $iin .= (string) self::$genderCenturyMap[$gender][$century]; + $iin .= (string) $population; - $controlDigit = self::checkSum($iin, $calculatedResult); - $calculatedResult[11] = $controlDigit; - - return implode('', $calculatedResult); + return $iin . (string) self::checkSum($iin); } /** - * @param string $iinValue - * @param array $calculatedResult + * @param string $iinValue * * @return integer */ - public static function checkSum($iinValue, &$calculatedResult = array()) + public static function checkSum($iinValue) { - $sum = 0; + $controlDigit = self::getControlDigit($iinValue, self::$firstSequenceBitWeights); - for ($i = 0; $i <= 10; $i++) { - $calculatedResult[$i] = (int)substr($iinValue, $i, 1); - $sum += $calculatedResult[$i] * Person::$firstSequenceBitWeights[$i]; + if ($controlDigit === 10) { + return self::getControlDigit($iinValue, self::$secondSequenceBitWeights); } - $controlDigit = $sum % 11; - - if ($controlDigit === 10) { - $sum = 0; + return $controlDigit; + } - for ($i = 0; $i <= 10; $i++) { - $calculatedResult[$i] = (int)substr($iinValue, $i, 1); - $sum += $calculatedResult[$i] * Person::$secondSequenceBitWeights[$i]; - } + /** + * @param string $iinValue + * @param array $sequence + * + * @return integer + */ + protected static function getControlDigit($iinValue, $sequence) + { + $sum = 0; - $controlDigit = $sum % 11; + for ($i = 0; $i <= 10; $i++) { + $sum += (int) $iinValue[$i] * $sequence[$i]; } - return $controlDigit; + return $sum % 11; } }