Skip to content

Commit 6890c33

Browse files
author
Evert Harmeling
committed
Added /postcodes call
1 parent 51062a6 commit 6890c33

6 files changed

Lines changed: 96 additions & 46 deletions

File tree

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.4
54
- 5.5
65
- 5.6
76
- 7.0

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.1.0
4+
5+
* Added `getPostcodes()` call, to get postcodes based on provided `latitude, longitude`. Note that this call is only available with a premium account.
6+
37
## 2.0.0
48

59
* Rewrite to support postcodeapi.nu version 2

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ $client = new \FH\PostcodeAPI\Client(new \GuzzleHttp\Client(), $apiKey);
4141
// call endpoints
4242
$response = $client->getAddresses('5041EB', 21);
4343
$response = $client->getAddress('0855200000061001');
44+
45+
// Note that this call is only available with a premium account
46+
$response = $client->getPostcodes('51.566405', '5.077171');
4447
```
4548

4649
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/freshheads/fhpostcodeapiclient/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"php": ">=5.4.0",
2020
"guzzlehttp/guzzle": "^6.0"
2121
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^4.3.5"
24+
},
2225
"autoload": {
2326
"psr-0": { "FH\\PostcodeAPI": "lib/" }
2427
},

lib/FH/PostcodeAPI/Client.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,35 @@
1616
*/
1717
class Client
1818
{
19-
const BASE_URI = 'https://postcode-api.apiwise.nl';
19+
const POSTCODES_SORT_DISTANCE = 'distance';
20+
21+
/**
22+
* @var string
23+
*/
24+
private $uriScheme = 'https://';
25+
26+
/**
27+
* @var null|string
28+
*/
29+
private $domain = 'postcode-api.apiwise.nl';
30+
31+
/**
32+
* @var string
33+
*/
34+
private $version = 'v2';
2035

2136
/**
2237
* @var HTTPClient
2338
*/
2439
private $httpClient;
2540

26-
public function __construct(ClientInterface $httpClient)
41+
42+
public function __construct(ClientInterface $httpClient, $domain = null)
2743
{
44+
if (null !== $domain) {
45+
$this->domain = $domain;
46+
}
47+
2848
$this->httpClient = $httpClient;
2949
}
3050

@@ -33,11 +53,11 @@ public function __construct(ClientInterface $httpClient)
3353
* @param string|null $number
3454
* @param int $from
3555
*
36-
* @return \StdClass
56+
* @return \stdClass
3757
*/
3858
public function getAddresses($postcode = null, $number = null, $from = 0)
3959
{
40-
return $this->get('/v2/addresses/', [
60+
return $this->get('/addresses/', [
4161
'postcode' => $postcode,
4262
'number' => $number,
4363
'from' => $from
@@ -47,26 +67,44 @@ public function getAddresses($postcode = null, $number = null, $from = 0)
4767
/**
4868
* @param string $id
4969
*
50-
* @return \StdClass
70+
* @return \stdClass
5171
*/
5272
public function getAddress($id)
5373
{
54-
return $this->get("/v2/addresses/{$id}");
74+
return $this->get(sprintf('/addresses/%s', $id));
75+
}
76+
77+
/**
78+
* @param string $latitude
79+
* @param string $longitude
80+
* @param string $sort
81+
*
82+
* @return \stdClass
83+
*/
84+
public function getPostcodesByCoordinates($latitude, $longitude, $sort = self::POSTCODES_SORT_DISTANCE)
85+
{
86+
return $this->get('/postcodes/', [
87+
'coords' => [
88+
'latitude' => $latitude,
89+
'longitude' => $longitude
90+
],
91+
'sort' => $sort
92+
]);
5593
}
5694

5795
/**
5896
* @param string $path
5997
* @param array $queryParams
6098
*
61-
* @return \StdClass
99+
* @return \stdClass
62100
*
63101
* @throws RequestException
64102
*/
65103
private function get($path, array $queryParams = array())
66104
{
67-
$url = self::BASE_URI . $path;
68-
69-
$request = $this->createHttpRequest('GET', $url, $queryParams);
105+
$request = $this->createHttpRequest('GET', sprintf('%s%s/%s%s', $this->uriScheme, $this->domain, $this->version, $path),
106+
$queryParams
107+
);
70108

71109
$response = $this->httpClient->send($request);
72110

@@ -76,7 +114,7 @@ private function get($path, array $queryParams = array())
76114
/**
77115
* @param ResponseInterface $response
78116
*
79-
* @return \StdClass
117+
* @return \stdClass
80118
*
81119
* @throws CouldNotParseResponseException
82120
*/
@@ -85,7 +123,7 @@ private function parseResponse(ResponseInterface $response)
85123
$out = json_decode((string) $response->getBody());
86124

87125
if (json_last_error() !== JSON_ERROR_NONE) {
88-
throw new CouldNotParseResponseException('Could not parse repsonse', $response);
126+
throw new CouldNotParseResponseException('Could not parse response', $response);
89127
}
90128

91129
return $out;

tests/FH/PostcodeAPI/ClientTest.php

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function testRequestExceptionIsThrownWhenUsingAnInvalidApiKey()
4343
try {
4444
$client->getAddresses();
4545

46-
$this->fail('Should not get to this point as an exception should have been thrown because the api client was supplied with an invalid API key');
46+
static::fail('Should not get to this point as an exception should have been thrown because the api client was supplied with an invalid API key');
4747
} catch (RequestException $exception) {
4848
if ($exception->getResponse() instanceof Response) {
49-
$this->assertTrue($exception->getResponse()->getStatusCode() === 401);
49+
static::assertTrue($exception->getResponse()->getStatusCode() === 401);
5050
} else {
51-
$this->fail($exception->getMessage());
51+
static::fail($exception->getMessage());
5252
}
5353
}
5454
}
@@ -65,7 +65,7 @@ public function testListResourceReturnsAllAddressesWhenNoParamsAreSupplied()
6565

6666
$addresses = $response->_embedded->addresses;
6767

68-
$this->assertTrue(count($addresses) > 0, 'Expecting that there are always addresses available');
68+
static::assertGreaterThan(0, count($addresses), 'Expecting that there are always addresses available');
6969

7070
$this->applyAddressFieldAreSetAndOfTheCorrectTypeAssertions($addresses[0]);
7171
}
@@ -82,7 +82,7 @@ public function testListResourceReturnsExpectedAddressWhenPostcodeAndNumberAreSu
8282

8383
$addresses = $response->_embedded->addresses;
8484

85-
$this->assertTrue(count($addresses) > 0, 'Expecting that there are always addresses available when no filters are applied');
85+
static::assertGreaterThan(0, count($addresses), 'Expecting that there are always addresses available when no filters are applied');
8686

8787
$firstAddress = $addresses[0];
8888

@@ -99,7 +99,6 @@ public function testExpectedAddressInformationIsReturnedFromDetailResource()
9999
$address = $client->getAddress(self::FRESHHEADS_ADDRESS_ID);
100100

101101
$this->applyAddressFieldAreSetAndOfTheCorrectTypeAssertions($address);
102-
103102
$this->applyIsFreshheadsAddressAssertions($address);
104103
}
105104

@@ -122,64 +121,68 @@ public function testClientThrowsExceptionWhenInvalidInputIsSupplied()
122121
*/
123122
private function loadMockResponse($name)
124123
{
125-
return file_get_contents(__DIR__ . "/../../Mock/{$name}");
124+
return file_get_contents(sprintf('%s/../../Mock/%s', __DIR__, $name));
126125
}
127126

128127
/**
129-
* @param \StdClass $address
128+
* @param \stdClass $address
130129
*/
131-
private function applyIsFreshheadsAddressAssertions(\StdClass $address)
130+
private function applyIsFreshheadsAddressAssertions(\stdClass $address)
132131
{
133-
$this->assertSame(strtoupper($address->postcode), self::FRESHHEADS_POSTCODE, 'Incoming postcode did not match the expected postcode');
134-
$this->assertSame((string)$address->number, (string)self::FRESHHEADS_NUMBER, 'Incoming number did not match the expected number');
135-
$this->assertSame($address->city->label, self::FRESHHEADS_CITY, 'Incoming city did not match the expected city');
132+
static::assertSame(strtoupper($address->postcode), self::FRESHHEADS_POSTCODE, 'Incoming postcode did not match the expected postcode');
133+
static::assertSame((string)$address->number, (string)self::FRESHHEADS_NUMBER, 'Incoming number did not match the expected number');
134+
static::assertSame($address->city->label, self::FRESHHEADS_CITY, 'Incoming city did not match the expected city');
136135

137136
// use number_format number rounding to allow for minor changes between expected and actual value
138-
$this->assertSame(
139-
number_format($address->geo->center->wgs84->coordinates[0], 5),
137+
$wgs84 = $address->geo->center->wgs84;
138+
139+
static::assertSame(
140+
number_format($wgs84->coordinates[0], 5),
140141
number_format(self::FRESHHEADS_LONGITUDE, 5),
141142
'Incoming longitude did not match the expected value'
142143
);
143-
$this->assertSame(
144-
number_format($address->geo->center->wgs84->coordinates[1], 5),
144+
static::assertSame(
145+
number_format($wgs84->coordinates[1], 5),
145146
number_format(self::FRESHHEADS_LATITUDE, 5),
146147
'Incoming latitude did not match the expected value'
147148
);
148149
}
149150

150151
/**
151-
* @param \StdClass $response
152+
* @param \stdClass $response
152153
*/
153-
private function applyAssertsToMakeSureAddressesArrayIsAvailableInResponse(\StdClass $response)
154+
private function applyAssertsToMakeSureAddressesArrayIsAvailableInResponse(\stdClass $response)
154155
{
155-
$this->assertTrue(isset($response->_embedded->addresses));
156-
$this->assertTrue(is_array($response->_embedded->addresses));
156+
static::assertTrue(isset($response->_embedded->addresses));
157+
static::assertTrue(is_array($response->_embedded->addresses));
157158
}
158159

159160
/**
160161
* @param \stdClass $address
161162
*/
162-
private function applyAddressFieldAreSetAndOfTheCorrectTypeAssertions(\StdClass $address)
163+
private function applyAddressFieldAreSetAndOfTheCorrectTypeAssertions(\stdClass $address)
163164
{
164165
// only test the availability of the most import fields and their values
165166

166-
$this->assertTrue(isset($address->street), 'Incoming address did not have a street field');
167-
$this->assertTrue(is_string($address->street), 'Incoming address did not have a street value of type string');
167+
static::assertTrue(isset($address->street), 'Incoming address did not have a street field');
168+
static::assertTrue(is_string($address->street), 'Incoming address did not have a street value of type string');
169+
170+
static::assertTrue(isset($address->city->label), 'Incoming address did not have a city.label field');
171+
static::assertTrue(is_string($address->city->label), 'Incoming address did not have a city.label value of type string');
168172

169-
$this->assertTrue(isset($address->city->label), 'Incoming address did not have a city.label field');
170-
$this->assertTrue(is_string($address->city->label), 'Incoming address did not have a city.label value of type string');
173+
static::assertTrue(isset($address->postcode), 'Incoming address did not have a postcode field');
174+
static::assertTrue(preg_match(self::POSTCODE_PATTERN, $address->postcode) === 1, 'Incoming address did not have a postcode value that matches the pattern: ' . self::POSTCODE_PATTERN);
171175

172-
$this->assertTrue(isset($address->postcode), 'Incoming address did not have a postcode field');
173-
$this->assertTrue(preg_match(self::POSTCODE_PATTERN, $address->postcode) === 1, 'Incoming address did not have a postcode value that matches the pattern: ' . self::POSTCODE_PATTERN);
176+
static::assertTrue(isset($address->number), 'Incoming address did not have a number field');
177+
static::assertTrue(is_string($address->number) || is_numeric($address->number), 'Incoming address did not have a number field with type string');
174178

175-
$this->assertTrue(isset($address->number), 'Incoming address did not have a number field');
176-
$this->assertTrue(is_string($address->number) || is_numeric($address->number), 'Incoming address did not have a number field with type string');
179+
$wgs84 = $address->geo->center->wgs84;
177180

178-
$this->assertTrue(isset($address->geo->center->wgs84->coordinates[0]), 'Incoming address did not have a longitude field');
179-
$this->assertTrue(is_float($address->geo->center->wgs84->coordinates[0]), 'Incoming address did not have a longitude value of type float');
181+
static::assertTrue(isset($wgs84->coordinates[0]), 'Incoming address did not have a longitude field');
182+
static::assertTrue(is_float($address->geo->center->wgs84->coordinates[0]), 'Incoming address did not have a longitude value of type float');
180183

181-
$this->assertTrue(isset($address->geo->center->wgs84->coordinates[1]), 'Incoming address did not have a latitude field');
182-
$this->assertTrue(is_float($address->geo->center->wgs84->coordinates[1]), 'Incoming address did not have a latitude value of type float');
184+
static::assertTrue(isset($wgs84->coordinates[1]), 'Incoming address did not have a latitude field');
185+
static::assertTrue(is_float($address->geo->center->wgs84->coordinates[1]), 'Incoming address did not have a latitude value of type float');
183186
}
184187

185188
/**

0 commit comments

Comments
 (0)