Skip to content

Commit f51ade2

Browse files
authored
Merge pull request #1 from prhost/master
merge master to 5.0
2 parents c9a6dcc + 5daf835 commit f51ade2

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ Um exemplo passando opções como o token do CEP Aberto
3939
```php
4040
use Prhost\CepGratis\CepGratis;
4141

42-
$address = CepGratis::search('31030080', ['token' => '123abc']);
42+
$cep = '31030080';
43+
$options = ['token' => '123abc'];
44+
$timeout = 15; //segundos
45+
46+
$address = CepGratis::search($cep, $options, $timeout);
4347
```
4448

4549
Outras formas:
@@ -49,7 +53,7 @@ use Prhost\CepGratis\CepGratis;
4953
use Prhost\CepGratis\Providers\CepAbertoProvider;
5054

5155
$cepGratis = new CepGratis();
52-
$cepGratis->setOptions(['token' => 'f944751e6dd14d7a40bf18d4d8df1741']);
56+
$cepGratis->setOptions(['token' => '123abc']);
5357
$cepGratis->addProvider(new CepAbertoProvider());
5458
$cepGratis->setTimeout(15);
5559
$address = $cepGratis->resolve('31030080');

src/CepGratis.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CepGratis
2929
private $providers = [];
3030

3131
/**
32-
* @var int
32+
* @var int in seconds
3333
*/
3434
private $timeout = 5;
3535

@@ -51,14 +51,16 @@ public function __construct()
5151
*
5252
* @param string $cep CEP
5353
* @param array $options
54+
* @param int $timeout in seconds (optional)
5455
* @return Address
5556
* @throws CepGratisInvalidParameterException
5657
* @throws CepGratisTimeoutException
5758
*/
58-
public static function search(string $cep, array $options = [])
59+
public static function search(string $cep, array $options = [], int $timeout = null)
5960
{
6061
$cepGratis = new self();
6162
$cepGratis->options = $options;
63+
$cepGratis->timeout = $timeout ? $timeout : $cepGratis->timeout;
6264

6365
$cepGratis->addProvider(new ViaCepProvider());
6466
$cepGratis->addProvider(new CorreiosProvider());
@@ -81,6 +83,8 @@ public static function search(string $cep, array $options = [])
8183
*/
8284
public function resolve($cep)
8385
{
86+
$cep = $this->clearCep($cep);
87+
8488
if (strlen($cep) != 8 && filter_var($cep, FILTER_VALIDATE_INT) === false) {
8589
throw new CepGratisInvalidParameterException('CEP is invalid');
8690
}
@@ -97,6 +101,13 @@ public function resolve($cep)
97101
do {
98102
foreach ($this->providers as $provider) {
99103
$address = $provider->getAddress($cep, $this->client, $this->options);
104+
if (!is_null($address)) {
105+
break;
106+
}
107+
}
108+
109+
if (!is_null($address)) {
110+
break;
100111
}
101112

102113
if ((time() - $time) >= $this->timeout) {
@@ -147,4 +158,15 @@ public function setOptions(array $options): void
147158
{
148159
$this->options = $options;
149160
}
161+
162+
/**
163+
* Limpa os caracteres especiais do CEP
164+
*
165+
* @param string $cep
166+
* @return string
167+
*/
168+
protected function clearCep(string $cep): string
169+
{
170+
return preg_replace('#[^0-9]#', '', $cep);
171+
}
150172
}

0 commit comments

Comments
 (0)