This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Generate valid individual identification numbers kk_KZ #1161
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
93557c2
Generate valid Individual identification number
YerlenZhubangaliyev f24aa1a
Switch array short notation for PHP5.3
YerlenZhubangaliyev b4a63ff
Switch array short notation for PHP5.3 WIP
YerlenZhubangaliyev 651cd6f
Fix constant names
YerlenZhubangaliyev 9dea2dd
Fix test
YerlenZhubangaliyev 96377e5
Remove unnecessary &&
YerlenZhubangaliyev d939425
checkSum separate method
YerlenZhubangaliyev df2726c
update kk_KZ person readme [ci-skip]
YerlenZhubangaliyev 3d6d208
Merge remote-tracking branch 'remotes/upstream/master'
YerlenZhubangaliyev 63aecf8
method refactoring
YerlenZhubangaliyev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,52 @@ | |
|
||
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_21ST = 5; | ||
|
||
const FEMALE_CENTURY_19TH = 2; | ||
const FEMALE_CENTURY_20TH = 4; | ||
const FEMALE_CENTURY_21ST = 6; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public static $firstSequenceBitWeights = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public static $secondSequenceBitWeights = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public 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::GENDER_FEMALE => array( | ||
self::CENTURY_19TH => self::FEMALE_CENTURY_19TH, | ||
self::CENTURY_20TH => self::FEMALE_CENTURY_20TH, | ||
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 | ||
* | ||
|
@@ -133,23 +176,81 @@ class Person extends \Faker\Provider\Person | |
'Ісмет', | ||
); | ||
|
||
/** | ||
* @param integer $year | ||
* | ||
* @return integer|null | ||
*/ | ||
private static function getCenturyByYear($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; | ||
} | ||
} | ||
|
||
/** | ||
* 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haven't tested, but looks like your methods can be refactored like this: public static function individualIdentificationNumber(\DateTime $birthDate = null, $gender = self::GENDER_MALE)
{
if (!$birthDate) {
$birthDate = DateTime::dateTimeBetween();
}
$population = mt_rand(1000, 2000);
$century = self::getCenturyByYear((int) $birthDate->format('Y'));
$iin = $birthDate->format('ymd');
$iin .= (string) self::$genderCenturyMap[$gender][$century];
$iin .= (string) $population;
return $iin . (string) self::checkSum($iin);
}
/**
* @param string $iinValue
*
* @return integer
*/
public static function checkSum($iinValue)
{
$controlDigit = self::getControlDigit($iinValue, self::$firstSequenceBitWeights);
if ($controlDigit === 10) {
return self::getControlDigit($iinValue, self::$secondSequenceBitWeights);
}
return $controlDigit;
}
/**
* @param string $iinValue
* @param array $sequence
*
* @return integer
*/
protected static function getControlDigit($iinValue, $sequence)
{
$sum = 0;
for ($i = 0; $i <= 10; $i++) {
$sum += (int) $iinValue[$i] * $sequence[$i];
}
return $sum % 11;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok looks fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
{ | ||
if (!$birthDate) { | ||
$birthDate = \Faker\Provider\DateTime::dateTimeBetween(); | ||
$birthDate = DateTime::dateTimeBetween(); | ||
} | ||
|
||
$dateAsString = $birthDate->format('ymd'); | ||
$genderAndCenturyId = (string) static::numberBetween(1, 6); | ||
$randomDigits = (string) static::numerify('#####'); | ||
$totalPopulation = mt_rand(1000, 2000); | ||
$century = self::getCenturyByYear((int)$birthDate->format('Y')); | ||
$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($iinValue, $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($iinValue, $i, 1); | ||
$sum += $calculatedResult[$i] * Person::$secondSequenceBitWeights[$i]; | ||
} | ||
|
||
$controlDigit = $sum % 11; | ||
} | ||
|
||
return $dateAsString . $genderAndCenturyId . $randomDigits; | ||
return $controlDigit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can flip conditions and remove
&&