Skip to content

Commit

Permalink
Merge branch 'feature/laravel-12'
Browse files Browse the repository at this point in the history
  • Loading branch information
nickurt committed Feb 28, 2025
2 parents 92147ba + 9e8f820 commit fa19a09
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 13 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ jobs:
strategy:
matrix:
php: [8.3, 8.2]
laravel: [10.*, 11.*]
laravel: [11.*, 12.*]
dependency-version: [prefer-lowest, prefer-stable]
os: [ubuntu-latest]
include:
- laravel: 12.*
testbench: 10.*
dependency-version: prefer-lowest
- laravel: 12.*
testbench: 10.*
dependency-version: prefer-stable
- laravel: 11.*
testbench: 9.*
- laravel: 10.*
testbench: 8.*
dependency-version: prefer-lowest
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
dependency-version: prefer-stable

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
Expand Down
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

All notable changes to `laravel-postcodeapi` will be documented in this file

## 2.0.0 - 2024-xx-xx
## 2.0.0 - 2025-xx-xx

- Removed outdated Providers (Algolia, GeonamesDE, GeoPostcodeOrgUk, PostcodeData, PostcodeNL, PostcodesNL, Pstcd and UkPostcodes)
- Removed outdated Providers (Algolia, GeonamesDE, GeoPostcodeOrgUk, PostcodeData, PostcodesNL, Pstcd and UkPostcodes)
- Refactor'd Guzzle HttpClient to Laravel's native Http Client
- Adding support for Laravel 12 ([#52](https://github.com/nickurt/laravel-postcodeapi/issues/52))

## 1.21.0 - 2024-03-09

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ $postCode30 = PostcodeApi::create('PostcodeApiNu')->find('1118CP');
$postCode31 = PostcodeApi::create('PostcodeApiNu')->findByPostcodeAndHouseNumber('1118CP', '202');
$postCode32 = PostcodeApi::create('PostcodeApiNuV3')->find('1118CP');
$postCode33 = PostcodeApi::create('PostcodeApiNuV3')->findByPostcodeAndHouseNumber('1118CP', '202');
$postCode34 = PostcodeApi::create('Pro6PP_NL')->find('1118CP');
$postCode34 = PostcodeApi::create('PostcodeNL')->findByPostcodeAndHouseNumber('1118CP', '202');
$postCode35 = PostcodeApi::create('Pro6PP_NL')->find('1118CP');
```
#### Route
```php
Route::get('/{postCode}', function($postCode) {
$postCode35 = PostcodeApi::create('PostcodeApiNu')->find($postCode);
$postCode36 = PostcodeApi::create('PostcodeApiNu')->find($postCode);

return Response::json($postCode35->toArray(), 200, [], JSON_PRETTY_PRINT);
});
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nickurt/laravel-postcodeapi",
"description": "Universal PostcodeApi for Laravel 10.x & 11.x",
"description": "Universal PostcodeApi for Laravel 11.x & 12.x",
"keywords": [
"postcodeapi",
"postcode",
Expand All @@ -11,13 +11,13 @@
"license": "MIT",
"require": {
"php": "^8.2",
"laravel/framework": "^10.0|^11.0",
"laravel/framework": "^11.0|^12.0",
"guzzlehttp/guzzle": "^7.8.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^10.5.12|^11.0.8",
"orchestra/testbench": "^8.0|^9.0"
"phpunit/phpunit": "^11.0.8",
"orchestra/testbench": "^9.0|^10.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions config/postcodeapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
'key' => '',
'code' => 'nl_NL'
],
'PostcodeNL' => [
'url' => 'https://api.postcode.nl/rest/addresses/%s/%s',
'key' => '',
'secret' => '',
'code' => 'nl_NL'
],
'PostcodeApiNu' => [
'url' => 'https://postcode-api.apiwise.nl/v2/addresses/?postcode=%s&number=%s',
'key' => '',
Expand Down
54 changes: 54 additions & 0 deletions src/Providers/nl_NL/PostcodeNL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace nickurt\PostcodeApi\Providers\nl_NL;

use Illuminate\Support\Facades\Http;
use nickurt\PostcodeApi\Entity\Address;
use nickurt\PostcodeApi\Exception\NotSupportedException;
use nickurt\PostcodeApi\Providers\Provider;

class PostcodeNL extends Provider
{
public function find(string $postCode): Address
{
throw new NotSupportedException();
}

protected function request()
{
try {
return Http::withBasicAuth($this->getApiKey(), $this->getApiSecret())
->get($this->getRequestUrl())->json();
} catch (\Exception $e) {
return json_decode($e->getMessage(), true);
}
}

public function findByPostcode(string $postCode): Address
{
throw new NotSupportedException();
}

public function findByPostcodeAndHouseNumber(string $postCode, string $houseNumber): Address
{
$this->setRequestUrl(sprintf($this->getRequestUrl(), $postCode, $houseNumber));

$response = $this->request();

if (isset($response['exception'])) {
return new Address();
}

$address = new Address();
$address
->setStreet($response['street'])
->setTown($response['city'])
->setHouseNo($response['houseNumber'])
->setMunicipality($response['municipality'])
->setProvince($response['province'])
->setLatitude($response['latitude'])
->setLongitude($response['longitude']);

return $address;
}
}
75 changes: 75 additions & 0 deletions tests/Providers/nl_NL/PostcodeNLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace nickurt\PostcodeApi\tests\Providers\nl_NL;

use Illuminate\Http\Client\HttpClientException;
use Illuminate\Support\Facades\Http;
use nickurt\PostcodeApi\Entity\Address;
use nickurt\PostcodeApi\Providers\nl_NL\PostcodeNL;
use nickurt\PostcodeApi\tests\TestCase;

class PostcodeNLTest extends TestCase
{
/** @var PostcodeNL */
protected $postcodeNL;

public function setUp(): void
{
$this->postcodeNL = (new PostcodeNL)
->setRequestUrl('https://api.postcode.nl/rest/addresses/%s/%s')
->setApiKey('api-key')
->setApiSecret('api-secret');
}

public function test_it_can_get_the_default_config_values_for_this_provider()
{
$this->assertSame('api-key', $this->postcodeNL->getApiKey());
$this->assertSame('api-secret', $this->postcodeNL->getApiSecret());
$this->assertSame('https://api.postcode.nl/rest/addresses/%s/%s', $this->postcodeNL->getRequestUrl());
}

public function test_it_can_get_the_correct_values_for_find_by_postcode_and_house_number_a_valid_postal_code()
{
Http::fake(['https://json.api-postcode.nl?postcode=1118CP&number=202' => Http::response('{"street":"Evert van de Beekstraat","houseNumber":202,"houseNumberAddition":"","postcode":"1118CP","city":"Schiphol","municipality":"Haarlemmermeer","province":"Noord-Holland","rdX":111396,"rdY":479739,"latitude":52.30389933,"longitude":4.74791023,"bagNumberDesignationId":"0394200001001951","bagAddressableObjectId":"0394010001001991","addressType":"building","purposes":["office"],"surfaceArea":16800,"houseNumberAdditions":[""]}')]);

$address = $this->postcodeNL->findByPostcodeAndHouseNumber('1118CP', '202');

$this->assertSame('api-key', $this->postcodeNL->getApiKey());
$this->assertSame('api-secret', $this->postcodeNL->getApiSecret());
$this->assertSame('https://api.postcode.nl/rest/addresses/1118CP/202', $this->postcodeNL->getRequestUrl());

$this->assertInstanceOf(Address::class, $address);

$this->assertSame([
'street' => 'Evert van de Beekstraat',
'house_no' => '202',
'town' => 'Schiphol',
'municipality' => 'Haarlemmermeer',
'province' => 'Noord-Holland',
'latitude' => 52.30389933,
'longitude' => 4.74791023,
], $address->toArray());
}

public function test_it_can_get_the_correct_values_for_find_by_postcode_and_house_number_an_invalid_postal_code()
{
Http::fake(['https://json.api-postcode.nl?postcode=1118CP&number=1' => fn () => throw new HttpClientException('{"exception":"Combination does not exist.","exceptionId":"PostcodeNl_Service_PostcodeAddress_AddressNotFoundException"}', 404)]);

// GuzzleHttp\Exception\ClientException: Client error: `GET https://json.api-postcode.nl?postcode=1118CP&number=1` resulted in a `404 Not Found` response:
// {"error":"Cannot resolve address for postcode: 1118CP"}

$address = $this->postcodeNL->findByPostcodeAndHouseNumber('1118CP', '1');

$this->assertInstanceOf(Address::class, $address);

$this->assertSame([
'street' => null,
'house_no' => null,
'town' => null,
'municipality' => null,
'province' => null,
'latitude' => null,
'longitude' => null,
], $address->toArray());
}
}

0 comments on commit fa19a09

Please # to comment.