-
Notifications
You must be signed in to change notification settings - Fork 12
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 #7 from flyingluscas/development
v2.0.0
- Loading branch information
Showing
6 changed files
with
188 additions
and
172 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
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,79 @@ | ||
<?php | ||
|
||
namespace FlyingLuscas\ViaCEP; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\ClientInterface; | ||
|
||
class ViaCEP | ||
{ | ||
/** | ||
* HTTP client. | ||
* | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
protected $http; | ||
|
||
/** | ||
* Creta a new ZipCode class instance. | ||
* | ||
* @param \GuzzleHttp\ClientInterface $http | ||
*/ | ||
public function __construct(ClientInterface $http = null) | ||
{ | ||
$this->http = $http ?: new Client; | ||
} | ||
|
||
/** | ||
* Find address by zip code (CEP). | ||
* | ||
* @param string $zipCode | ||
* | ||
* @return \FlyingLuscas\ViaCEP\Address | ||
*/ | ||
public function findByZipCode($zipCode) | ||
{ | ||
$url = sprintf('https://viacep.com.br/ws/%s/json', $zipCode); | ||
|
||
$response = $this->http->request('GET', $url); | ||
|
||
$attributes = json_decode($response->getBody(), true); | ||
|
||
if (array_key_exists('erro', $attributes) && $attributes['erro'] === true) { | ||
return new Address; | ||
} | ||
|
||
return new Address($attributes); | ||
} | ||
|
||
/** | ||
* Find addresses by state, city and street name. | ||
* | ||
* @param string $state | ||
* @param string $city | ||
* @param string $street | ||
* | ||
* @return \FlyingLuscas\ViaCEP\Address[] | ||
*/ | ||
public function findByStreetName($state, $city, $street) | ||
{ | ||
$url = sprintf( | ||
'https://viacep.com.br/ws/%s/%s/%s/json', | ||
rawurlencode($state), | ||
rawurlencode($city), | ||
rawurlencode($street) | ||
); | ||
|
||
$response = $this->http->request('GET', $url); | ||
|
||
$results = json_decode($response->getBody(), true); | ||
|
||
if (array_key_exists('erro', $results) && $results['erro'] === true) { | ||
return []; | ||
} | ||
|
||
return array_map(function ($attributes) { | ||
return new Address($attributes); | ||
}, $results); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
<?php | ||
|
||
namespace FlyingLuscas\ViaCEP; | ||
|
||
class ViaCEPTest extends TestCase | ||
{ | ||
public function testGetEmptyAddressInCaseOfErrors() | ||
{ | ||
$address = (new ViaCEP)->findByZipCode('99999-999'); | ||
|
||
$this->assertInstanceOf(Address::class, $address); | ||
|
||
array_map(function ($value) { | ||
$this->assertTrue(empty($value)); | ||
}, get_object_vars($address)); | ||
} | ||
|
||
public function testFindAddressByZipCode() | ||
{ | ||
$zipcode = '01001-000'; | ||
$address = (new ViaCEP)->findByZipCode($zipcode); | ||
|
||
$this->assertInstanceOf(Address::class, $address); | ||
$this->assertEquals($zipcode, $address->zipCode); | ||
} | ||
|
||
public function testGetEmptyListOfAddressesInCaseOfErrors() | ||
{ | ||
$state = 'PR'; | ||
$city = 'Maringá'; | ||
$street = 'Bender'; | ||
|
||
$addresses = (new ViaCEP)->findByStreetName($state, $city, $street); | ||
|
||
$this->assertCount(0, $addresses); | ||
} | ||
|
||
public function testFindAddressByStreetName() | ||
{ | ||
$state = 'SP'; | ||
$city = 'São Paulo'; | ||
$street = 'Vapabussu'; | ||
|
||
$addresses = (new ViaCEP)->findByStreetName($state, $city, $street); | ||
|
||
$this->assertCount(2, $addresses); | ||
|
||
array_map(function ($address) { | ||
$this->assertInstanceOf(Address::class, $address); | ||
$this->assertEquals('Rua Vapabussu', $address->street); | ||
}, $addresses); | ||
} | ||
} |
Oops, something went wrong.