Skip to content

Commit 4a65fb6

Browse files
authored
Merge pull request #3 from emretnrvrd/feature/api
Api verify supported.
2 parents f470e2e + 42ec323 commit 4a65fb6

File tree

12 files changed

+449
-179
lines changed

12 files changed

+449
-179
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
{
2-
"name": "emretnrvrd/tckn-helpers",
2+
"name": "emretnrvrd/tckn",
33
"authors": [
44
{
55
"name": "Emre Tanrıverdi",
66
"email": "[email protected]",
77
"homepage": "https://github.com/emretnrvrd"
88
}
99
],
10-
"description": "This package enables algorithmic validation of Turkish Republic Identification Numbers (TCKN) and generates fake TCKN for testing purposes in a straightforward way.",
10+
"description": "This package is the most comprehensive package for the Turkish Republic ID number. It includes functions for algorithmically validating Turkish ID numbers, verifying identity information through an API and generating random Turkish ID numbers for testing purposes.",
1111
"type": "library",
12-
"keywords": ["tckn", "tc kimlik no", "doğrulama", "validator", "verify", "helpers"],
12+
"keywords": ["tckn", "tc kimlik no", "api" , "doğrulama", "validator", "verify", "helpers"],
1313
"minimum-stability": "stable",
14-
"version": "1.0.2",
14+
"version": "1.0.0",
1515
"require": {
1616
"php": ">=7.4"
1717
},
1818
"autoload": {
1919
"psr-4": {
20-
"Emretnrvrd\\TcknHelpers\\": "src/"
20+
"Emretnrvrd\\Tckn\\": "src/"
2121
},
2222
"files": [
23-
"src/helpers.php"
23+
"src/Helpers/helpers.php"
2424
]
2525
},
2626
"config": {

src/Helpers/helpers.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Emretnrvrd\Tckn\Services\TcknVerifier\TcknVerifier;
4+
5+
if(!function_exists('validateTckn')) {
6+
/**
7+
* @param string|int $tckn
8+
* @return bool
9+
*/
10+
function validateTckn(string $tckn): bool
11+
{
12+
return (new \Emretnrvrd\Tckn\Services\TcknValidator\TcknValidator($tckn))->validate();
13+
}
14+
}
15+
16+
if(!function_exists('randomTckn')) {
17+
/**
18+
* @return string
19+
*/
20+
function generateTckn(): string
21+
{
22+
return (new \Emretnrvrd\Tckn\Services\TcknRandom\TcknRandom)->generate();
23+
}
24+
}
25+
26+
if(!function_exists('verifyTckn')) {
27+
/**
28+
* @var string $name
29+
* @var string $lastname
30+
* @var int $birthyear
31+
* @var int $tckn
32+
* @return bool
33+
*/
34+
function verifyTckn(string $name, string $lastname, int $birthyear, int $tckn): bool
35+
{
36+
$tcknVerifier = new Emretnrvrd\Tckn\Services\TcknVerifier\TcknVerifier(
37+
$name, $lastname, $birthyear, $tckn
38+
);
39+
return $tcknVerifier->verify();
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Emretnrvrd\Tckn\Services\TcknRandom;
4+
5+
use Emretnrvrd\Tckn\Services\TcknValidator\TcknValidator;
6+
use Emretnrvrd\Tckn\Traits\TcknValidationDigitsCalculator;
7+
use SebastianBergmann\CodeCoverage\Report\PHP;
8+
9+
class TcknRandom
10+
{
11+
12+
use TcknValidationDigitsCalculator;
13+
14+
/**
15+
* Generates a random TCKN.
16+
*
17+
* @return string
18+
*/
19+
public function generate(): string
20+
{
21+
$randomNineDigit = (string)random_int(100_000_000, 999_999_999);
22+
$calculatedTenthDigit = $this->calculateTenthDigit($randomNineDigit);
23+
$calculatedEleventhDigit = $this->calculateEleventhDigit($randomNineDigit . $calculatedTenthDigit);
24+
25+
return $randomNineDigit . $calculatedTenthDigit . $calculatedEleventhDigit;
26+
}
27+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Emretnrvrd\Tckn\Services\TcknValidator;
4+
5+
use Emretnrvrd\Tckn\Traits\TcknValidationDigitsCalculator;
6+
7+
abstract class TcknValidationCases
8+
{
9+
use TcknValidationDigitsCalculator;
10+
11+
/**
12+
* Validate the is numeric of the TCKN.
13+
* @var string
14+
* @return bool
15+
*/
16+
protected function validateIsNumber(string $value): bool
17+
{
18+
return is_numeric($value);
19+
}
20+
/**
21+
* Validate the length of the TCKN.
22+
* @var string
23+
* @return bool
24+
*/
25+
protected function validateLength(string $value): bool
26+
{
27+
return strlen($value) === 11;
28+
}
29+
30+
/**
31+
* Validate the is even of the TCKN.
32+
* @var string
33+
* @return bool
34+
*/
35+
protected function validateIsEven(string $value): bool
36+
{
37+
return ((int)$value) % 2 === 0;
38+
}
39+
40+
/**
41+
* Validate the first digit of the TCKN.
42+
* @var string
43+
* @return bool
44+
*/
45+
protected function validateFirstDigit(string $value): bool
46+
{
47+
return substr($value, 0, 1) != "0";
48+
}
49+
50+
/**
51+
* Validate the tenth digit of the TCKN.
52+
* @var string
53+
* @return bool
54+
*/
55+
protected function validateTenthDigit(string $value): bool
56+
{
57+
$nineDigits = substr($value, 0, 9);
58+
$tenthDigit = substr($value, 9, 1);
59+
60+
return $tenthDigit == $this->calculateTenthDigit($nineDigits);
61+
}
62+
63+
/**
64+
* Validate the eleventh digit of the TCKN.
65+
* @var string
66+
* @return bool
67+
*/
68+
protected function validateEleventhDigit(string $value): bool
69+
{
70+
$tenDigits = substr($value, 0, 10);
71+
$eleventhDigit = substr($value, 10, 1);
72+
73+
return $eleventhDigit == $this->calculateEleventhDigit($tenDigits);
74+
}
75+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Emretnrvrd\Tckn\Services\TcknValidator;
4+
5+
class TcknValidator extends TcknValidationCases implements TcknValidatorInterface
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private string $value;
11+
12+
/**
13+
* @return string
14+
*/
15+
public function getValue(): string
16+
{
17+
return $this->value;
18+
}
19+
20+
/**
21+
* Sets the value of the TCKN.
22+
*
23+
* @param string|int $value
24+
* @return void
25+
*/
26+
public function setValue(string $value): void
27+
{
28+
$this->value = $value;
29+
}
30+
31+
/**
32+
* @param string|int $tckn
33+
*/
34+
public function __construct(string $tckn = null)
35+
{
36+
if ($tckn != null) {
37+
$this->setValue($tckn);
38+
}
39+
}
40+
41+
/**
42+
* Validates the TCKN.
43+
*
44+
* @return bool
45+
* @throws Exception
46+
*/
47+
public function validate(): bool
48+
{
49+
if (!isset($this->value)) {
50+
throw new \Exception('TCKN value must not be null.');
51+
}
52+
return
53+
$this->validateIsNumber($this->value) &&
54+
$this->validateLength($this->value) &&
55+
$this->validateIsEven($this->value) &&
56+
$this->validateFirstDigit($this->value) &&
57+
$this->validateTenthDigit($this->value) &&
58+
$this->validateEleventhDigit($this->value);
59+
}
60+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Emretnrvrd\Tckn\Services\TcknValidator;
4+
5+
interface TcknValidatorInterface
6+
{
7+
public function getValue(): string;
8+
9+
public function setValue(string $value): void;
10+
11+
public function validate(): bool;
12+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Emretnrvrd\Tckn\Services\TcknVerifier;
4+
5+
use Exception;
6+
7+
class TcknApi
8+
{
9+
private TcknRequest $request;
10+
11+
private const URL = "https://tckimlik.nvi.gov.tr/Service/KPSPublic.asmx";
12+
private const HTTPHEADER = [
13+
'POST /Service/KPSPublic.asmx HTTP/1.1',
14+
'Host: tckimlik.nvi.gov.tr',
15+
'Content-Type: text/xml; charset=utf-8',
16+
'SOAPAction: "http://tckimlik.nvi.gov.tr/WS/TCKimlikNoDogrula"',
17+
'Content-Length: {{length}}'
18+
];
19+
private const XML_PATTERN =
20+
'<?xml version="1.0" encoding="utf-8"?>
21+
<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/">
22+
<soap:Body>
23+
<TCKimlikNoDogrula xmlns="http://tckimlik.nvi.gov.tr/WS">
24+
<TCKimlikNo>{{tckn}}</TCKimlikNo>
25+
<Ad>{{name}}</Ad>
26+
<Soyad>{{last_name}}</Soyad>
27+
<DogumYili>{{birthyear}}</DogumYili>
28+
</TCKimlikNoDogrula>
29+
</soap:Body>
30+
</soap:Envelope>
31+
';
32+
33+
34+
35+
public function __construct(TcknRequest $request)
36+
{
37+
$this->request = $request;
38+
}
39+
40+
41+
/**
42+
* Sends the HTTP request to the TCKN verification API
43+
* @throws Exception if an error occurs due to the API
44+
* @return string the API response
45+
*/
46+
public function request()
47+
{
48+
try {
49+
$curl = curl_init();
50+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
51+
curl_setopt($curl, CURLOPT_POST, true);
52+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
53+
curl_setopt($curl, CURLOPT_HEADER, FALSE);
54+
curl_setopt($curl, CURLOPT_URL, self::URL);
55+
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getXmlData());
56+
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->getHttpHeader());
57+
$response = curl_exec($curl);
58+
curl_close($curl);
59+
60+
return $response;
61+
} catch (\Exception $e) {
62+
throw new Exception("An error occurred due to the API.");
63+
}
64+
}
65+
66+
/**
67+
* Creates the XML request data for the API request
68+
* @return string the XML request data
69+
*/
70+
private function getXmlData()
71+
{
72+
$data = [
73+
"{{tckn}}" => $this->request->tckn,
74+
"{{name}}" => $this->request->name,
75+
"{{last_name}}" => $this->request->lastname,
76+
"{{birthyear}}" => $this->request->birthyear,
77+
];
78+
return str_replace(array_keys($data), array_values($data), self::XML_PATTERN);
79+
}
80+
81+
/**
82+
* Creates the HTTP header for the API request
83+
* @return array the HTTP header
84+
*/
85+
private function getHttpHeader()
86+
{
87+
return str_replace("{{length}}", strlen($this->getXmlData()), self::HTTPHEADER);
88+
}
89+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Emretnrvrd\Tckn\Services\TcknVerifier;
4+
5+
class TcknRequest
6+
{
7+
/**
8+
* @var int
9+
*/
10+
public int $tckn;
11+
/**
12+
* @var string
13+
*/
14+
public string $name;
15+
/**
16+
* @var string
17+
*/
18+
public string $lastname;
19+
/**
20+
* @var int
21+
*/
22+
public int $birthyear;
23+
24+
public function __construct(string $name, string $lastname, int $birthyear, int $tckn)
25+
{
26+
$this->name = $name;
27+
$this->lastname = $lastname;
28+
$this->birthyear = $birthyear;
29+
$this->tckn = $tckn;
30+
}
31+
}

0 commit comments

Comments
 (0)