1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AndreDeBrito\PHPViaCep; |
4
|
|
|
|
5
|
|
|
use AndreDeBrito\PHPViaCep\ViaCepApi; |
6
|
|
|
use AndreDeBrito\PHPViaCep\Validators\EmptyValidator; |
7
|
|
|
use AndreDeBrito\PHPViaCep\Validators\LengthValidator; |
8
|
|
|
use AndreDeBrito\PHPViaCep\Exceptions\InvalidCepException; |
9
|
|
|
use AndreDeBrito\PHPViaCep\Exceptions\InvalidEstateException; |
10
|
|
|
use AndreDeBrito\PHPViaCep\Exceptions\InvalidCityException; |
11
|
|
|
use AndreDeBrito\PHPViaCep\Exceptions\InvalidAddressException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PhpViaCep |
15
|
|
|
* |
16
|
|
|
* @author André de Brito <https://github.com/andredebrito> |
17
|
|
|
* @package AndredeBrito\PHPViaCep |
18
|
|
|
*/ |
19
|
|
|
class PhpViaCep extends ViaCepApi { |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $cep; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $estate; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $city; |
29
|
|
|
|
30
|
|
|
/** @var sring */ |
|
|
|
|
31
|
|
|
private $address; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* PhpViaCep constructor |
35
|
|
|
*/ |
36
|
|
|
public function __construct() { |
37
|
|
|
parent::__construct(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* @param string $cep |
43
|
|
|
* @return \AndreDeBrito\PHPViaCep\PhpViaCep|null |
44
|
|
|
*/ |
45
|
|
|
public function findByCep(string $cep): ?PhpViaCep { |
46
|
|
|
$this->cep = trim($cep); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$this->validateCep(); |
50
|
|
|
} catch (InvalidCepException $ex) { |
51
|
|
|
$this->error = $ex->getMessage(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->setEndpoint("/{$this->cep}/"); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @param string $estate |
61
|
|
|
* @param string $city |
62
|
|
|
* @param string $address |
63
|
|
|
* @return \AndreDeBrito\PHPViaCep\PhpViaCep|null |
64
|
|
|
*/ |
65
|
|
|
public function findByAddress(string $estate, string $city, string $address): ?PhpViaCep { |
66
|
|
|
$this->estate = trim($estate); |
67
|
|
|
$this->city = trim($city); |
68
|
|
|
$this->address = trim($address); |
|
|
|
|
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$this->validateEstate(); |
72
|
|
|
} catch (InvalidEstateException $ex) { |
73
|
|
|
$this->error = $ex->getMessage(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$this->validateCity(); |
78
|
|
|
} catch (InvalidCityException $ex) { |
79
|
|
|
$this->error = $ex->getMessage(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$this->validateAddress(); |
84
|
|
|
} catch (InvalidAddressException $ex) { |
85
|
|
|
$this->error = $ex->getMessage(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->setEndpoint("/{$this->estate}/{$this->city}/{$this->address}/"); |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
* @throws InvalidCepException |
96
|
|
|
*/ |
97
|
|
|
private function validateCep(): void { |
98
|
|
|
if (!EmptyValidator::isValid($this->cep)) { |
99
|
|
|
throw new InvalidCepException("Informe o CEP!"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!is_numeric($this->cep)) { |
103
|
|
|
throw new InvalidCepException("O CEP só deve ser somente números!"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!LengthValidator::equals($this->cep, 8)) { |
107
|
|
|
throw new InvalidCepException("O CEP deve ter 8 digitos"); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
* @throws InvalidEstateException |
115
|
|
|
*/ |
116
|
|
|
private function validateEstate(): void { |
117
|
|
|
if (!EmptyValidator::isValid($this->estate)) { |
118
|
|
|
throw new InvalidEstateException("Informe o UF!"); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!LengthValidator::equals($this->estate, 2)) { |
122
|
|
|
throw new InvalidEstateException("O UF deve ter somente 2 caracteres"); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
* @throws InvalidCityException |
130
|
|
|
*/ |
131
|
|
|
private function validateCity(): void { |
132
|
|
|
if (!EmptyValidator::isValid($this->city)) { |
133
|
|
|
throw new InvalidCityException("Informe a cidade!"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!LengthValidator::aboveOrEqual($this->city, 3)) { |
137
|
|
|
throw new InvalidCityException("A cidade deve ter no mínimo 3 caracteres!"); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* @throws InvalidAddressException |
144
|
|
|
*/ |
145
|
|
|
private function validateAddress() { |
146
|
|
|
if (!EmptyValidator::isValid($this->address)) { |
147
|
|
|
throw new InvalidAddressException("Informe o logradouro!"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!LengthValidator::aboveOrEqual($this->address, 3)) { |
151
|
|
|
throw new InvalidAddressException("O logradouro deve ter no mínimo 3 caracteres!"); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths