Skip to content

Commit

Permalink
Merge pull request #12 from Cyber-Duck/feature/add-postzon-by-postcod…
Browse files Browse the repository at this point in the history
…e-endpoint

Extended the package with a new Loqate endpoint
  • Loading branch information
nadge authored Apr 19, 2023
2 parents 175f9e3 + 21e99ca commit f88764d
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 6 deletions.
7 changes: 4 additions & 3 deletions config/laravel-address-finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
'loqate' =>[
'api' => [
'key' => env('LOQATE_API_KEY'),
'base_uri' => env('LOQATE_API_BASE_URI', 'https://api.addressy.com/Capture/Interactive/'),
'base_uri' => env('LOQATE_API_BASE_URI', 'https://api.addressy.com/'),
'endpoints' => [
'suggestions' => 'Find/v1.10/json3.ws',
'details' => 'Retrieve/v1.10/json3.ws',
'suggestions' => 'Capture/Interactive/Find/v1.10/json3.ws',
'details' => 'Capture/Interactive/Retrieve/v1.10/json3.ws',
'postzon' => 'GovernmentData/Postzon/RetrieveByPostcode/v1.50/json3ex.ws',
],
],
]
Expand Down
67 changes: 64 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This package provides a facade for address searching. Currently the only driver
Via Composer

``` bash
$ composer require cyberduck/laravel-address-finder
$ composer require cyber-duck/laravel-address-finder
```

## Usage
Expand All @@ -31,7 +31,7 @@ ADDRESS_FINDER_CACHE_DRIVER # If cacheing this setting can be used to override t
## Default: null
LOQATE_API_KEY # Required if using the loqate driver
## Default: https://api.addressy.com/Capture/Interactive/
## Default: https://api.addressy.com/
LOQATE_API_BASE_URI # This can be used to overide the API based URI when using the loqate driver
```

Expand Down Expand Up @@ -101,4 +101,65 @@ CyberDuck\AddressFinder\Facades\Address::details('2')->get();
"address_line_3" => "",
]
*/
```
```

### Retrieving Postzon record for the given postcode

```php
// $postcode: The postcode to use to search with
Address::postzon($postcode);
```

**Example**

```php
CyberDuck\AddressFinder\Facades\Address::postzon('N7 7PH')->get();

