1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Unicodeveloper\Ngsc; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class Ngsc { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Instance of GuzzleHttp |
11
|
|
|
* @var object |
12
|
|
|
*/ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Data response |
17
|
|
|
* @var object |
18
|
|
|
*/ |
19
|
|
|
protected $response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Devcenter States and Cities API |
23
|
|
|
* Source: http://states-cities.devcenter.co/ |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $baseUrl = 'http://states-cities.devcenter.co/api/v1'; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->setRequestOptions(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set options for making the Client request |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
private function setRequestOptions() |
38
|
|
|
{ |
39
|
|
|
$this->client = new Client(['base_uri' => $this->baseUrl]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Make the client request and get the response |
44
|
|
|
* @param string $relativeUrl |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function setResponse($relativeUrl) |
48
|
|
|
{ |
49
|
|
|
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get an associative array of all the states and their cities |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getAllStates() |
57
|
|
|
{ |
58
|
|
|
$this->setResponse('/states'); |
59
|
|
|
|
60
|
|
|
return $this->data(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the details of a state |
65
|
|
|
* @param string $state |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getOneState($state) |
69
|
|
|
{ |
70
|
|
|
$this->setResponse("/state/{$state}"); |
71
|
|
|
|
72
|
|
|
return $this->data(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the local government areas of a particular state |
77
|
|
|
* @param string $state |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getLGAS($state) |
81
|
|
|
{ |
82
|
|
|
$this->setResponse("/state/{$state}/lgas"); |
83
|
|
|
|
84
|
|
|
return $this->data(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get all the cities of a particular state |
89
|
|
|
* @param string $state |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getCities($state) |
93
|
|
|
{ |
94
|
|
|
$this->setResponse("/state/{$state}/cities"); |
95
|
|
|
|
96
|
|
|
return $this->data(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the details of the required request |
101
|
|
|
* @return object |
102
|
|
|
*/ |
103
|
|
|
private function data() |
104
|
|
|
{ |
105
|
|
|
$result = json_decode($this->response->getBody()); |
106
|
|
|
|
107
|
|
|
$simplifiedResult = []; |
108
|
|
|
|
109
|
|
|
if (is_array($result)) { |
110
|
|
|
foreach ($result as $key => $value) { |
111
|
|
|
$simplifiedResult[$key] = (array)$value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $simplifiedResult; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return (array)$result; |
118
|
|
|
} |
119
|
|
|
} |