-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostcodeApi.php
More file actions
156 lines (137 loc) · 4.8 KB
/
postcodeApi.php
File metadata and controls
156 lines (137 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
class PostcodeAPI {
// Properties
private $apiUrl = 'api.postcodes.io';
private $postcodes;
private $endPoint;
private $postData;
private $result;
private $addresses = array();
function __construct() {
}
// Methods
/**
* runs postcode lookup depending on number of postcodes
* */
function dynamicPostcodeLookup(){
if (count($this->get_postcodes()) > 1){
$this->bulkPostCodeLookup();
return;
}
$this->singlePostcodeLookup();
}
/**
* single postcode lookup uses the following endpoint GET api.postcodes.io/postcodes/
* */
function singlePostcodeLookup(){
$this->set_endPoint('/postcodes/' . $this->get_postcodes()[0]);
$this->runApi('GET');
$this->buildAddressFromResult();
}
/**
* bulk postcode lookup uses the following endpoint POST api.postcodes.io/postcodes
* */
function bulkPostCodeLookup(){
$this->set_endPoint('/postcodes');
$this->set_postData(json_encode(array('postcodes' => $this->postcodes)));
$this->runApi('POST');
$this->buildAddressesFromResult();
}
/**
* builds an address object from the single postcode lookup response
* */
function buildAddressFromResult(){
$phpResult = json_decode($this->get_result(), 1);
$address = new Address();
$phpResult['result'] = ($phpResult['result']) ?? '';
$address->buildAddressFromPostcodeApi($phpResult['result'], $this->postcodes[0]);
$this->pushToAddresses($address);
}
/**
* builds address objects from the bulk postcode lookup response
* */
function buildAddressesFromResult(){
$phpResult = json_decode($this->get_result(), 1);
if (isset($phpResult['result'])){
foreach($phpResult['result'] as $fullAddress){
$address = new Address();
$address->buildAddressFromPostcodeApi($fullAddress['result'], $fullAddress['query']);
$this->pushToAddresses($address);
}
}
}
/**
* runs API calls and sets the result
* */
function runApi($restCommand){
$curl = curl_init();
switch($restCommand){
case 'POST':
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->get_postData());
break;
}
curl_setopt($curl, CURLOPT_URL, $this->get_apiUrl() . $this->get_endPoint());
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$this->set_result($result);
}
/**
* adds postcodes to an array.
* Removes preceeding and trailing white space from all entries
* */
function formatPostcodes($postcodes){
$postcodeArray = explode(",", $postcodes);
foreach($postcodeArray as &$postcode){
$postcode = trim($postcode);
}
return $postcodeArray;
}
/**
* pushs a new address to the addresses array
* */
function pushToAddresses($address){
$addressArray = $this->get_addresses();
$addressArray[] = $address;
$this->set_addresses($addressArray);
}
//Getters & Setters
function set_apiUrl($url) {
$this->apiUrl = $url;
}
function get_apiUrl() {
return $this->apiUrl;
}
function set_endPoint($endPoint) {
$this->endPoint = $endPoint;
}
function get_endPoint() {
return $this->endPoint;
}
function set_postData($postData) {
$this->postData = $postData;
}
function get_postData() {
return $this->postData;
}
function set_result($result) {
$this->result = $result;
}
function get_result() {
return $this->result;
}
function set_postcodes($postcodes) {
$this->postcodes = $postcodes;
}
function get_postcodes() {
return $this->postcodes;
}
function set_addresses($addresses) {
$this->addresses = $addresses;
}
function get_addresses() {
return $this->addresses;
}
}