diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 6a33daf..cce49ab 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 08d631c..038d57c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index e76aa6e..12e1522 100644 --- a/README.md +++ b/README.md @@ -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); }); diff --git a/composer.json b/composer.json index dfd4f78..119ed2c 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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": { diff --git a/config/postcodeapi.php b/config/postcodeapi.php index cc62ffb..a22264a 100644 --- a/config/postcodeapi.php +++ b/config/postcodeapi.php @@ -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' => '', diff --git a/src/Providers/nl_NL/PostcodeNL.php b/src/Providers/nl_NL/PostcodeNL.php new file mode 100644 index 0000000..f3cbd80 --- /dev/null +++ b/src/Providers/nl_NL/PostcodeNL.php @@ -0,0 +1,54 @@ +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; + } +} diff --git a/tests/Providers/nl_NL/PostcodeNLTest.php b/tests/Providers/nl_NL/PostcodeNLTest.php new file mode 100644 index 0000000..5e7e669 --- /dev/null +++ b/tests/Providers/nl_NL/PostcodeNLTest.php @@ -0,0 +1,75 @@ +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()); + } +}