netdragoon / zipcode by fulviocanducci

ZipCode From Brazil
37,579
44
4
Package Data
Maintainer Username: fulviocanducci
Maintainer Contact: fulviocanducci@hotmail.com (fulviocanducci)
Package Create Date: 2015-10-15
Package Last Update: 2020-12-16
Home Page: https://packagist.org/packages/canducci/zipcode
Language: PHP
License: MIT
Last Refreshed: 2024-11-18 15:00:19
Package Statistics
Total Downloads: 37,579
Monthly Downloads: 314
Daily Downloads: 3
Total Stars: 44
Total Watchers: 4
Total Forks: 19
Total Open Issues: 0

Canducci Zipcode

Laravel package for VIACEP web service - http://viacep.com.br

Zoom Icon

Build Status Downloads License Version

See Demo

Quick Start with Composer

composer require canducci/zipcode

Add these classes to the end of providers array in config/app.php:

'providers' => [
    // ...,
    Canducci\ZipCode\Providers\ZipCodeServiceProvider::class,
    Canducci\ZipCode\Providers\ZipCodeAddressServiceProvider::class,
],

And add those to the aliases array:

'aliases' => [
    // ...,
    'ZipCode'   => Canducci\ZipCode\Facades\ZipCode::class,
    'Address'   => Canducci\ZipCode\Facades\ZipCodeAddress::class,
],

How to Use It

Just pass the zipcode to any of the ways of retrieving it:

Facade

use Canducci\ZipCode\Facades\ZipCode;

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

Helper

$zipCodeInfo = zipcode('01414000');

Injection

use Canducci\ZipCode\Contracts\ZipCodeContract;

public function index(ZipCodeContract $zipcode)
{
      $zipCodeInfo = $zipcode->find('01414-000');
}

Trait

use Canducci\ZipCode\ZipCodeTrait;

class WelcomeController extends Controller
{
    use ZipCodeTrait;

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

How to Use Summary

These is how they differ:

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

$zipCodeInfo = $zipcode->find('01414-000'); // Contract

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

$zipCodeInfo = $this->zipcode('01414-000'); // Trait

Cache Renewal

You can force an item to renewal its cache with the second parameter:

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

$zipCodeInfo = $zipcode->find('01414-000', true); // Contract

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

$zipCodeInfo = $this->zipcode('01414-000', true); // Trait

Transforming the Return Type

By default, the return type is null or an instance of Canducci\ZipCode\ZipCodeInfo, but you can transform it to any of the following common types:

if ($zipCodeInfo) // null or ZipCodeInfo
{
    $arr = $zipCodeInfo->getArray(); // Array

    $json = $zipCodeInfo->getJson(); // Json

    $obj = $zipCodeInfo->getObject(); // Object
}

Array

if ($zipCodeInfo)
{
    $arr = $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,
        [unidade] =>
    )
    */
}

Json

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

Object

if ($zipCodeInfo)
{
    $obj = $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
        [unidade] =>
    )
    */
}

Seaching for all Zipcodes for a particular City, UF, 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]);
}

Note: it's not mandatory to use it via helper in this case too.


To add to the list of UF

use Canducci\ZipCode\ZipCodeUf;

$lists = ZipCodeUf::lists();