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