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 |
Laravel package for VIACEP web service - http://viacep.com.br
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,
],
Just pass the zipcode to any of the ways of retrieving it:
use Canducci\ZipCode\Facades\ZipCode;
$zipCodeInfo = ZipCode::find('01414-001');
$zipCodeInfo = zipcode('01414000');
use Canducci\ZipCode\Contracts\ZipCodeContract;
public function index(ZipCodeContract $zipcode)
{
$zipCodeInfo = $zipcode->find('01414-000');
}
use Canducci\ZipCode\ZipCodeTrait;
class WelcomeController extends Controller
{
use ZipCodeTrait;
public function index()
{
$zipCodeInfo = $this->zipcode('01414000');
}
}
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
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
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
}
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] =>
)
*/
}
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": ""
}
*/
}
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] =>
)
*/
}
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.
use Canducci\ZipCode\ZipCodeUf;
$lists = ZipCodeUf::lists();