Skip to content

Commit

Permalink
Initial work of Postcode Anywhere integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed Jun 22, 2017
1 parent 3ea7854 commit 1694ce8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 45 deletions.
9 changes: 2 additions & 7 deletions Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_once 'vendor/autoload.php';
use \RapidWeb\Postcodes\Utils\Generator;
use \RapidWeb\Postcodes\Utils\Validator;
use \RapidWeb\Postcodes\Factories\IdealPostcodesFactory;
use \RapidWeb\Postcodes\Objects\IdealPostCodes;

$postcode = Generator::generatePostcode();

Expand All @@ -12,12 +12,7 @@

if ($validated) {

// You should specify IDEAL_POSTCODES_API_KEY in your .env file,
// or pass it via the getByAPIKey method.

$idealPostcodes = IdealPostcodesFactory::getByEnvironment();

$addresses = $idealPostcodes->getAddressesByPostcode('ST163DP');
$addresses = new IdealPostCodes('API_KEY');

var_dump($addresses);

Expand Down
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,22 @@ To install, just run the following composer command.
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.

If you are using a `.env` file or another way of manage environment variables in your
project, set the following variable to specify the Ideal Postcode.

```
IDEAL_POSTCODES_API_KEY=abcdef
```

You can then use the following code to get an `IdealPostcodes` object.

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

If you are not using environment variables in your project, simple pass through the API
key as follows.

### 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.

You can then use the following code to get an `PostcodeAnywhere` object.

```php
$idealPostcodes = \RapidWeb\Postcodes\Factories\IdealPostcodesFactory::getByAPIKey('abdef');
$postcodeAnywhere = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY');
```

## Usage
Expand Down
8 changes: 8 additions & 0 deletions pa-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
require_once 'vendor/autoload.php';

use RapidWeb\Postcodes\Objects\PostcodeAnywhere;

$postcodeAnywhere = new PostcodeAnywhere('API_KEY');

$postcodeAnywhere->getAddressesByPostcode('st163jr');
26 changes: 0 additions & 26 deletions src/Factories/IdealPostcodesFactory.php

This file was deleted.

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

interface PostcodeServiceInterface
{
public function __construct($apiKey);
public function getAddressesByPostcode($postcode);
}
3 changes: 2 additions & 1 deletion src/Objects/IdealPostcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface;
use Exception;

class IdealPostCodes
class IdealPostCodes implements PostcodeServiceInterface
{
private $apiKey = null;
private $client = null;
Expand Down
41 changes: 41 additions & 0 deletions src/Objects/PostcodeAnywhere.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace RapidWeb\Postcodes\Objects;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface;
use Exception;
use SoapClient;

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

public function __construct($apiKey)
{
if (!$apiKey) {
throw new Exception('No Postcode Anywhere API key specified.');
}

$this->apiKey = $apiKey;

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

}

public function getAddressesByPostcode($postcode)
{
$findResponse = $this->findSOAPClient->PostcodeAnywhere_Interactive_Find_v1_10(
[
'Key' => $this->apiKey,
'SearchTerm' => $postcode
]
);

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

return $result;
}

}

0 comments on commit 1694ce8

Please sign in to comment.