Skip to content

Commit c9adefa

Browse files
Add support for address validation calls and an example
1 parent 4a44da4 commit c9adefa

File tree

3 files changed

+112
-12
lines changed

3 files changed

+112
-12
lines changed

example/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
throw new Exception('This example only supports calls to international or dutchAddress methods');
3030

3131
// Use the session header if it was specified already (the Postcode.nl js library will generate one automatically, for example)
32-
// Fall back to a place holder identifier. Don't use a fixed identifier for production environments, please read:
32+
// Fall back to a placeholder identifier. Don't use a fixed identifier for production environments, please read:
3333
// https://developer.postcode.eu/documentation/international/v1/Autocomplete/autocomplete
3434
$sessionHeaderKey = 'HTTP_' . str_replace('-', '_', strtoupper(PostcodeNl\Api\Client::SESSION_HEADER_KEY));
3535
$sessionId = $_SERVER[$sessionHeaderKey] ?? 'MY_SESSION_ID';

example/validate.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
define('API_KEY', '** insert key here **');
3+
define('API_SECRET', '** insert secret here **');
4+
define('PLATFORM', 'example proxy');
5+
6+
7+
// this will load exceptions when they occur
8+
spl_autoload_register(function($class){
9+
if (0 === strpos($class, 'PostcodeNl\\'))
10+
require(__DIR__ .'/../src/'. str_replace('\\', '/', $class) .'.php');
11+
});
12+
$result = null;
13+
if (isset($_GET['validate']))
14+
{
15+
$client = new PostcodeNl\Api\Client(API_KEY, API_SECRET, PLATFORM);
16+
$result = $client->validate(
17+
$_GET['country'],
18+
$_GET['postcode'],
19+
$_GET['locality'],
20+
$_GET['street'],
21+
$_GET['building'],
22+
$_GET['region'],
23+
$_GET['streetAndBuilding']
24+
);
25+
}
26+
27+
?>
28+
<!DOCTYPE html>
29+
<html lang="nl-NL">
30+
<head>
31+
<style>
32+
input[type=text]{
33+
width: 500px;
34+
}
35+
36+
.validation-result {
37+
padding: 1em;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<h3>Address validation</h3>
43+
<p>For more information see README.md</p>
44+
<form action="validate.php" method="GET">
45+
<label>Country <input type="text" name="country" placeholder="Country" value="<?php print($_GET['country'] ?? 'nld'); ?>"></label><br>
46+
<label>Postcode <input type="text" name="postcode" placeholder="Postcode" value="<?php print($_GET['postcode'] ?? '2012 ES'); ?>"></label><br>
47+
<label>Locality <input type="text" name="locality" placeholder="Locality" value="<?php print($_GET['locality'] ?? 'Haarlem'); ?>"></label><br>
48+
<label>Street <input type="text" name="street" placeholder="Street" value="<?php print($_GET['street'] ?? 'Julianastraat'); ?>"></label><br>
49+
<label>Building <input type="text" name="building" placeholder="Building" value="<?php print($_GET['building'] ?? '30'); ?>"></label><br>
50+
<label>Region <input type="text" name="region" placeholder="Region" value="<?php print($_GET['region'] ?? ''); ?>"></label><br>
51+
<label>Street and building <input type="text" name="streetAndBuilding" placeholder="Street and building" value="<?php print($_GET['streetAndBuilding'] ?? ''); ?>"></label><br>
52+
53+
<input type="submit" value="Validate" name="validate">
54+
</form>
55+
56+
<pre class="validation-result"><?php print($result === null ? '' : var_export($result, true)); ?></pre>
57+
</body>

src/PostcodeNl/Api/Client.php

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct(string $key, string $secret, string $platform)
8282
}
8383

