1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeDevBr\Bankly\Validators\Card; |
4
|
|
|
|
5
|
|
|
use WeDevBr\Bankly\Types\Card\Card; |
6
|
|
|
use WeDevBr\Bankly\Validators\CpfCnpjValidator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* CardValidator class |
10
|
|
|
* |
11
|
|
|
* PHP version 7.3|7.4|8.0 |
12
|
|
|
* |
13
|
|
|
* @author WeDev Brasil Team <[email protected]> |
14
|
|
|
* @author Rafael Teixeira <[email protected]> |
15
|
|
|
* @copyright 2020 We Dev Tecnologia Ltda |
16
|
|
|
* @link https://github.com/wedevBr/bankly-laravel/ |
17
|
|
|
*/ |
18
|
|
|
class CardValidator |
19
|
|
|
{ |
20
|
|
|
private $card; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* CardValidator constructor. |
24
|
|
|
* @param Card $card |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Card $card) |
27
|
|
|
{ |
28
|
|
|
$this->card = $card; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This validate the virtual card |
33
|
|
|
*/ |
34
|
|
|
public function validate(): void |
35
|
|
|
{ |
36
|
|
|
$this->validateDocumentNumber(); |
37
|
|
|
$this->validateCardName(); |
38
|
|
|
$this->validateAlias(); |
39
|
|
|
$this->validateBankAgency(); |
40
|
|
|
$this->validateBankAccount(); |
41
|
|
|
$this->validateProgramId(); |
42
|
|
|
$this->validatePassword(); |
43
|
|
|
$this->validateAddress(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* This validate a virtual card document |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
private function validateDocumentNumber() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$documentNumber = $this->card->documentNumber; |
55
|
|
|
if (empty($documentNumber) || !is_string($documentNumber) || !is_numeric($documentNumber)) { |
56
|
|
|
throw new \InvalidArgumentException('document number should be a numeric string'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$documentValidator = new CpfCnpjValidator($documentNumber); |
60
|
|
|
$documentValidator->validate(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* This validates a card name |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
* @throws \InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
private function validateCardName() |
70
|
|
|
{ |
71
|
|
|
$cardName = $this->card->cardName; |
72
|
|
|
if (empty($cardName) || !is_string($cardName)) { |
73
|
|
|
throw new \InvalidArgumentException('card name should be a string'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* This validate a virtual card alias |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
* @throws \InvalidArgumentException |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
private function validateAlias() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$alias = $this->card->alias; |
86
|
|
|
if (empty($alias) || !is_string($alias)) { |
87
|
|
|
throw new \InvalidArgumentException('alias should be a string'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* This validate a virtual card bank agency |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
* @throws \InvalidArgumentException |
96
|
|
|
*/ |
97
|
|
|
private function validateBankAgency() |
98
|
|
|
{ |
99
|
|
|
$bankAgency = $this->card->bankAgency; |
100
|
|
|
if (empty($bankAgency) || !is_string($bankAgency) || !is_numeric($bankAgency) || strlen($bankAgency) != 4) { |
101
|
|
|
throw new \InvalidArgumentException('bank agency should be a numeric string'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This validate a virtual card bank account |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
* @throws \InvalidArgumentException |
110
|
|
|
*/ |
111
|
|
|
private function validateBankAccount() |
112
|
|
|
{ |
113
|
|
|
$bankAccount = $this->card->bankAccount; |
114
|
|
|
if (empty($bankAccount) || !is_string($bankAccount) || !is_numeric($bankAccount)) { |
115
|
|
|
throw new \InvalidArgumentException('bank account should be a numeric string'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* This validate a virtual card program ID |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
* @throws \InvalidArgumentException |
124
|
|
|
*/ |
125
|
|
|
private function validateProgramId() |
126
|
|
|
{ |
127
|
|
|
$programId = $this->card->programId; |
128
|
|
|
if (!empty($programId) && (!is_string($programId) || !is_numeric($programId) || strlen($programId) != 32)) { |
129
|
|
|
throw new \InvalidArgumentException('program ID should be a numeric string'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* This validate a virtual card password |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
* @throws \InvalidArgumentException |
138
|
|
|
*/ |
139
|
|
|
private function validatePassword() |
140
|
|
|
{ |
141
|
|
|
$password = $this->card->password; |
142
|
|
|
if (empty($password) || !is_string($password) || !is_numeric($password) || strlen($password) != 4) { |
143
|
|
|
throw new \InvalidArgumentException('password should be a numeric string'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* This validate a virtual card address |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
* @throws \InvalidArgumentException |
152
|
|
|
*/ |
153
|
|
|
private function validateAddress() |
154
|
|
|
{ |
155
|
|
|
$address = $this->card->address; |
156
|
|
|
$address->validate(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* This validate a card type |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
* @throws \InvalidArgumentException |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
private function validateType() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$type = $this->card->type; |
168
|
|
|
if (empty($type) || !is_string($type)) { |
169
|
|
|
throw new \InvalidArgumentException('type should be a string'); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.