1 | <?php |
||
2 | |||
3 | namespace Milwad\LaravelValidate\Traits; |
||
4 | |||
5 | trait IbanTrait |
||
6 | { |
||
7 | /** |
||
8 | * Set value of $countries property. |
||
9 | */ |
||
10 | private function setCountries(?array $countries): void |
||
11 | { |
||
12 | if (empty($countries)) { |
||
13 | $this->countries = []; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
14 | |||
15 | return; |
||
16 | } |
||
17 | |||
18 | if (is_array($countries[0])) { |
||
19 | $countries = $countries[0]; |
||
20 | } |
||
21 | |||
22 | foreach ($countries as $country) { |
||
23 | $this->countries[] = $country; |
||
24 | } |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Check IBAN is valid. |
||
29 | */ |
||
30 | private function isIbanValid(string $iban): bool |
||
31 | { |
||
32 | if (! $this->checkIbanFormat($iban)) { |
||
33 | return false; |
||
34 | } |
||
35 | |||
36 | /* |
||
37 | * Connect Iban title with value (code) ex: 8330001234567NO . |
||
38 | */ |
||
39 | $parsedIban = substr($iban, 4).substr($iban, 0, 4); |
||
40 | |||
41 | /* |
||
42 | * Replace iban value with character map. |
||
43 | */ |
||
44 | $parsedIban = strtr($parsedIban, $this->characterMap); |
||
45 | |||
46 | return bcmod($parsedIban, '97') === '1'; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Check IBAN format is valid. |
||
51 | */ |
||
52 | private function checkIbanFormat(string $iban): bool |
||
53 | { |
||
54 | if (empty($iban)) { |
||
55 | return false; |
||
56 | } |
||
57 | |||
58 | $ibanCountryCode = $this->getIbanCountryCode($iban); |
||
59 | |||
60 | return ! (empty($this->checkIfBcmodIsAvailable()) |
||
61 | || ! $this->twoFirstCharactersValid($ibanCountryCode) |
||
62 | || ! $this->isCountriesValid($ibanCountryCode) |
||
63 | || ! $this->isIbanLengthValid($iban, $ibanCountryCode)); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get IBAN country code. |
||
68 | */ |
||
69 | private function getIbanCountryCode(string $iban): string |
||
70 | { |
||
71 | return substr($iban, 0, 2); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Check if bcmod function is available. |
||
76 | */ |
||
77 | private function checkIfBcmodIsAvailable(): bool |
||
78 | { |
||
79 | return function_exists('bcmod'); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Check two first character's validity. |
||
84 | */ |
||
85 | private function twoFirstCharactersValid(string $countryCode): bool |
||
86 | { |
||
87 | return ! empty($countryCode) && ctype_alpha($countryCode); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Check countries of the IBAN. |
||
92 | */ |
||
93 | private function isCountriesValid(string $ibanCountryCode): bool |
||
94 | { |
||
95 | if (empty($this->countries)) { |
||
96 | return true; |
||
97 | } |
||
98 | |||
99 | foreach ($this->countries as $country) { |
||
100 | if ($this->isCountryValid($country, $ibanCountryCode)) { |
||
101 | return true; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | return false; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Check country of the IBAN. |
||
110 | */ |
||
111 | private function isCountryValid(string $country, string $ibanCountryCode): bool |
||
112 | { |
||
113 | return ! empty($country) |
||
114 | && isset($this->ibanLengthByCountry[$country]) |
||
115 | && $ibanCountryCode === $country; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Check country of the IBAN. |
||
120 | */ |
||
121 | private function isIbanLengthValid(string $iban, string $ibanCountryCode): bool |
||
122 | { |
||
123 | return strlen($iban) === $this->ibanLengthByCountry[$ibanCountryCode]; |
||
124 | } |
||
125 | } |
||
126 |