1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeDevBr\Bankly\Validators\Billet; |
4
|
|
|
|
5
|
|
|
use WeDevBr\Bankly\Types\Billet\Address; |
6
|
|
|
use WeDevBr\Bankly\Types\Billet\Payer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* PayerValidator 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 2021 We Dev Tecnologia Ltda |
16
|
|
|
* @link https://github.com/wedevBr/bankly-laravel/ |
17
|
|
|
*/ |
18
|
|
|
class PayerValidator |
19
|
|
|
{ |
20
|
|
|
/** @var Payer */ |
21
|
|
|
private $payer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Payer $payer |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Payer $payer) |
27
|
|
|
{ |
28
|
|
|
$this->payer = $payer; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Validate the attributes of the payer class |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function validate(): void |
37
|
|
|
{ |
38
|
|
|
$this->validateDocument(); |
39
|
|
|
$this->validateName(); |
40
|
|
|
$this->validateTradeName(); |
41
|
|
|
$this->validateAddress(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This validates the document |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* @throws InvalidArgumentException |
49
|
|
|
*/ |
50
|
|
|
private function validateDocument() |
51
|
|
|
{ |
52
|
|
|
$document = $this->payer->document; |
53
|
|
|
if (empty($document) || !is_string($document) || !is_numeric($document)) { |
54
|
|
|
throw new \InvalidArgumentException('payer document should be a numeric string'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This validates the payer name |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
private function validateName() |
65
|
|
|
{ |
66
|
|
|
$name = $this->payer->name; |
67
|
|
|
if (empty($name) || !is_string($name)) { |
68
|
|
|
throw new \InvalidArgumentException('payer name should be a string'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This validates the payer trade name |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @throws InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
|
private function validateTradeName() |
79
|
|
|
{ |
80
|
|
|
$tradeName = $this->payer->tradeName; |
81
|
|
|
if (empty($tradeName) || !is_string($tradeName)) { |
82
|
|
|
throw new \InvalidArgumentException('payer trade name should be a string'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* This validates a payer address |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
*/ |
92
|
|
|
private function validateAddress() |
93
|
|
|
{ |
94
|
|
|
if (!$this->payer->address instanceof Address) { |
95
|
|
|
throw new \InvalidArgumentException('payer address should be a Address type'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->payer |
99
|
|
|
->address |
100
|
|
|
->validate(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|