Ean.php
5 years ago
Iban.php
5 years ago
Inn.php
5 years ago
Luhn.php
5 years ago
TCNo.php
5 years ago
TCNo.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Faker\Calculator; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | class TCNo |
| 8 | { |
| 9 | /** |
| 10 | * Generates Turkish Identity Number Checksum |
| 11 | * Gets first 9 digit as prefix and calculates checksum |
| 12 | * |
| 13 | * https://en.wikipedia.org/wiki/Turkish_Identification_Number |
| 14 | * |
| 15 | * @param string $identityPrefix |
| 16 | * @return string Checksum (two digit) |
| 17 | */ |
| 18 | public static function checksum($identityPrefix) |
| 19 | { |
| 20 | if (strlen((string)$identityPrefix) !== 9) { |
| 21 | throw new InvalidArgumentException('Argument should be an integer and should be 9 digits.'); |
| 22 | } |
| 23 | |
| 24 | $oddSum = 0; |
| 25 | $evenSum = 0; |
| 26 | |
| 27 | $identityArray = array_map('intval', str_split($identityPrefix)); // Creates array from int |
| 28 | foreach ($identityArray as $index => $digit) { |
| 29 | if ($index % 2 == 0) { |
| 30 | $evenSum += $digit; |
| 31 | } else { |
| 32 | $oddSum += $digit; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | $tenthDigit = (7 * $evenSum - $oddSum) % 10; |
| 37 | $eleventhDigit = ($evenSum + $oddSum + $tenthDigit) % 10; |
| 38 | |
| 39 | return $tenthDigit . $eleventhDigit; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Checks whether a TCNo has a valid checksum |
| 44 | * |
| 45 | * @param string $tcNo |
| 46 | * @return boolean |
| 47 | */ |
| 48 | public static function isValid($tcNo) |
| 49 | { |
| 50 | return self::checksum(substr($tcNo, 0, -2)) === substr($tcNo, -2, 2); |
| 51 | } |
| 52 | } |
| 53 |