8484
/**
85-
* @see https://developer.postcode.eu/documentation//international/v1/Autocomplete/autocomplete
85+
* @see https://developer.postcode.eu/documentation/international/v1/Autocomplete/autocomplete
8686
*/
8787
public function internationalAutocomplete(string $context, string $term, string $session, string $language = null): array
8888
{
@@ -100,7 +100,7 @@ public function internationalAutocomplete(string $context, string $term, string
100100
}
101101

102102
/**
103-
* @see https://developer.postcode.eu/documentation//international/v1/Autocomplete/getDetails
103+
* @see https://developer.postcode.eu/documentation/international/v1/Autocomplete/getDetails
104104
*/
105105
public function internationalGetDetails(string $context, string $session): array
106106
{
@@ -110,15 +110,15 @@ public function internationalGetDetails(string $context, string $session): array
110110
}
111111

112112
/**
113-
* @see https://developer.postcode.eu/documentation//international/v1/Autocomplete/getSupportedCountries
113+
* @see https://developer.postcode.eu/documentation/international/v1/Autocomplete/getSupportedCountries
114114
*/
115115
public function internationalGetSupportedCountries(): array
116116
{
117117
return $this->_performApiCall('international/v1/supported-countries', null);
118118
}
119119

120120
/**
121-
* @see https://developer.postcode.eu/documentation//nl/v1/Address/viewByPostcode
121+
* @see https://developer.postcode.eu/documentation/nl/v1/Address/viewByPostcode
122122
*/
123123
public function dutchAddressByPostcode(string $postcode, int $houseNumber, ?string $houseNumberAddition = null): array
124124
{
@@ -143,7 +143,7 @@ public function dutchAddressByPostcode(string $postcode, int $houseNumber, ?stri
143143
}
144144

145145
/**
146-
* @see https://developer.postcode.eu/documentation//nl/v1/Address/matchExact
146+
* @see https://developer.postcode.eu/documentation/nl/v1/Address/matchExact
147147
*/
148148
public function dutchAddressExactMatch(string $city, string $street, int $houseNumber, string $houseNumberAddition = ''): array
149149
{
@@ -159,7 +159,7 @@ public function dutchAddressExactMatch(string $city, string $street, int $houseN
159159
}
160160

161161
/**
162-
* @see https://developer.postcode.eu/documentation//nl/v1/Address/viewByRd
162+
* @see https://developer.postcode.eu/documentation/nl/v1/Address/viewByRd
163163
*/
164164
public function dutchAddressRD(float $rdX, float $rdY): array
165165
{
@@ -173,7 +173,7 @@ public function dutchAddressRD(float $rdX, float $rdY): array
173173
}
174174

175175
/**
176-
* @see https://developer.postcode.eu/documentation//nl/v1/Address/viewByLatLon
176+
* @see https://developer.postcode.eu/documentation/nl/v1/Address/viewByLatLon
177177
*/
178178
public function dutchAddressLatLon(float $latitude, float $longitude): array
179179
{
@@ -187,7 +187,7 @@ public function dutchAddressLatLon(float $latitude, float $longitude): array
187187
}
188188

189189
/**
190-
* @see https://developer.postcode.eu/documentation//nl/v1/Address/viewByBagNumberDesignationId
190+
* @see https://developer.postcode.eu/documentation/nl/v1/Address/viewByBagNumberDesignationId
191191
*/
192192
public function dutchAddressBagNumberDesignation(string $bagNumberDesignationId): array
193193
{
@@ -200,7 +200,7 @@ public function dutchAddressBagNumberDesignation(string $bagNumberDesignationId)
200200
}
201201

202202
/**
203-
* @see https://developer.postcode.eu/documentation//nl/v1/Address/viewByBagAddressableObjectId
203+
* @see https://developer.postcode.eu/documentation/nl/v1/Address/viewByBagAddressableObjectId
204204
*/
205205
public function dutchAddressBagAddressableObject(string $bagAddressableObjectId): array
206206
{
@@ -213,7 +213,7 @@ public function dutchAddressBagAddressableObject(string $bagAddressableObjectId)
213213
}
214214

215215
/**
216-
* @see https://developer.postcode.eu/documentation//nl/v1/PostcodeRange/viewByPostcode
216+
* @see https://developer.postcode.eu/documentation/nl/v1/PostcodeRange/viewByPostcode
217217
*/
218218
public function dutchAddressPostcodeRanges(string $postcode): array
219219
{
@@ -233,7 +233,50 @@ public function dutchAddressPostcodeRanges(string $postcode): array
233233
}
234234

235235
/**
236-
* @see https://developer.postcode.eu/documentation//account/v1/Account/getInfo
236+
* @see https://developer.postcode.eu/documentation/international/v1/Validate/validate
237+
*/
238+
public function validate(string $country, ?string $postcode = null, ?string $locality = null, ?string $street = null, ?string $building = null, ?string $region = null, ?string $streetAndBuilding = null): array
239+
{
240+
$urlParts = [
241+
'international/v1/validate',
242+
rawurlencode($country)
243+
];
244+
$variables = [
245+
'postcode' => $postcode,
246+
'locality' => $locality,
247+
'street' => $street,
248+
'building' => $building,
249+
'region' => $region,
250+
'streetAndBuilding' => $streetAndBuilding,
251+
];
252+
$parameters = [];
253+
foreach ($variables as $key => $value)
254+
{
255+
if ($value === null)
256+
{
257+
continue;
258+
}
259+
$parameters[] = $key . '=' . rawurlencode($value);
260+
}
261+
262+
return $this->_performApiCall(implode('/', $urlParts) . '?' . implode('&', $parameters), null);
263+
}
264+
265+
/**
266+
* @see https://developer.postcode.eu/documentation/international/v1/Validate/getCountry
267+
*/
268+
public function getCountry(string $country): array
269+
{
270+
$urlParts = [
271+
'international/v1/country',
272+
rawurlencode($country),
273+
];
274+
275+
return $this->_performApiCall(implode('/', $urlParts), null);
276+
}
277+
278+
/**
279+
* @see https://developer.postcode.eu/documentation/account/v1/Account/getInfo
237280
*/
238281
public function accountInfo(): array
239282
{

0 commit comments

Comments
 (0)