1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WeDevBr\Bankly\Validators\Pix; |
4
|
|
|
|
5
|
|
|
use WeDevBr\Bankly\Types\Pix\Bank; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* BankValidator class |
9
|
|
|
* |
10
|
|
|
* PHP version 7.3|7.4|8.0 |
11
|
|
|
* |
12
|
|
|
* @author WeDev Brasil Team <[email protected]> |
13
|
|
|
* @author Rafael Teixeira <[email protected]> |
14
|
|
|
* @copyright 2020 We Dev Tecnologia Ltda |
15
|
|
|
* @link https://github.com/wedevBr/bankly-laravel/ |
16
|
|
|
*/ |
17
|
|
|
class BankValidator |
18
|
|
|
{ |
19
|
|
|
/** @var Bank */ |
20
|
|
|
private $bank; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Bank $bank |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Bank $bank) |
26
|
|
|
{ |
27
|
|
|
$this->bank = $bank; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Validate the attributes of the bank class |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function validate(): void |
36
|
|
|
{ |
37
|
|
|
$this->validateIspb(); |
38
|
|
|
$this->validateCompe(); |
39
|
|
|
$this->validateName(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This validates a bank ispb |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
* @throws InvalidArgumentException |
47
|
|
|
*/ |
48
|
|
|
private function validateIspb() |
49
|
|
|
{ |
50
|
|
|
$ispb = $this->bank->ispb; |
51
|
|
|
if (empty($ispb) || !is_string($ispb) || !is_numeric($ispb)) { |
52
|
|
|
throw new \InvalidArgumentException('bank ispb should be a numeric string'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This validates a bank compe |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
* @throws InvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
private function validateCompe() |
63
|
|
|
{ |
64
|
|
|
$compe = $this->bank->compe; |
65
|
|
|
if (empty($compe) || !is_string($compe) || !is_numeric($compe)) { |
66
|
|
|
throw new \InvalidArgumentException('bank compe account should be a numeric string'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* This validates a bank name |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
private function validateName() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$name = $this->bank->name; |
79
|
|
|
if (empty($name) || !is_string($name)) { |
80
|
|
|
throw new \InvalidArgumentException('bank name should be a string'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.