roelofr / postcodeapi
A simple service for Laravel that provides postal code validation via postcodeapi.nu
Requires
- php: >=7.4
- guzzlehttp/guzzle: ^6.5 | ^7.0
- illuminate/cache: ^5.1 | ^6.0
- illuminate/support: ^5.1 | ^6.0
- illuminate/validation: ^5.1 | ^6.0
Requires (Dev)
- minimaxir/big-list-of-naughty-strings: ^1.0
- nesbot/carbon: ^2.28
- orchestra/testbench: ^3.1 | ^4.0
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2022-07-17 00:47:51 UTC
README
Easy access the postcodeapi.nu using a simple-to-use, well tested Laravel plugin.
License
Requirements
- PHP 7.4+
- Laravel 5.1 (Supports Statamic)
Installation
- Firstly, pull the addon off Packagist
composer require roelofr/postcodeapi
- (Laravel <5.5) Add Service Provider to
config/app.php
'providers' => [ // … Roelofr\PostcodeApi\ServiceProvider::class ]
- Publish the configuration
php artian vendor:publish --provider=Roelofr\PostcodeApi\ServiceProvider
- You're good to go!
Usage
This plugin provides a contract (PostcodeApiContract
) and a facade
(PostcodeApi
), use whichever you want.
The main route is the retrieve
method, which expects a postcode and a number.
public function retrieve(string $postcode, string $number): AddressInformation;
The method will automatically try to clean the postcode and number to allow for
API submission. After retireval an AddressInformation model is returned, or a
NotFoundException
is thrown.
Exceptions
- A
MalformedDataException
, if we cannot find a Dutch post code and house number in the paramters. - A
AuthenticationFailureException
if your API key does not work. - A
NotFoundException
if the given postcode + number cannot be found.
Without Laravel
To get this plugin to work without Laravel is fairly tricky, as it requires a configuration and a cache according to Laravel's schematics.
If you want to rewrite parts of the code to work without Laravel, feel free to open a PR. It's somewhat out of scope though.