1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AndreDeBrito\PHPViaCep; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ViaCepApi |
7
|
|
|
* |
8
|
|
|
* @author André de Brito <https://github.com/andredebrito> |
9
|
|
|
* @package AndredeBrito\PHPViaCep |
10
|
|
|
*/ |
11
|
|
|
abstract class ViaCepApi { |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $apiUrl; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $endpoint; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $method; |
21
|
|
|
|
22
|
|
|
/** @var mixed */ |
23
|
|
|
protected $response; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $responseType; |
27
|
|
|
|
28
|
|
|
/** @var sring */ |
|
|
|
|
29
|
|
|
protected $error; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ViaCepAPi constructor |
33
|
|
|
*/ |
34
|
|
|
public function __construct() { |
35
|
|
|
$this->apiUrl = "https://viacep.com.br/ws"; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set endpoint |
40
|
|
|
* |
41
|
|
|
* @param string $endpoint |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function setEndpoint(string $endpoint): void { |
45
|
|
|
$this->endpoint = $endpoint; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return response |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function getResponse() { |
54
|
|
|
return $this->response = $this->checkNullResponse(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getError(): ?string { |
62
|
|
|
return $this->error; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set API return type to JSON |
67
|
|
|
* |
68
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
69
|
|
|
*/ |
70
|
|
|
public function json(): ?ViaCepApi { |
71
|
|
|
$this->endpoint .= "json/"; |
72
|
|
|
$this->responseType = "json"; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set API return type do JSON Unicode |
78
|
|
|
* |
79
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
80
|
|
|
*/ |
81
|
|
|
public function jsonUnicode(): ?ViaCepApi { |
82
|
|
|
$this->endpoint .= "json/unicode/"; |
83
|
|
|
$this->responseType = "json"; |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set API return type to XML |
89
|
|
|
* |
90
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
91
|
|
|
*/ |
92
|
|
|
public function xml(): ?ViaCepApi { |
93
|
|
|
$this->endpoint .= "xml/"; |
94
|
|
|
$this->responseType = "xml"; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set API return type to PIPED |
100
|
|
|
* |
101
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
102
|
|
|
*/ |
103
|
|
|
public function piped(): ?ViaCepApi { |
104
|
|
|
$this->endpoint .= "piped/"; |
105
|
|
|
$this->responseType = "pided"; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set API return type to QUERTY |
111
|
|
|
* |
112
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
113
|
|
|
*/ |
114
|
|
|
public function querty(): ?ViaCepApi { |
115
|
|
|
$this->endpoint .= "querty/"; |
116
|
|
|
$this->responseType = "querty"; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* <b>API JSON return only</b> |
122
|
|
|
* Set JSON response to Object |
123
|
|
|
* |
124
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
125
|
|
|
*/ |
126
|
|
|
public function jsonToObject(): ?ViaCepApi { |
127
|
|
|
if($this->responseType == "json"){ |
128
|
|
|
$this->responseType = "object"; |
129
|
|
|
$this->response = (object) json_decode($this->response); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Fetch the request |
137
|
|
|
* |
138
|
|
|
* @return \AndreDeBrito\PHPViaCep\ViaCepApi|null |
139
|
|
|
*/ |
140
|
|
|
public function fetch(): ?ViaCepApi { |
141
|
|
|
if (!$this->error) { |
142
|
|
|
$this->request("GET", $this->endpoint); |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Call the request |
151
|
|
|
* |
152
|
|
|
* @param string $method |
153
|
|
|
* @param string $endpoint |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
protected function request(string $method, string $endpoint): void { |
157
|
|
|
$this->method = $method; |
158
|
|
|
$this->endpoint = $endpoint; |
159
|
|
|
|
160
|
|
|
$this->dispatch(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Execute the request via CURL |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
private function dispatch(): void { |
168
|
|
|
$curl = curl_init(); |
169
|
|
|
|
170
|
|
|
curl_setopt_array($curl, array( |
|
|
|
|
171
|
|
|
CURLOPT_URL => "{$this->apiUrl}/{$this->endpoint}", |
172
|
|
|
CURLOPT_RETURNTRANSFER => true, |
173
|
|
|
CURLOPT_MAXREDIRS => 10, |
174
|
|
|
CURLOPT_TIMEOUT => 30, |
175
|
|
|
CURLOPT_HTTP_VERSION => "CURL_HTTP_VERSION_1_1", |
176
|
|
|
CURLOPT_CUSTOMREQUEST => $this->method |
177
|
|
|
)); |
178
|
|
|
|
179
|
|
|
$this->response = curl_exec($curl); |
|
|
|
|
180
|
|
|
curl_close($curl); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function checkNullResponse() { |
184
|
|
|
switch ($this->responseType) { |
185
|
|
|
case "object": |
186
|
|
|
return (!empty($this->response->erro) ? null : $this->response); |
187
|
|
|
|
188
|
|
|
case "json": |
189
|
|
|
return (in_array("erro", json_decode($this->response)) ? null : $this->response); |
190
|
|
|
|
191
|
|
|
case "xml": |
192
|
|
|
return (simplexml_load_string($this->response)->erro ? null : $this->response); |
193
|
|
|
|
194
|
|
|
case "pided": |
195
|
|
|
return ($this->response == "erro:true" ? null : $this->response); |
196
|
|
|
|
197
|
|
|
case "querty": |
198
|
|
|
return ($this->response == "erro=true" ? null : $this->response); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
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