/*
[
'Easting' => 530915,
'Northing' => 186310,
'Latitude' => 51.560487,
'Longitude' => -0.112808,
'OsGrid' => 'TQ 30915 86310',
'CountryCode' => '921',
'NewCountryCode' => 'E92000001',
'CountryName' => 'England',
'CountyCode' => '',
'NewCountyCode' => 'E99999999',
'CountyName' => '(pseudo) England (UA/MD/LB)',
'DistrictCode' => '',
'NewDistrictCode' => 'E09000019',
'DistrictName' => 'Islington',
'WardCode' => '00AUGC',
'NewWardCode' => 'E05013703',
'WardName' => '',
'NhsShaCode' => 'Q36',
'NewNhsShaCode' => '',
'NhsShaName' => 'London',
'NhsPctCode' => '',
'NewNhsPctCode' => 'E16000048',
'NhsPctName' => 'Islington',
'LeaCode' => '',
'LeaName' => '',
'GovernmentOfficeCode' => 'H',
'GovernmentOfficeName' => 'London',
'WestminsterConstituencyCode' => 'C36',
'WestminsterConstituencyName' => 'Islington North',
'WestminsterMP' => 'Jeremy Corbyn',
'WestminsterParty' => 'Labour',
'WestminsterConstituencyCode2010' => 'C36',
'WestminsterConstituencyName2010' => 'Islington North',
'LSOACode' => 'E01002730',
'LSOAName' => 'Islington 007A',
'MSOACode' => 'E02000560',
'MSOAName' => 'Islington 007',
'CCGCode' => '93C',
'CCGName' => 'NHS North Central London CCG',
'CCGAreaCode' => '',
'CCGAreaName' => '',
'CCGRegionCode' => '',
'CCGRegionName' => '',
]
*/
```
9 changes: 9 additions & 0 deletions src/AddressFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public function details($addressId, bool $raw = false, bool $translated = false,
return $this->addressEngine()->getDetails($addressId, $raw, $translated, $customFields);
}

/**
* @param $postcode
* @return Postzon
*/
public function postzon($postcode)
{
return $this->addressEngine()->postzon($postcode);
}

/**
* Get the Scout engine for the model.
*
Expand Down
15 changes: 15 additions & 0 deletions src/CachedAddressFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ function () use ($addressId, $raw, $translated, $customFields) {
);
}

/**
* @param $postcode
* @return Postzon
*/
public function postzon($postcode)
{
return \Cache::store($this->store)->remember(
$this->buildCacheKey([$postcode]),
config('laravel-address-finder.cache.ttl', 1440),
function () use ($postcode) {
return parent::postzon($postcode);
}
);
}

/**
* @param $slug
* @return string
Expand Down
7 changes: 7 additions & 0 deletions src/Drivers/DriverContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CyberDuck\AddressFinder\Drivers;

use CyberDuck\AddressFinder\Details;
use CyberDuck\AddressFinder\Postzon;
use CyberDuck\AddressFinder\Suggestions;

interface DriverContract
Expand All @@ -24,4 +25,10 @@ public function suggestions($query, $country, $group_id, bool $raw = false);
* @return Details
*/
public function getDetails($id, bool $raw = false, bool $translated = false, array $customFields = []);

/**
* @param $postcode
* @return Postzon
*/
public function postzon($postcode);
}
35 changes: 35 additions & 0 deletions src/Drivers/LoqateDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CyberDuck\AddressFinder\Drivers;

use CyberDuck\AddressFinder\Details;
use CyberDuck\AddressFinder\Postzon;
use CyberDuck\AddressFinder\Suggestions;
use Http;
use Illuminate\Http\Client\PendingRequest;
Expand All @@ -26,6 +27,11 @@ class LoqateDriver implements DriverContract
*/
private $detailsEndpoint;

/**
* @var string
*/
private $postzonEndpoint;

/**
* @var PendingRequest
*/
Expand All @@ -39,6 +45,7 @@ public function __construct()
$config = config('laravel-address-finder.loqate');
$this->suggestionsEndpoint = $config['api']['endpoints']['suggestions'];
$this->detailsEndpoint = $config['api']['endpoints']['details'];
$this->postzonEndpoint = $config['api']['endpoints']['postzon'];
$this->client = Http::withOptions([
'base_uri' => $config['api']['base_uri'],
'query' => [
Expand Down Expand Up @@ -141,6 +148,34 @@ public function parseDetails($response, bool $raw = false, bool $translated = fa
->setCustomFields($this->buildCustomFieldsForResponse($customFields, $addressDetails));
}

/**
* @param $postcode
* @return Postzon
*/
public function postzon($postcode)
{
return $this->parsePostzon($this->client->get(
$this->postzonEndpoint,
[
'Postcode' => $postcode,
]
)->json());
}

/**
* @param $response
* @return Postzon
*/
public function parsePostzon($response)
{
/** @var Postzon $postzone */
$postzon = app(Postzon::class);

$postzon->setItems($response['Items'] ?? []);

return $postzon;
}

/**
* @param $id
* @param array $customFields
Expand Down
125 changes: 125 additions & 0 deletions src/Drivers/MockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CyberDuck\AddressFinder\Drivers;

use CyberDuck\AddressFinder\Details;
use CyberDuck\AddressFinder\Postzon;
use CyberDuck\AddressFinder\Suggestions;
use Faker\Generator;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -435,6 +436,130 @@ public function parseDetails($response, bool $raw = false, bool $translated = fa
->setCustomFields($this->buildCustomFieldsForResponse($customFields, $addressDetails));
}

/**
* @param $postcode
* @return Postzon
*/
public function postzon($postcode)
{
$postzonRecords = [
'N7 7PH' => [
'Items' => [
[
'Easting' => 530915,
'Northing' => 186310,
'Latitude' => 51.560487,
'Longitude' => -0.112808,
'OsGrid' => 'TQ 30915 86310',
'CountryCode' => '921',
'NewCountryCode' => 'E92000001',
'CountryName' => 'England',
'CountyCode' => '',
'NewCountyCode' => 'E99999999',
'CountyName' => '(pseudo) England (UA/MD/LB)',
'DistrictCode' => '',
'NewDistrictCode' => 'E09000019',
'DistrictName' => 'Islington',
'WardCode' => '00AUGC',
'NewWardCode' => 'E05013703',
'WardName' => '',
'NhsShaCode' => 'Q36',
'NewNhsShaCode' => '',
'NhsShaName' => 'London',
'NhsPctCode' => '',
'NewNhsPctCode' => 'E16000048',
'NhsPctName' => 'Islington',
'LeaCode' => '',
'LeaName' => '',
'GovernmentOfficeCode' => 'H',
'GovernmentOfficeName' => 'London',
'WestminsterConstituencyCode' => 'C36',
'WestminsterConstituencyName' => 'Islington North',
'WestminsterMP' => 'Jeremy Corbyn',
'WestminsterParty' => 'Labour',
'WestminsterConstituencyCode2010' => 'C36',
'WestminsterConstituencyName2010' => 'Islington North',
'LSOACode' => 'E01002730',
'LSOAName' => 'Islington 007A',
'MSOACode' => 'E02000560',
'MSOAName' => 'Islington 007',
'CCGCode' => '93C',
'CCGName' => 'NHS North Central London CCG',
'CCGAreaCode' => '',
'CCGAreaName' => '',
'CCGRegionCode' => '',
'CCGRegionName' => '',
],
],
],
'IG11 9XL' => [
'Items' => [
[
'Easting' => 544703,
'Northing' => 184286,
'Latitude' => 51.538933,
'Longitude' => 0.085102,
'OsGrid' => 'TQ 44703 84286',
'CountryCode' => '921',
'NewCountryCode' => 'E92000001',
'CountryName' => 'England',
'CountyCode' => '',
'NewCountyCode' => 'E99999999',
'CountyName' => '(pseudo) England (UA/MD/LB)',
'DistrictCode' => '',
'NewDistrictCode' => 'E09000002',
'DistrictName' => 'Barking and Dagenham',
'WardCode' => '00ABFX',
'NewWardCode' => 'E05014066',
'WardName' => '',
'NhsShaCode' => 'Q36',
'NewNhsShaCode' => '',
'NhsShaName' => 'London',
'NhsPctCode' => '',
'NewNhsPctCode' => 'E16000009',
'NhsPctName' => 'Barking and Dagenham',
'LeaCode' => '',
'LeaName' => '',
'GovernmentOfficeCode' => 'H',
'GovernmentOfficeName' => 'London',
'WestminsterConstituencyCode' => 'A11',
'WestminsterConstituencyName' => 'Barking',
'WestminsterMP' => 'Dame Margaret Hodge',
'WestminsterParty' => 'Labour',
'WestminsterConstituencyCode2010' => 'A11',
'WestminsterConstituencyName2010' => 'Barking',
'LSOACode' => 'E01000009',
'LSOAName' => 'Barking and Dagenham 016B',
'MSOACode' => 'E02000017',
'MSOAName' => 'Barking and Dagenham 016',
'CCGCode' => 'A3A8R',
'CCGName' => 'NHS North East London CCG',
'CCGAreaCode' => '',
'CCGAreaName' => '',
'CCGRegionCode' => '',
'CCGRegionName' => '',
],
],
],
];

return $this->parsePostzon($postzonRecords[$postcode]);
}

/**
* @param $response
* @return Postzon
*/
public function parsePostzon($response)
{
/** @var Postzon $postzone */
$postzon = app(Postzon::class);

$postzon->setItems($response['Items'] ?? []);

return $postzon;
}

/**
* @param array $customFields
* @param array $addressDetails
Expand Down
2 changes: 2 additions & 0 deletions src/Facades/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace CyberDuck\AddressFinder\Facades;

use CyberDuck\AddressFinder\Details;
use CyberDuck\AddressFinder\Postzon;
use CyberDuck\AddressFinder\Suggestions;
use Illuminate\Support\Facades\Facade;

/**
* @method static Suggestions suggestions($query, $country, $group_id, bool $raw = false)
* @method static Details details($id, bool $raw = false, bool $translated = false, array $customFields = [])
* @method static Postzon postzon($postcode)
*
* @see \CyberDuck\AddressFinder\AddressFinder
*
Expand Down
Loading

0 comments on commit f88764d

Please sign in to comment.