forked from loophp/tin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountryHandler.php
156 lines (127 loc) · 3.39 KB
/
CountryHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace loophp\Tin\CountryHandler;
use loophp\Tin\Exception\TINException;
use function strlen;
/**
* Base handler class.
*/
abstract class CountryHandler implements CountryHandlerInterface
{
/**
* @var string
*/
private $tin;
private $clean;
/**
* CountryHandler constructor.
*/
final public function __construct(string $tin = '', ?bool $clean = true)
{
$this->tin = $tin;
$this->clean = $clean;
}
public function getTIN(): string
{
if (!$this->clean) {
return strtoupper($this->tin);
}
if (null !== $string = preg_replace('#[^[:alnum:]\-+]#u', '', $this->tin)) {
return strtoupper($string);
}
return '';
}
final public static function supports(string $country): bool
{
return strtoupper($country) === strtoupper(static::COUNTRYCODE);
}
final public function validate(): bool
{
$tin = $this->getTIN();
if (!$this->hasValidLength($tin)) {
throw TINException::invalidLength($this->tin);
}
if (!$this->hasValidPattern($tin)) {
throw TINException::invalidPattern($this->tin);
}
if (!$this->hasValidDate($tin)) {
throw TINException::invalidDate($this->tin);
}
if (!$this->hasValidRule($tin)) {
throw TINException::invalidSyntax($this->tin);
}
return true;
}
/**
* @param string $tin
* The TIN.
*/
final public function withTIN(string $tin): CountryHandlerInterface
{
$clone = clone $this;
$clone->tin = $tin;
return $clone;
}
/**
* Get digit at a given position.
*/
protected function digitAt(string $str, int $index): int
{
return (int) ($str[$index] ?? 0);
}
protected function digitsSum(int $int): int
{
return array_reduce(
str_split((string) $int),
static function (int $carry, string $digit): int {
return $carry + (int) $digit;
},
0
);
}
/**
* Get the alphabetical position.
*
* eg: A = 1
*/
protected function getAlphabeticalPosition(string $character): int
{
return 1 + array_flip(range('a', 'z'))[strtolower($character)];
}
protected function getLastDigit(int $number): int
{
$split = str_split((string) $number);
return (int) end($split);
}
protected function hasValidDate(string $tin): bool
{
return true;
}
/**
* Match length.
*/
protected function hasValidLength(string $tin): bool
{
return $this->matchLength($this->getTIN(), static::LENGTH);
}
protected function hasValidPattern(string $tin): bool
{
return $this->matchPattern($this->getTIN(), static::PATTERN);
}
protected function hasValidRule(string $tin): bool
{
return true;
}
protected function matchLength(string $tin, int $length): bool
{
return strlen($tin) === $length;
}
protected function matchPattern(string $subject, string $pattern): bool
{
return 1 === preg_match(sprintf('/%s/', $pattern), $subject);
}
}