Created
July 27, 2017 11:35
-
-
Save emir/9712a17fdf1bd04736d311cef39bfaf3 to your computer and use it in GitHub Desktop.
Type Value Object for AIG Identity Service
This file contains hidden or 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
<?php | |
namespace App\Domain\Identity\ValueObjects; | |
final class Type | |
{ | |
const TURKISH_TYPE = 'TURKISH'; | |
const FOREIGNER_TYPE = 'FOREIGNER'; | |
const CORPORATION_TYPE = 'CORPORATION'; | |
/** | |
* @var string | |
*/ | |
private $type; | |
/** | |
* @param string $number | |
*/ | |
public function __construct(string $number) | |
{ | |
$this->validate($number); | |
} | |
/** | |
* @param string $number | |
* @return bool | |
*/ | |
private function validate(string $number): bool | |
{ | |
if (strlen($number) === 11 && true === $this->validateIdentityNumber($number)) { | |
$this->type = $this->findIdentityNumberType($number); | |
} elseif (strlen($number) === 10 && true === $this->validateTaxNumber($number)) { | |
$this->type = self::CORPORATION_TYPE; | |
} else { | |
throw new \InvalidArgumentException('Allowed Turkish Identity number size is 11, VAT number size is 10'); | |
} | |
return true; | |
} | |
/** | |
* @param string $number | |
* @return string | |
*/ | |
private function findIdentityNumberType(string $number): string | |
{ | |
if ($number[0] !== 9) { | |
return self::TURKISH_TYPE; | |
} | |
$this->type = self::FOREIGNER_TYPE; | |
} | |
/** | |
* This method logically validates Turkish Identity number | |
* | |
* @param string $identityNumber | |
* @return bool | |
*/ | |
private function validateIdentityNumber(string $identityNumber): bool | |
{ | |
if (!preg_match('/^[1-9]{1}[0-9]{9}[0,2,4,6,8]{1}$/', $identityNumber)) { | |
return false; | |
} | |
$odd = $identityNumber[0] + $identityNumber[2] + $identityNumber[4] + $identityNumber[6] + $identityNumber[8]; | |
$even = $identityNumber[1] + $identityNumber[3] + $identityNumber[5] + $identityNumber[7]; | |
$digit10 = ($odd * 7 - $even) % 10; | |
$total = ($odd + $even + $identityNumber[9]) % 10; | |
if ($digit10 != $identityNumber[9] || $total != $identityNumber[10]) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* This method logically validates Turkish VAT number | |
* | |
* @param string $taxNumber | |
* @return bool | |
*/ | |
private function validateTaxNumber(string $taxNumber): bool | |
{ | |
if (strlen($taxNumber) !== 10) { | |
return false; | |
} | |
$total = 0; | |
$checkNum = null; | |
for ($i = 0; $i < 9; $i++) { | |
$tmp1 = ($taxNumber[$i] + (9 - $i)) % 10; | |
$tmp2 = ($tmp1 * (2 ** (9 - $i))) % 9; | |
if ($tmp1 !== 0 && $tmp2 === 0) { | |
$tmp2 = 9; | |
} | |
$total += $tmp2; | |
} | |
if ($total % 10 === 0) { | |
$checkNum = 0; | |
} else { | |
$checkNum = 10 - ($total % 10); | |
} | |
if ((int)$taxNumber[9] !== $checkNum) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* @return string | |
*/ | |
public function getType(): string | |
{ | |
return $this->type; | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->getType(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment