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
|
|
|
* AddressValidator 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 AddressValidator |
19
|
|
|
{ |
20
|
|
|
/** @var Address */ |
21
|
|
|
private $address; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Address $address |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Address $address) |
27
|
|
|
{ |
28
|
|
|
$this->address = $address; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Validate the attributes of the address class |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function validate(): void |
37
|
|
|
{ |
38
|
|
|
$this->validateAddressLine(); |
39
|
|
|
$this->validateCity(); |
40
|
|
|
$this->validateState(); |
41
|
|
|
$this->validateZipCode(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This validates the address line |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* @throws InvalidArgumentException |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
private function validateAddressLine() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$addressLine = $this->address->addressLine; |
53
|
|
|
if (empty($addressLine) || !is_string($addressLine)) { |
54
|
|
|
throw new \InvalidArgumentException('address line should be a string'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This validates the city address |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
private function validateCity() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$city = $this->address->city; |
67
|
|
|
if (empty($city) || !is_string($city)) { |
68
|
|
|
throw new \InvalidArgumentException('city address should be a string'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This validates the state address |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @throws InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
private function validateState() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$state = $this->address->state; |
81
|
|
|
if (empty($state) || !is_string($state)) { |
82
|
|
|
throw new \InvalidArgumentException('state address should be a string'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* This validates a zip code |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
private function validateZipCode() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$zipCode = $this->address->zipCode; |
95
|
|
|
if (empty($zipCode) || !is_string($zipCode)) { |
96
|
|
|
throw new \InvalidArgumentException('zip code should be a string'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.