-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ThiagoCrepequer/dev
feat: refactored CEP and created more functions
- Loading branch information
Showing
5 changed files
with
187 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Crepequer\PhpBrasilUtils; | ||
|
||
use Crepequer\PhpBrasilUtils\Helpers\Temp; | ||
use DateTime; | ||
use Exception; | ||
|
||
final class Address | ||
{ | ||
public function __construct( | ||
private bool $saveTemp = false, | ||
private DateTime $validity = new DateTime('+30 days') | ||
) | ||
{} | ||
/** | ||
* This method is responsible for getting the address from the CEP using the ViaCEP API | ||
* | ||
* @param string $cep | ||
* @param bool $error - If true, it will throw an exception if the CEP is invalid | ||
* @param bool $saveTemp - If true, it will save the address in a temporary file | ||
* | ||
* @example getAddresses("12345-678"); | ||
* @example getAddresses("98765432"); | ||
* | ||
* @return array|bool | ||
* | ||
* @author Thiago Crepequer | ||
*/ | ||
public function searchByCep(string $cep, bool|null $error = false): array|bool | ||
{ | ||
if (!CEP::validateFormatting($cep, $error)) { | ||
return false; | ||
} | ||
|
||
if ($this->saveTemp) { | ||
$temp = Temp::get($cep, "ceps", $this->validity); | ||
if ($temp) { | ||
return $temp; | ||
} | ||
} | ||
|
||
$filtredCep = CEP::removeMask($cep); | ||
$url = "https://viacep.com.br/ws/{$filtredCep}/json/"; | ||
|
||
$curl = curl_init($url); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
|
||
$json = json_decode($response, true); | ||
|
||
if (empty($json) || (isset($json["erro"]) && $json["erro"] === true)) { | ||
if ($error) { | ||
throw new Exception("No address found for the CEP: {$cep}"); | ||
} | ||
return false; | ||
} | ||
|
||
if ($this->saveTemp) { | ||
Temp::set($response, $cep, "ceps"); | ||
} | ||
|
||
return $json; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Crepequer\PhpBrasilUtils\Tests; | ||
|
||
use Crepequer\PhpBrasilUtils\Address; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AddressTest extends TestCase | ||
{ | ||
|
||
public function testGetAddresses(): void | ||
{ | ||
$this->setName("Get address with valid CEP"); | ||
|
||
$addresses = [ | ||
'01001-000', | ||
'70070-600', | ||
'20010000', | ||
'60175045', | ||
'04094050' | ||
]; | ||
|
||
foreach ($addresses as $cep) { | ||
$returnedAdres = (new Address(saveTemp: true))->searchByCep($cep); | ||
$this->assertIsArray($returnedAdres); | ||
$this->assertArrayHasKey("cep", $returnedAdres); | ||
$this->assertArrayHasKey("logradouro", $returnedAdres); | ||
$this->assertArrayHasKey("complemento", $returnedAdres); | ||
$this->assertArrayHasKey("bairro", $returnedAdres); | ||
$this->assertArrayHasKey("localidade", $returnedAdres); | ||
$this->assertArrayHasKey("uf", $returnedAdres); | ||
$this->assertArrayHasKey("ibge", $returnedAdres); | ||
} | ||
} | ||
|
||
public function testGetAdressWithInvalidCEP(): void | ||
{ | ||
$this->setName("Get address with invalid CEP but formatted correctly"); | ||
$res = (new Address())->searchByCep("12345678"); | ||
$this->assertFalse($res); | ||
|
||
$this->setName("Get address with invalid CEP and not formatted correctly"); | ||
$res = (new Address())->searchByCep("123456789"); | ||
$this->assertFalse($res); | ||
} | ||
|
||
public function testGetAdressWithInvalidCEPAndErrorTrue(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
(new Address())->searchByCep("123456789", true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters