Skip to content

Commit

Permalink
Merge pull request #11 from epigra/refactor/tc-kimlik-class
Browse files Browse the repository at this point in the history
RAIDEN-5 refactor TcKimlik class and tests
  • Loading branch information
taylanunutmaz committed Jan 12, 2024
2 parents d4a1bbb + 2382539 commit f043b68
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 90 deletions.
173 changes: 87 additions & 86 deletions src/TcKimlik.php
@@ -1,15 +1,16 @@
<?php
<?php

namespace Epigra;

class TcKimlik
{
private static $validationfields = ['tcno', 'isim', 'soyisim', 'dogumyili'];
private static $yabancivalidationfields = ["tcno","isim","soyisim","dogumgunu","dogumayi","dogumyili"];
private static $validationFields = ['tcno', 'isim', 'soyisim', 'dogumyili'];
private static $yabanciValidationFields = ["tcno", "isim", "soyisim", "dogumgunu", "dogumayi", "dogumyili"];

public static function verify($input)
{
$tcno = $input;

if (is_array($input) && !empty($input['tcno'])) {
$tcno = $input['tcno'];
}
Expand All @@ -22,7 +23,11 @@ public static function verify($input)
if (!preg_match('/^[1-9]{1}[0-9]{9}[0,2,4,6,8]{1}$/', $tcno)) {
return false;
}


if (is_int($tcno)) {
$tcno = (string) $tcno;
}

$odd = $tcno[0] + $tcno[2] + $tcno[4] + $tcno[6] + $tcno[8];
$even = $tcno[1] + $tcno[3] + $tcno[5] + $tcno[7];
$digit10 = ($odd * 7 - $even) % 10;
Expand All @@ -35,119 +40,115 @@ public static function verify($input)
return true;
}

public static function validate(array $data, $auto_uppercase = true)
public static function validate(array $data, $autoUppercase = true)
{
if (! self::verify($data)) {
if (!self::verify($data)) {
return false;
}

if (count(array_diff(self::$validationfields, array_keys($data))) != 0) {
return false;
}
$yabanci=isset($data['yabanci']) && $data['yabanci'];
if($yabanci)
$response = self::yabanciKimlikValidate( $data,$auto_uppercase);
else
$response = self::tcKimlikValidate( $data,$auto_uppercase);
$response = isset($data['yabanci']) && $data['yabanci'] == true ?
self::yabanciKimlikValidate($data, $autoUppercase) :
self::tcKimlikValidate($data, $autoUppercase);

return (strip_tags($response) === 'true') ? true : false;
return strip_tags($response) === 'true';
}
private static function tcKimlikValidate(Array $data,$auto_uppercase = TRUE)

public static function trUppercase($string)
{
if($auto_uppercase){
foreach(self::$validationfields as $field){
$data[$field] = self::tr_uppercase($data[$field]);
}
}
$string = str_replace(array('i'), array('İ'), $string);
return mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
}

$post_data = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TCKimlikNoDogrula xmlns="http://tckimlik.nvi.gov.tr/WS">
<TCKimlikNo>'.$data['tcno'].'</TCKimlikNo>
<Ad>'.$data['isim'].'</Ad>
<Soyad>'.$data['soyisim'].'</Soyad>
<DogumYili>'.$data['dogumyili'].'</DogumYili>
</TCKimlikNoDogrula>
</soap:Body>
</soap:Envelope>';

$ch = curl_init();

// CURL options
$options = array(
CURLOPT_URL => 'https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
'POST /Service/KPSPublic.asmx HTTP/1.1',
'Host: tckimlik.nvi.gov.tr',
'Content-Type: text/xml; charset=utf-8',
'SOAPAction: "http://tckimlik.nvi.gov.tr/WS/TCKimlikNoDogrula"',
'Content-Length: '.strlen($post_data)
),
);
curl_setopt_array($ch, $options);
private static function tcKimlikValidate(array $data, $autoUppercase = TRUE)
{
if (count(array_diff(self::$validationFields, array_keys($data))) != 0) {
return false;
}

$response = curl_exec($ch);
curl_close($ch);
return $response;
}
if ($autoUppercase) {
foreach (self::$validationFields as $field) {
$data[$field] = self::trUppercase($data[$field]);
}
}

public static function tr_uppercase($string){
$string = str_replace(array('i'), array('İ'), $string);
return mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
return self::sendSoapRequest(
'/Service/KPSPublic.asmx',
[
'TCKimlikNo' => $data['tcno'],
'Ad' => $data['isim'],
'Soyad' => $data['soyisim'],
'DogumYili' => $data['dogumyili']
],
'TCKimlikNoDogrula'
);
}


private static function yabanciKimlikValidate(Array $data,$auto_uppercase = TRUE)
private static function yabanciKimlikValidate(array $data, $autoUppercase = TRUE)
{
if($auto_uppercase){
foreach(self::$yabancivalidationfields as $field){
if ($autoUppercase) {
foreach (self::$yabanciValidationFields as $field) {
$data[$field] = mb_convert_case($data[$field], MB_CASE_UPPER, "UTF-8");
}
}

$post_data = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<YabanciKimlikNoDogrula xmlns="http://tckimlik.nvi.gov.tr/WS">
<KimlikNo>'.$data['tcno'].'</KimlikNo>
<Ad>'.$data['isim'].'</Ad>
<Soyad>'.$data['soyisim'].'</Soyad>
<DogumGun>'.$data['dogumgunu'].'</DogumGun>
<DogumAy>'.$data['dogumayi'].'</DogumAy>
<DogumYil>'.$data['dogumyili'].'</DogumYil>
</YabanciKimlikNoDogrula>
</soap:Body>
</soap:Envelope>';
return self::sendSoapRequest(
'/Service/KPSPublicYabanciDogrula.asmx',
[
'KimlikNo' => $data['tcno'],
'Ad' => $data['isim'],
'Soyad' => $data['soyisim'],
'DogumGun' => $data['dogumgunu'],
'DogumAy' => $data['dogumayi'],
'DogumYil' => $data['dogumyili']
],
'YabanciKimlikNoDogrula'
);
}

private static function sendSoapRequest(string $url, array $payload, string $soapAction)
{
$fields = array_reduce(
array_chunk($payload, 1, true),
function ($r, $i) {
return $r . '<' . key($i) . ">" . current($i) . '</' . key($i) . ">" . PHP_EOL;
},
""
);

$ch = curl_init();

$postData = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<' . $soapAction . ' xmlns="http://tckimlik.nvi.gov.tr/WS">
' . $fields . '
</' . $soapAction . '>
</soap:Body>
</soap:Envelope>';

// CURL options
$options = array(
CURLOPT_URL => 'https://tckimlik.nvi.gov.tr/Service/KPSPublicYabanciDogrula.asmx',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
'POST /Service/KPSPublicYabanciDogrula.asmx HTTP/1.1',
CURLOPT_URL => 'https://tckimlik.nvi.gov.tr' . $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
'POST ' . $url . ' HTTP/1.1',
'Host: tckimlik.nvi.gov.tr',
'Content-Type: text/xml; charset=utf-8',
'SOAPAction: "http://tckimlik.nvi.gov.tr/WS/YabanciKimlikNoDogrula"',
'Content-Length: '.strlen($post_data)
'SOAPAction: "http://tckimlik.nvi.gov.tr/WS/' . $soapAction . '"',
'Content-Length: ' . strlen($postData)
),
);
curl_setopt_array($ch, $options);

$response = curl_exec($ch);

curl_close($ch);
return $response;

return $response;
}
}
4 changes: 2 additions & 2 deletions tests/Unit/ValidateLowercaseTest.php
Expand Up @@ -85,8 +85,8 @@ public function test_validate_lowercase_wrong_info() {
public function test_validate_lowercase_env_definitions() {
$check = TcKimlik::validate([
'tcno' => $this->TCIdentificationNumber,
'isim' => TcKimlik::tr_uppercase($this->FirstName),
'soyisim' => TcKimlik::tr_uppercase($this->LastName),
'isim' => TcKimlik::trUppercase($this->FirstName),
'soyisim' => TcKimlik::trUppercase($this->LastName),
'dogumyili' => $this->BirthYear
], false);

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ValidatorTest.php
Expand Up @@ -57,7 +57,7 @@ public function test_verify_bad_tc_identification_number_regex_match() {
*@group Unit
**/
public function test_verify_good_tc_identification_regex_match() {
$number = 10000000000;
$number = 10774881040;
$check = TcKimlik::verify($number);

$validator = \Validator::make(['tcno' => $number], [
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/VerifyTCIdentificationNumberTest.php
Expand Up @@ -61,7 +61,7 @@ public function test_verify_bad_tc_identification_number_regex_match() {
*@group Unit
**/
public function test_verify_good_tc_identification_regex_match() {
$number = 10000000000;
$number = 10774881040;
$check = TcKimlik::verify($number);

$this->assertTrue($check);
Expand Down

0 comments on commit f043b68

Please sign in to comment.