Skip to content

Commit

Permalink
Merge pull request #7 from flyingluscas/development
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
flyingluscas authored Aug 25, 2019
2 parents 54b66cf + cc28a03 commit 3ff964c
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 172 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All Notable changes to `viacep-php` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## v2.0.0 - 2019-08-24

### Changed
- Class `ZipCode` renamed to `ViaCEP`.
- Method `findByCep` renamed to `findByZipCode`.
- Method `findByAddress` renamed to `findByStreetName`.

### Removed
- Method `find` (deprecated) removed.

## v1.1.0 - 2019-08-24

### Added
Expand Down
60 changes: 46 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ $ composer require flyingluscas/viacep-php

## Usage

``` php
use FlyingLuscas\ViaCEP\ZipCode;
### findByZipCode

$zipcode = new ZipCode;
Find address by zip code.

```
```php
use FlyingLuscas\ViaCEP\ViaCEP;

### Array
$viacep = new ViaCEP;

``` php
$address = $zipcode->findByCep('01001-000')->toArray();
$address = $viacep->findByZipCode('01001-000')->toArray();

/*
The returned result would be something like this:
Should return something like this:

[
'zipCode' => '01001-000',
Expand All @@ -43,15 +42,11 @@ The returned result would be something like this:
'ibge' => '3550308',
]
*/
```

### JSON

``` php
$address = $zipcode->findByCep('01001-000')->toJson();
$address = $viacep->findByZipCode('01001-000')->toJson();

/*
The returned result would be something like this:
Should return something like this:

{
"zipCode": "01001-000",
Expand All @@ -65,6 +60,43 @@ The returned result would be something like this:
*/
```

### findByStreetName

Search for addresses using state, city and a street name.

```php
use FlyingLuscas\ViaCEP\ViaCEP;

$viacep = new ViaCEP;

$addresses = $viacep->findByStreetName('SP', 'São Paulo', 'Gomes de Carvalho');

/*
Should return something like this:

[
[
'zipCode' => '01001-000',
'street' => 'Praça da Sé',
'complement' => 'lado ímpar',
'neighborhood' => 'Sé',
'city' => 'São Paulo',
'state' => 'SP',
'ibge' => '3550308',
],
[
'zipCode' => '01001-000',
'street' => 'Praça da Sé',
'complement' => 'lado ímpar',
'neighborhood' => 'Sé',
'city' => 'São Paulo',
'state' => 'SP',
'ibge' => '3550308',
]
]
*/
```

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
79 changes: 79 additions & 0 deletions src/ViaCEP.php
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);
}
}
115 changes: 0 additions & 115 deletions src/ZipCode.php

This file was deleted.

53 changes: 53 additions & 0 deletions tests/ViaCEPTest.php
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);
}
}
Loading

0 comments on commit 3ff964c

Please sign in to comment.