Skip to content

Commit 1694ce8

Browse files
author
Jordan Hall
committed
Initial work of Postcode Anywhere integration
1 parent 3ea7854 commit 1694ce8

7 files changed

Lines changed: 70 additions & 45 deletions

File tree

Example.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require_once 'vendor/autoload.php';
33
use \RapidWeb\Postcodes\Utils\Generator;
44
use \RapidWeb\Postcodes\Utils\Validator;
5-
use \RapidWeb\Postcodes\Factories\IdealPostcodesFactory;
5+
use \RapidWeb\Postcodes\Objects\IdealPostCodes;
66

77
$postcode = Generator::generatePostcode();
88

@@ -12,12 +12,7 @@
1212

1313
if ($validated) {
1414

15-
// You should specify IDEAL_POSTCODES_API_KEY in your .env file,
16-
// or pass it via the getByAPIKey method.
17-
18-
$idealPostcodes = IdealPostcodesFactory::getByEnvironment();
19-
20-
$addresses = $idealPostcodes->getAddressesByPostcode('ST163DP');
15+
$addresses = new IdealPostCodes('API_KEY');
2116

2217
var_dump($addresses);
2318

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,22 @@ To install, just run the following composer command.
2222
Using some of the data retrieval features provided by this library requires an Ideal Postcodes
2323
API key. Sign up at https://ideal-postcodes.co.uk if you need to use these features.
2424

25-
If you are using a `.env` file or another way of manage environment variables in your
26-
project, set the following variable to specify the Ideal Postcode.
27-
28-
```
29-
IDEAL_POSTCODES_API_KEY=abcdef
30-
```
31-
3225
You can then use the following code to get an `IdealPostcodes` object.
3326

3427
```php
35-
$idealPostcodes = \RapidWeb\Postcodes\Factories\IdealPostcodesFactory::getByEnvironment();
28+
$idealPostcodes = new \RapidWeb\Postcodes\Objects\IdealPostcodes('API_KEY');
3629
```
3730

38-
If you are not using environment variables in your project, simple pass through the API
39-
key as follows.
31+
32+
### Postcode Anywhere (PCA Predict)
33+
34+
Using some of the data retrieval features provided by this library requires an Postcode Anywhere (PCA Predict)
35+
API key. Sign up at https://www.pcapredict.com/ if you need to use these features.
36+
37+
You can then use the following code to get an `PostcodeAnywhere` object.
4038

4139
```php
42-
$idealPostcodes = \RapidWeb\Postcodes\Factories\IdealPostcodesFactory::getByAPIKey('abdef');
40+
$postcodeAnywhere = new \RapidWeb\Postcodes\Objects\PostcodeAnywhere('API_KEY');
4341
```
4442

4543
## Usage

pa-test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
require_once 'vendor/autoload.php';
3+
4+
use RapidWeb\Postcodes\Objects\PostcodeAnywhere;
5+
6+
$postcodeAnywhere = new PostcodeAnywhere('API_KEY');
7+
8+
$postcodeAnywhere->getAddressesByPostcode('st163jr');

src/Factories/IdealPostcodesFactory.php

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace RapidWeb\Postcodes\Interfaces;
3+
4+
interface PostcodeServiceInterface
5+
{
6+
public function __construct($apiKey);
7+
public function getAddressesByPostcode($postcode);
8+
}

src/Objects/IdealPostcodes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
use GuzzleHttp\Client;
55
use GuzzleHttp\Psr7\Response;
6+
use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface;
67
use Exception;
78

8-
class IdealPostCodes
9+
class IdealPostCodes implements PostcodeServiceInterface
910
{
1011
private $apiKey = null;
1112
private $client = null;

src/Objects/PostcodeAnywhere.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace RapidWeb\Postcodes\Objects;
3+
4+
use GuzzleHttp\Client;
5+
use GuzzleHttp\Psr7\Response;
6+
use RapidWeb\Postcodes\Interfaces\PostcodeServiceInterface;
7+
use Exception;
8+
use SoapClient;
9+
10+
class PostcodeAnywhere implements PostcodeServiceInterface
11+
{
12+
private $apiKey = null;
13+
private $findSOAPClient = null;
14+
15+
public function __construct($apiKey)
16+
{
17+
if (!$apiKey) {
18+
throw new Exception('No Postcode Anywhere API key specified.');
19+
}
20+
21+
$this->apiKey = $apiKey;
22+
23+
$this->findSOAPClient = new SoapClient('https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/wsdlnew.ws');
24+
25+
}
26+
27+
public function getAddressesByPostcode($postcode)
28+
{
29+
$findResponse = $this->findSOAPClient->PostcodeAnywhere_Interactive_Find_v1_10(
30+
[
31+
'Key' => $this->apiKey,
32+
'SearchTerm' => $postcode
33+
]
34+
);
35+
36+
$findResponseAddresses = $findResponse->PostcodeAnywhere_Interactive_Find_v1_10_Result->PostcodeAnywhere_Interactive_Find_v1_10_Results;
37+
38+
return $result;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)