canducci/zipcode

v1.1 2016-02-15 18:57 UTC

README

Web Service provided by http://viacep.com.br/

Canducci ZipCode

Build Status Packagist Packagist Packagist

Demo

Demo Canducci ZipCode

Quick start

Required setup

In the require key of composer.json file add the following

"canducci/zipcode": "1.*"

Run the Composer update comand

$ composer update

In your config/app.php add 'Canducci\ZipCode\Providers\ZipCodeServiceProvider' and 'Canducci\ZipCode\Providers\ZipCodeAddressServiceProvider' to the end of the providers array:

'providers' => array(
    ...,
    Canducci\ZipCode\Providers\ZipCodeServiceProvider::class,
    Canducci\ZipCode\Providers\ZipCodeAddressServiceProvider::class,

),

At the end of config/app.php add 'ZipCode' => 'Canducci\ZipCode\Facade\ZipCode' and add 'Address' => 'Canducci\ZipCode\Facades\ZipCodeAddress' to the aliases array:


'aliases' => array(
    ...,
    'ZipCode'   => Canducci\ZipCode\Facades\ZipCode::class,
    'Address'   => Canducci\ZipCode\Facades\ZipCodeAddress::class,

),

##How to Use

To use is very simple, pass the ZIP and calls the various types of returns, like this:

Package ZipCode

##Facade

Add namespace:

use Canducci\ZipCode\Facades\ZipCode;

Code Example

$zipCodeInfo = ZipCode::find('01414-001');

##Helper

$zipCodeInfo = zipcode('01414000');

##Injection Add Namespace

use Canducci\ZipCode\Contracts\ZipCodeContract;

Code Example

public function index(ZipCodeContract $zipcode)
{

      $zipCodeInfo = $zipcode->find('01414000');
      

##Traits Add Namespace

use Canducci\ZipCode\ZipCodeTrait;

Code Example


class WelcomeController extends Controller {

	use ZipCodeTrait;
	
	public function index()
	{
      		$zipCodeInfo =	$this->zipcode('01414000');
      	
      		

Summary of How to Use

Code


$zipCodeInfo = ZipCode::find('01414000', false); //Facade

$zipCodeInfo = $zipcode->find('01414000', false); //Contracts

$zipCodeInfo = zipcode('01414000', false); // Helper

$zipCodeInfo = $this->zipcode('01414000', true); //Traist

Return

The return can be null or class instance ZipCodeInfo (Canducci\ZipCode\ZipCodeInfo)

Methods ZipCodeInfo:

  • Json => getJson()

if ($zipCodeInfo) 
{

    $zipCodeInfo->getJson();
    
    {
        "cep": "01414-001",
        "logradouro": "Rua Haddock Lobo",
        "bairro": "Cerqueira César",
        "localidade": "São Paulo",
        "uf": "SP",
        "ibge": "3550308", 
        "complemento": ""
		"gia": 1004
    }
    
}
  • Array => getArray()

if ($zipCodeInfo) 
{

    $zipCodeInfo->getArray();
    
    Array
    (
        [cep] => 01414-001
        [logradouro] => Rua Haddock Lobo
        [bairro] => Cerqueira César
        [localidade] => São Paulo
        [uf] => SP
        [ibge] => 3550308,
        [complemento] => 
		[gia] => 1004
    )
    
}
  • Object => getObject()

if ($zipCodeInfo) 
{

    $zipCodeInfo->getObject();
    
    stdClass Object
    (
        [cep] => 01414-001
        [logradouro] => Rua Haddock Lobo
        [bairro] => Cerqueira César
        [localidade] => São Paulo
        [uf] => SP
        [ibge] => 3550308
        [complemento] => 
		[gia] => 1004
    )
    
}

##Renew item from cache


$zipCodeInfo  = ZipCode::find('01414001', true);

if ($zipCodeInfo) 
{

    $zipCodeInfo->getObject();
   
    stdClass Object
    (
        [cep] => 01414-001
        [logradouro] => Rua Haddock Lobo
        [bairro] => Cerqueira César
        [localidade] => São Paulo
        [uf] => SP
        [ibge] => 3550308
        [complemento] => 
		[gia] => 1004
    )
}

Package Address

Obs: follows the same coding of ZipCode

###To add to the list of UF:

use Canducci\ZipCode\ZipCodeUf;

$lists = ZipCodeUf::lists();

###To search for all zip of a particular city , uf and address


public function get(Request $request)
{
    $uf = $request->get('uf');
    
    $city = $request->get('cidade');
    
    $address = $request->get('endereco')
    
    $zipcodeaddressinfo = zipcodeaddress($uf,$city,$address);

    if ($zipcodeaddressinfo)
    {

        return $zipcodeaddressinfo->getJson();

    }
    
    return Response::json(['error' => 1]);

}