Skip to content

Commit

Permalink
Completed support for PostcodeAnywhere and refactored to use custom A…
Browse files Browse the repository at this point in the history
…ddress object
  • Loading branch information
Jordan Hall committed Jun 22, 2017
1 parent 1694ce8 commit 1613d4e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
15 changes: 9 additions & 6 deletions Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
require_once 'vendor/autoload.php';
use \RapidWeb\Postcodes\Utils\Generator;
use \RapidWeb\Postcodes\Utils\Validator;
use \RapidWeb\Postcodes\Objects\IdealPostCodes;
use \RapidWeb\Postcodes\Objects\IdealPostcodes;
use \RapidWeb\Postcodes\Objects\PostcodeAnywhere;

$postcode = Generator::generatePostcode();

var_dump($postcode);

$validated = Validator::validatePostcode($postcode);

if ($validated) {
var_dump($validated);

$addresses = new IdealPostCodes('API_KEY');
$idealPostcodes = new IdealPostCodes('API_KEY');
$addresses = $idealPostcodes->getAddressesByPostcode('ST16 3DP');
var_dump($addresses);

var_dump($addresses);

}
$postcodeAnywhere = new PostcodeAnywhere('API_KEY');
$addresses = $postcodeAnywhere->getAddressesByPostcode('ST16 3DP');
var_dump($addresses);
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,33 @@ To install, just run the following composer command.

## Setup

### Ideal Postcodes
### Postcode Lookup Services

Using some of the data retrieval features provided by this library requires an Ideal Postcodes
API key. Sign up at https://ideal-postcodes.co.uk if you need to use these features.
Using some of the data retrieval features provided by this library requires a postcode lookup service.
It currently supports the following postcode lookup services.

You can then use the following code to get an `IdealPostcodes` object.
* Ideal Postcodes - https://ideal-postcodes.co.uk
* Postcode Anywhere (PCA Predict) - https://www.pcapredict.com/

```php
$idealPostcodes = new \RapidWeb\Postcodes\Objects\IdealPostcodes('API_KEY');
```


### Postcode Anywhere (PCA Predict)

Using some of the data retrieval features provided by this library requires an Postcode Anywhere (PCA Predict)
API key. Sign up at https://www.pcapredict.com/ if you need to use these features.
Sign up at the respective website if you need to use these features.

You can then use the following code to get an `PostcodeAnywhere` object.
You can then use the following code to get an appropriate postcode lookup service object.

```php
$postcodeAnywhere = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY');
$postcodeLookupService = new \RapidWeb\Postcodes\Objects\IdealPostcodes('API_KEY');
// OR
$postcodeLookupService = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY');
```

## Usage

### Get addresses by postcode

To retrieve the addreses associated with a UK postcode, just pass it to the method shown below.
You will receive an array of addresses, appropriately split by their address lines.
You will receive an array of address objects, appropriately split by their address lines and other details.

```php
$addresses = $idealPostcodes->getAddressesByPostcode('ST163DP');
$addresses = $postcodeLookupService->getAddressesByPostcode('ST163DP');
```

### Validate postcode
Expand Down
8 changes: 0 additions & 8 deletions pa-test.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Objects/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace RapidWeb\Postcodes\Objects;

class Address
{
public $companyName = '';
public $line1 = '';
public $line2 = '';
public $line3 = '';
public $townCity = '';
public $county = '';
public $country = 'United Kingdom';
public $postcode = '';
}
18 changes: 17 additions & 1 deletion src/Objects/IdealPostcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface;
use RapidWeb\Postcodes\Objects\Address;
use Exception;

class IdealPostCodes implements PostcodeServiceInterface
Expand Down Expand Up @@ -58,7 +59,22 @@ private function parseResponse(Response $response)
throw new Exception('Response does not contain a result.');
}

return $object->result;
$addresses = [];

foreach($object->result as $idealPostcodesAddress) {
$address = new Address;
$address->companyName = $idealPostcodesAddress->organisation_name;
$address->line1 = $idealPostcodesAddress->line_1;
$address->line2 = $idealPostcodesAddress->line_2;
$address->line3 = $idealPostcodesAddress->line_3;
$address->townCity = $idealPostcodesAddress->post_town;
$address->county = $idealPostcodesAddress->county;
$address->country = $idealPostcodesAddress->country;
$address->postcode = $idealPostcodesAddress->postcode;
$addresses[] = $address;
}

return $addresses;

}
}
30 changes: 29 additions & 1 deletion src/Objects/PostcodeAnywhere.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface;
use RapidWeb\Postcodes\Objects\Address;
use Exception;
use SoapClient;

class PostcodeAnywhere implements PostcodeServiceInterface
{
private $apiKey = null;
private $findSOAPClient = null;
private $retrieveByIDSOAPClient = null;

public function __construct($apiKey)
{
Expand All @@ -21,6 +23,7 @@ public function __construct($apiKey)
$this->apiKey = $apiKey;

$this->findSOAPClient = new SoapClient('https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/wsdlnew.ws');
$this->retrieveByIDSOAPClient = new \SoapClient('https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveById/v1.30/wsdlnew.ws');

}

Expand All @@ -35,7 +38,32 @@ public function getAddressesByPostcode($postcode)

$findResponseAddresses = $findResponse->PostcodeAnywhere_Interactive_Find_v1_10_Result->PostcodeAnywhere_Interactive_Find_v1_10_Results;

return $result;
$addresses = [];

foreach($findResponseAddresses as $findResponseAddress) {

$retrieveByIDResponse = $this->retrieveByIDSOAPClient->PostcodeAnywhere_Interactive_RetrieveById_v1_30(
[
'Key' => $this->apiKey,
'Id' => $findResponseAddress->Id
]
);

$retrieveAddress = $retrieveByIDResponse->PostcodeAnywhere_Interactive_RetrieveById_v1_30_Result->PostcodeAnywhere_Interactive_RetrieveById_v1_30_Results;

$address = new Address;
$address->companyName = $retrieveAddress->Company;
$address->line1 = $retrieveAddress->Line1;
$address->line2 = $retrieveAddress->Line2;
$address->line3 = $retrieveAddress->Line3;
$address->townCity = $retrieveAddress->PostTown;
$address->county = $retrieveAddress->County;
$address->postcode = $retrieveAddress->Postcode;
$address->country = $retrieveAddress->CountryName;
$addresses[] = $address;
}

return $addresses;
}

}

0 comments on commit 1613d4e

Please sign in to comment.