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\CepLa; |
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
|
|
|
'WideNet', |
26
|
|
|
'CepLa' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
private $credential = [ |
30
|
|
|
'webMania' => [ |
31
|
|
|
'apiKey' => null, |
32
|
|
|
'apiSecret' => null |
33
|
|
|
] |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private $maxAttempts = 5; |
37
|
|
|
private $countAttempts = 0; |
38
|
|
|
|
39
|
|
|
public function setMaxAttempts($maxAttempts = 5) |
40
|
2 |
|
{ |
41
|
|
|
return $this->maxAttempts = (int) $maxAttempts; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
public function setCredential($service, $credential = []) |
45
|
2 |
|
{ |
46
|
|
|
if (is_string($service) && is_array($credential)) { |
47
|
2 |
|
$this->credential[$service] = $credential; |
48
|
2 |
|
} |
49
|
|
|
} |
50
|
2 |
|
|
51
|
|
|
public function getAddressFromZipcode($zipCode) |
52
|
1 |
|
{ |
53
|
|
|
try { |
54
|
|
|
$found = $this->attemptSearch($zipCode); |
55
|
1 |
|
|
56
|
|
|
if (!$found['city']) { |
57
|
1 |
|
$this->countAttempts++; |
58
|
|
|
|
59
|
|
|
return $this->getAddressFromZipcode($zipCode); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$found['address'] = strlen(trim($found['address'])) == 0 ? "Não encontrado" : $found['address']; |
63
|
1 |
|
$found['district'] = strlen(trim($found['district'])) == 0 ? "Não encontrado" : $found['district']; |
64
|
1 |
|
|
65
|
|
|
return $found; |
66
|
1 |
|
} catch (\Exception $e) { |
67
|
|
|
if ($this->countAttempts < $this->maxAttempts) { |
68
|
|
|
$this->countAttempts++; |
69
|
|
|
|
70
|
|
|
return $this->getAddressFromZipcode($zipCode); |
71
|
|
|
} else { |
72
|
|
|
throw new \Exception("Not Found data for this cep"); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function attemptSearch($zipCode) |
78
|
3 |
|
{ |
79
|
|
|
$found = false; |
80
|
3 |
|
|
81
|
|
|
shuffle($this->listApi); |
82
|
3 |
|
|
83
|
|
|
foreach ($this->listApi as $api) { |
84
|
3 |
|
if (!$found) { |
85
|
3 |
|
switch ($api) { |
86
|
3 |
|
case 'ViaCep': |
87
|
3 |
|
$found = $this->getFromViaCep($zipCode); |
88
|
2 |
|
break; |
89
|
2 |
|
|
90
|
|
|
case 'WebMania': |
91
|
2 |
|
$found = $this->getFromWebMania($zipCode); |
92
|
1 |
|
break; |
93
|
1 |
|
|
94
|
|
|
case 'CepLa': |
95
|
2 |
|
$found = $this->getFromCepLa($zipCode); |
96
|
1 |
|
break; |
97
|
1 |
|
} |
98
|
|
|
|
99
|
2 |
|
if (!isset($found['city']) || !$found['status'] || !$found['city'] || strlen(trim($found['city'])) == 0) { |
100
|
2 |
|
$found = false; |
101
|
2 |
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
3 |
|
|
105
|
3 |
|
if (!$found) { |
106
|
|
|
throw new ZipCodeNotFoundException("Error to get address by zipcode: {$zipCode}"); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $found; |
110
|
3 |
|
} |
111
|
1 |
|
|
112
|
|
|
private function getFromViaCep($zipCode) |
113
|
|
|
{ |
114
|
2 |
|
$zip = new ViaCep(); |
115
|
|
|
$zip = $zip->getAddressFromZipcode($zipCode); |
116
|
|
|
|
117
|
3 |
|
return $zip; |
118
|
|
|
} |
119
|
3 |
|
|
120
|
3 |
|
private function getFromWebMania($zipCode, $runningTest = false) |
121
|
|
|
{ |
122
|
3 |
|
$zip = new WebMania(isset($this->credential['webMania']) ? $this->credential['webMania'] : []); |
123
|
|
|
$zip = $zip->getAddressFromZipcode($zipCode, $runningTest); |
124
|
|
|
|
125
|
2 |
|
return $zip; |
126
|
|
|
} |
127
|
2 |
|
|
128
|
2 |
|
private function getFromCepLa($zipCode) |
129
|
|
|
{ |
130
|
2 |
|
$zip = new CepLa(); |
131
|
|
|
$zip = $zip->getAddressFromZipcode($zipCode); |
132
|
|
|
|
133
|
2 |
|
return $zip; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|