|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Classe principal para as buscas de CEP |
|
5
|
|
|
* |
|
6
|
|
|
* Recebe o cep a ser procurado e randomicamente consulta os serviços |
|
7
|
|
|
* online, retorna o resultado do primeiro que encontrar |
|
8
|
|
|
* |
|
9
|
|
|
* @author Adriano Maciel <[email protected]> |
|
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT Licence |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Wead\ZipCode; |
|
14
|
|
|
|
|
15
|
|
|
use Wead\ZipCode\Exceptions\ZipCodeNotFoundException; |
|
16
|
|
|
use Wead\ZipCode\WS\ApiCEP; |
|
17
|
|
|
use Wead\ZipCode\WS\ViaCep; |
|
18
|
|
|
use Wead\ZipCode\WS\WebMania; |
|
19
|
|
|
|
|
20
|
|
|
class Search |
|
21
|
|
|
{ |
|
22
|
|
|
private $listApi = [ |
|
23
|
|
|
'ViaCep', |
|
24
|
|
|
'WebMania', |
|
25
|
|
|
'ApiCep', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
private $credential = [ |
|
29
|
|
|
'webMania' => [ |
|
30
|
|
|
'apiKey' => null, |
|
31
|
|
|
'apiSecret' => null |
|
32
|
|
|
] |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
private $maxAttempts = 5; |
|
36
|
|
|
private $countAttempts = 0; |
|
37
|
|
|
|
|
38
|
|
|
public function setMaxAttempts($maxAttempts = 5) |
|
39
|
|
|
{ |
|
40
|
2 |
|
return $this->maxAttempts = (int) $maxAttempts; |
|
41
|
|
|
} |
|
42
|
2 |
|
|
|
43
|
|
|
public function setCredential($service, $credential = []) |
|
44
|
|
|
{ |
|
45
|
2 |
|
if (is_string($service) && is_array($credential)) { |
|
46
|
|
|
$this->credential[$service] = $credential; |
|
47
|
2 |
|
} |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function getAddressFromZipcode($zipCode) |
|
51
|
|
|
{ |
|
52
|
1 |
|
try { |
|
53
|
|
|
$found = $this->attemptSearch($zipCode); |
|
54
|
|
|
|
|
55
|
1 |
|
if (!$found['city']) { |
|
56
|
|
|
$this->countAttempts++; |
|
57
|
1 |
|
|
|
58
|
|
|
return $this->getAddressFromZipcode($zipCode); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$found['address'] = strlen(trim($found['address'])) == 0 ? "Não encontrado" : $found['address']; |
|
62
|
|
|
$found['district'] = strlen(trim($found['district'])) == 0 ? "Não encontrado" : $found['district']; |
|
63
|
1 |
|
|
|
64
|
1 |
|
return $found; |
|
65
|
|
|
} catch (\Exception $e) { |
|
66
|
1 |
|
if ($this->countAttempts < $this->maxAttempts) { |
|
67
|
|
|
$this->countAttempts++; |
|
68
|
|
|
|
|
69
|
|
|
return $this->getAddressFromZipcode($zipCode); |
|
70
|
|
|
} else { |
|
71
|
|
|
throw new \Exception("Not Found data for this cep"); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function attemptSearch($zipCode) |
|
77
|
|
|
{ |
|
78
|
3 |
|
$found = false; |
|
79
|
|
|
|
|
80
|
3 |
|
shuffle($this->listApi); |
|
81
|
|
|
|
|
82
|
3 |
|
foreach ($this->listApi as $api) { |
|
83
|
|
|
if (!$found) { |
|
84
|
3 |
|
switch ($api) { |
|
85
|
3 |
|
case 'ViaCep': |
|
86
|
3 |
|
$found = $this->getFromViaCep($zipCode); |
|
87
|
3 |
|
break; |
|
88
|
2 |
|
|
|
89
|
2 |
|
case 'ApiCep': |
|
90
|
|
|
$found = $this->getFromApiCep($zipCode); |
|
91
|
2 |
|
break; |
|
92
|
1 |
|
|
|
93
|
1 |
|
case 'WebMania': |
|
94
|
|
|
$found = $this->getFromWebMania($zipCode); |
|
95
|
2 |
|
break; |
|
96
|
1 |
|
} |
|
97
|
1 |
|
|
|
98
|
|
|
if (!isset($found['city'])) { |
|
99
|
2 |
|
$found = false; |
|
100
|
2 |
|
} elseif (!$found['status']) { |
|
101
|
2 |
|
$found = false; |
|
102
|
|
|
} elseif (!$found['city']) { |
|
103
|
|
|
$found = false; |
|
104
|
3 |
|
} elseif (strlen(trim($found['city'])) == 0) { |
|
105
|
3 |
|
$found = false; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
if (!$found) { |
|
111
|
1 |
|
throw new ZipCodeNotFoundException("Error to get address by zipcode: {$zipCode}"); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
return $found; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
3 |
|
private function getFromViaCep($zipCode) |
|
118
|
|
|
{ |
|
119
|
3 |
|
$zip = new ViaCep(); |
|
120
|
3 |
|
$zip = $zip->getAddressFromZipcode($zipCode); |
|
121
|
|
|
|
|
122
|
3 |
|
return $zip; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
private function getFromApiCep($zipCode) |
|
126
|
|
|
{ |
|
127
|
2 |
|
$zip = new ApiCEP(); |
|
128
|
2 |
|
$zip = $zip->getAddressFromZipcode($zipCode); |
|
129
|
|
|
|
|
130
|
2 |
|
return $zip; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
private function getFromWebMania($zipCode, $runningTest = false) |
|
134
|
|
|
{ |
|
135
|
2 |
|
$mania = isset($this->credential['webMania']) ? $this->credential['webMania'] : []; |
|
136
|
2 |
|
|
|
137
|
|
|
$zip = new WebMania($mania); |
|
138
|
2 |
|
$zip = $zip->getAddressFromZipcode($zipCode, $runningTest); |
|
139
|
|
|
|
|
140
|
|
|
return $zip; |
|
141
|
3 |
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|