1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeDevBr\Bankly\Validators\Pix; |
4
|
|
|
|
5
|
|
|
use WeDevBr\Bankly\Types\Pix\AddressingKey; |
6
|
|
|
|
7
|
|
|
class AddressingKeyValidator |
8
|
|
|
{ |
9
|
|
|
/** @var AddressingKey */ |
10
|
|
|
private $addressingKey; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param AddressingKey $addressingKey |
14
|
|
|
*/ |
15
|
|
|
public function __construct(AddressingKey $addressingKey) |
16
|
|
|
{ |
17
|
|
|
$this->addressingKey = $addressingKey; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Validate the attributes of the Addressing Key class |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function validate(): void |
26
|
|
|
{ |
27
|
|
|
$this->validateType(); |
28
|
|
|
$this->validateValue(); |
29
|
|
|
$this->validateEvpType(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This validates a type of pix key |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
* @throws \InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
private function validateType() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$type = $this->addressingKey->type; |
41
|
|
|
if (empty($type) || !is_string($type)) { |
42
|
|
|
throw new \InvalidArgumentException('type should be a string'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$typeList = [ |
46
|
|
|
'CPF', |
47
|
|
|
'CNPJ', |
48
|
|
|
'EMAIL', |
49
|
|
|
'PHONE', |
50
|
|
|
'EVP', |
51
|
|
|
]; |
52
|
|
|
if (!in_array($this->addressingKey->type, $typeList)) { |
53
|
|
|
throw new \InvalidArgumentException('this key type is not valid'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* This validates a key value |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
private function validateValue() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$value = $this->addressingKey->value; |
66
|
|
|
if ($this->addressingKey->type !== 'EVP' && (empty($value) || !is_string($value))) { |
67
|
|
|
throw new \InvalidArgumentException('value should be a string'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* This validates a key value |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
* @throws \InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
private function validateEvpType() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$value = $this->addressingKey->value; |
80
|
|
|
if ($this->addressingKey->type === 'EVP' && !empty($value)) |
81
|
|
|
{ |
82
|
|
|
throw new \InvalidArgumentException('value must be empty for EVP type'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.