lekoala /
tin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Tin\Algo; |
||
| 4 | |||
| 5 | use LeKoala\Tin\Util\StringUtil; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Croatia |
||
| 9 | */ |
||
| 10 | class HRAlgorithm extends TINAlgorithm |
||
| 11 | { |
||
| 12 | const LENGTH = 11; |
||
| 13 | const PATTERN = "\\d{11}"; |
||
| 14 | |||
| 15 | public function validate(string $tin) |
||
| 16 | { |
||
| 17 | $str = StringUtil::clearString($tin); |
||
| 18 | if (!StringUtil::isFollowLength($str, 11)) { |
||
| 19 | return StatusCode::INVALID_LENGTH; |
||
| 20 | } |
||
| 21 | if (!StringUtil::isFollowPattern($str, self::PATTERN)) { |
||
| 22 | return StatusCode::INVALID_PATTERN; |
||
| 23 | } |
||
| 24 | if (!$this->isFollowRules($str)) { |
||
| 25 | return StatusCode::INVALID_SYNTAX; |
||
| 26 | } |
||
| 27 | return StatusCode::VALID; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function isFollowRules(string $tin) |
||
| 31 | { |
||
| 32 | $sum = 0; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 33 | $rest = 0; |
||
| 34 | $sum = StringUtil::digitAt($tin, 0) + 10; |
||
| 35 | for ($i = 1; $i < 11; $i++) { |
||
| 36 | $rest = $sum % 10; |
||
| 37 | $rest = (($rest == 0) ? 10 : $rest) * 2 % 11; |
||
| 38 | $sum = $rest + StringUtil::digitAt($tin, $i); |
||
| 39 | } |
||
| 40 | $diff = 11 - $rest; |
||
| 41 | $lastDigit = StringUtil::digitAt($tin, 10); |
||
| 42 | return ($rest == 1 && $lastDigit == 0) || $lastDigit == $diff; |
||
| 43 | } |
||
| 44 | } |
||
| 45 |