GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d90ae7...580929 )
by Yunus Emre
05:49
created

TCKimlikNo   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 95.56%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 42
c 4
b 2
f 0
dl 0
loc 119
ccs 43
cts 45
cp 0.9556
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 23 5
B validate() 0 37 7
A generateChecksumDigits() 0 13 2
A toUppercaseTr() 0 7 1
1
<?php
2
3
namespace Deligoez\TCKimlikNo;
4
5
use SoapClient;
6
7
class TCKimlikNo
8
{
9
    /**
10
     * Validates Citizenship Number using nvi.gov.tr.
11
     *
12
     * @param  string  $tcKimlikNo
13
     * @param  string  $name
14
     * @param  string  $surname
15
     * @param  string  $birthYear
16
     * @param  bool  $autoUppercase
17
     * @return bool
18
     * @throws \SoapFault
19
     */
20 5
    public static function validate(
21
        $tcKimlikNo,
22
        string $name,
23
        string $surname,
24
        $birthYear,
25
        bool $autoUppercase = true
26
    ): bool {
27 5
        if ($autoUppercase) {
28 5
            $name = self::toUppercaseTr($name);
29 5
            $surname = self::toUppercaseTr($surname);
30
        }
31
32 5
        if (! preg_match('/^[A-Z ÇĞÖŞÜİ]+$/', self::toUppercaseTr($name))) {
33 1
            return false;
34
        }
35
36 5
        if (! preg_match('/^[A-Z ÇĞÖŞÜİ]+$/', self::toUppercaseTr($surname))) {
37 1
            return false;
38
        }
39
40 5
        if (! preg_match('/^[0-9]{4}$/', $birthYear)) {
41 1
            return false;
42
        }
43
44 4
        if (! self::verify($tcKimlikNo)) {
45 1
            return false;
46
        }
47
48 3
        $response = (new SoapClient('https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx?WSDL'))
49 3
            ->TCKimlikNoDogrula([
50 3
                'TCKimlikNo' => intval($tcKimlikNo),
51 3
                'Ad'         => trim($name),
52 3
                'Soyad'      => trim($surname),
53 3
                'DogumYili'  => intval($birthYear),
54
            ]);
55
56 3
        return $response->TCKimlikNoDogrulaResult ? true : false;
57
    }
58
59
    /**
60
     * Prepares, trims, and makes uppercase names.
61
     *
62
     * @param $name
63
     * @return bool|false|mixed|string|string[]|null
64
     */
65 8
    private static function toUppercaseTr(string $name): string
66
    {
67 8
        return mb_strtoupper(
68 8
            str_replace(
69 8
                ['ç', 'ğ', 'ı', 'ö', 'ş', 'ü', 'i'],
70 8
                ['Ç', 'Ğ', 'I', 'Ö', 'Ş', 'Ü', 'İ'],
71 8
                $name
72
            )
73
        );
74
    }
75
76
    /**
77
     * Verify Citizenship Number According to it's Algorithm.
78
     *
79
     * @param  string  $tcKimlikNo
80
     * @return bool
81
     */
82 13
    public static function verify($tcKimlikNo): bool
83
    {
84 13
        $tcKimlikNo = strval($tcKimlikNo);
85
86 13
        if (strlen(strval($tcKimlikNo)) != 11) {
87 3
            return false;
88
        }
89
90 10
        if (! preg_match('/^[1-9]{1}[0-9]{9}[0,2,4,6,8]{1}$/', $tcKimlikNo)) {
91 2
            return false;
92
        }
93
94 8
        $checksumDigits = static::generateChecksumDigits($tcKimlikNo);
95
96 8
        if ($checksumDigits[0] != $tcKimlikNo[9]) {
97 3
            return false;
98
        }
99
100 7
        if ($checksumDigits[1] != $tcKimlikNo[10]) {
101
            return false;
102
        }
103
104 7
        return true;
105
    }
106
107
    /**
108
     * Generates Checksum Digits from the first 9 Digits.
109
     *
110
     * @param $tcKimlikNo
111
     * @return string
112
     */
113 9
    public static function generateChecksumDigits($tcKimlikNo): string
114
    {
115 9
        $oddDigitsSum = $tcKimlikNo[0] + $tcKimlikNo[2] + $tcKimlikNo[4] + $tcKimlikNo[6] + $tcKimlikNo[8];
116 9
        $evenDigitsSum = $tcKimlikNo[1] + $tcKimlikNo[3] + $tcKimlikNo[5] + $tcKimlikNo[7];
117
118 9
        $digit10 = ($oddDigitsSum * 7 - $evenDigitsSum) % 10;
119 9
        $digit11 = ($oddDigitsSum + $evenDigitsSum + $digit10) % 10;
120
121 9
        if ($digit10 < 0) {
122
            $digit10 += 10;
123
        }
124
125 9
        return $digit10.$digit11;
126
    }
127
}
128