Skip to content

Commit

Permalink
Merge pull request #4 from prhost/master
Browse files Browse the repository at this point in the history
fix provider Correios. Obrigado https://github.com/jansenfelipe/cep-g…
  • Loading branch information
prhost authored May 6, 2020
2 parents aae0f75 + 3e645c5 commit ff7e064
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions src/Providers/CorreiosProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,47 @@ public function getAddress($cep, HttpClientContract $client, array $option = [])

$message = $crawler->filter('div.ctrlcontent p')->html();

if ($message == 'DADOS ENCONTRADOS COM SUCESSO.') {
$tr = $crawler->filter('table.tmptabela');
if (!is_null($response)) {
$crawler = new Crawler($response);

$params['zipcode'] = $cep;
$params['street'] = $tr->filter('tr:nth-child(1) td:nth-child(2)')->html();
$params['neighborhood'] = $tr->filter('tr:nth-child(2) td:nth-child(2)')->html();
$message = $crawler->filter('div.ctrlcontent p')->html();

$aux = explode('/', $tr->filter('tr:nth-child(3) td:nth-child(2)')->html());
$params['city'] = $aux[0];
$params['state'] = $aux[1];
if ($message == 'DADOS ENCONTRADOS COM SUCESSO.') {
$tr = $crawler->filter('table.tmptabela');

$aux = explode(' - ', $params['street']);
$params['street'] = (count($aux) == 2) ? $aux[0] : $params['street'];
$params['zipcode'] = $cep;
$params['street'] = '';
$params['neighborhood'] = '';
$params['city'] = '';
$params['state'] = '';

return Address::create(array_map(function ($item) {
return urldecode(str_replace('%C2%A0', '', urlencode($item)));
}, $params));
for ($i = 1; $i <= 3; $i++) {
if ($tr->filter('tr:nth-child('.$i.') th:nth-child(1)')->count() <= 0) {
break;
}

$index = trim($tr->filter('tr:nth-child('.$i.') th:nth-child(1)')->text(), ':');
$value = $tr->filter('tr:nth-child('.$i.') td:nth-child(2)')->text();
switch ($index) {
case 'Logradouro':
$aux = explode(' - ', $value);
$params['street'] = (count($aux) == 2) ? $aux[0] : $value;
break;
case 'Bairro':
$params['neighborhood'] = $value;
break;
case 'Localidade/UF':
$aux = explode('/', $value);
$params['city'] = $aux[0];
$params['state'] = $aux[1];
break;
}
}

return Address::create(array_map(function ($item) {
return urldecode(str_replace('%C2%A0', '', urlencode($item)));
}, $params));
}
}

} catch (\Exception $exception) {
Expand Down

0 comments on commit ff7e064

Please sign in to comment.