1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file contains the SodaClient class |
5
|
|
|
* |
6
|
|
|
* @copyright 2015 Vladimir Jimenez |
7
|
|
|
* @license https://github.com/allejo/PhpSoda/blob/master/LICENSE.md MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace allejo\Socrata; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* An object provided to handle tokens, authentication, and configuration for interacting with the the Socrata API. |
14
|
|
|
* |
15
|
|
|
* @package allejo\Socrata |
16
|
|
|
* @since 0.1.0 |
17
|
|
|
*/ |
18
|
|
|
class SodaClient |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The URL or domain of where the data set will be received from |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $domain; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The AppToken used to read private data and to allow the application to work with less throttling |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $token; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The user email for the account that will be adding, deleting, or modifying data |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $email; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The password for said account |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $password; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The OAuth 2.0 Access Token to be used with requests |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $oAuth2Token; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether or not to return the decoded JSON as an associative array. When set to false, it will return stdClass |
57
|
|
|
* objects |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
private $associativeArray; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a client object to connect to the Socrata API |
65
|
|
|
* |
66
|
|
|
* @param string $url The URL or domain of the Socrata data set |
67
|
|
|
* @param string $token The AppToken used to access this information |
68
|
|
|
* @param string $email Username for authentication |
69
|
|
|
* @param string $password Password for authentication |
70
|
|
|
* |
71
|
|
|
* @since 0.1.0 |
72
|
|
|
*/ |
73
|
|
|
public function __construct ($url, $token = "", $email = "", $password = "") |
74
|
|
|
{ |
75
|
|
|
$this->domain = rtrim(preg_replace('/http(s)?:\/\//', "", $url), '/'); |
76
|
|
|
$this->token = $token; |
77
|
|
|
$this->email = $email; |
78
|
|
|
$this->password = $password; |
79
|
|
|
$this->oAuth2Token = ""; |
80
|
|
|
$this->associativeArray = true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* When fetching a data set, the returned data will be in an array of associative arrays. |
85
|
|
|
* |
86
|
|
|
* ``` |
87
|
|
|
* Array |
88
|
|
|
* ( |
89
|
|
|
* [foo] => Test data |
90
|
|
|
* [bar] => Array |
91
|
|
|
* ( |
92
|
|
|
* [baaz] => Testing |
93
|
|
|
* [fooz] => Array |
94
|
|
|
* ( |
95
|
|
|
* [baz] => Testing again |
96
|
|
|
* ) |
97
|
|
|
* |
98
|
|
|
* ) |
99
|
|
|
* [foox] => Just test |
100
|
|
|
* ) |
101
|
|
|
* ``` |
102
|
|
|
* |
103
|
|
|
* When returned in this format, all of the children elements are array elements and are accessed as such. |
104
|
|
|
* |
105
|
|
|
* ```php |
106
|
|
|
* $myVariable = $results['bar']['baz']; // Testing |
107
|
|
|
* ``` |
108
|
|
|
* |
109
|
|
|
* @since 0.1.0 |
110
|
|
|
*/ |
111
|
|
|
public function enableAssociativeArrays () |
112
|
|
|
{ |
113
|
|
|
$this->associativeArray = true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* When fetching a data set, the returned data will be in an array of stdClass objects. When AssociativeArrays is |
118
|
|
|
* disabled, the returned data will in the follow format: |
119
|
|
|
* |
120
|
|
|
* ``` |
121
|
|
|
* stdClass Object |
122
|
|
|
* ( |
123
|
|
|
* [foo] => Test data |
124
|
|
|
* [bar] => stdClass Object |
125
|
|
|
* ( |
126
|
|
|
* [baz] => Testing |
127
|
|
|
* [foz] => stdClass Object |
128
|
|
|
* ( |
129
|
|
|
* [baz] => Testing again |
130
|
|
|
* ) |
131
|
|
|
* ) |
132
|
|
|
* [fox] => Just test |
133
|
|
|
* ) |
134
|
|
|
* ``` |
135
|
|
|
* |
136
|
|
|
* When returned in this format, all of the children elements are objects and are accessed as such. |
137
|
|
|
* |
138
|
|
|
* ```php |
139
|
|
|
* $myVariable = $results->bar->baz; // Testing |
140
|
|
|
* ``` |
141
|
|
|
* |
142
|
|
|
* @since 0.1.0 |
143
|
|
|
*/ |
144
|
|
|
public function disableAssociativeArrays () |
145
|
|
|
{ |
146
|
|
|
$this->associativeArray = false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get whether or not the returned data should be associative arrays or as stdClass objects |
151
|
|
|
* |
152
|
|
|
* @since 0.1.0 |
153
|
|
|
* |
154
|
|
|
* @return bool True if the data is returned as associative arrays |
155
|
|
|
*/ |
156
|
|
|
public function associativeArrayEnabled () |
157
|
|
|
{ |
158
|
|
|
return $this->associativeArray; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the domain of the API endpoint. This function will **always** return just the domain without the protocol |
163
|
|
|
* in order to let this library use the appropriate protocol |
164
|
|
|
* |
165
|
|
|
* @since 0.1.0 |
166
|
|
|
* |
167
|
|
|
* @return string The domain of the API endpoint |
168
|
|
|
*/ |
169
|
|
|
public function getDomain () |
170
|
|
|
{ |
171
|
|
|
return $this->domain; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the email of the account that will be used for authenticated actions |
176
|
|
|
* |
177
|
|
|
* @since 0.1.0 |
178
|
|
|
* |
179
|
|
|
* @return string The user's email address. Returns an empty string if not set. |
180
|
|
|
*/ |
181
|
|
|
public function getEmail () |
182
|
|
|
{ |
183
|
|
|
return $this->email; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the app token used by the library to bypass throttling and appear as a registered application |
188
|
|
|
* |
189
|
|
|
* @since 0.1.0 |
190
|
|
|
* |
191
|
|
|
* @return string The app token used. Returns an empty string if not set. |
192
|
|
|
*/ |
193
|
|
|
public function getToken () |
194
|
|
|
{ |
195
|
|
|
return $this->token; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the password of the account that will be used for authenticated actions |
200
|
|
|
* |
201
|
|
|
* @since 0.1.0 |
202
|
|
|
* |
203
|
|
|
* @return string The password used. Returns an empty string if not set. |
204
|
|
|
*/ |
205
|
|
|
public function getPassword () |
206
|
|
|
{ |
207
|
|
|
return $this->password; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the access token being used for OAuth 2.0 |
212
|
|
|
* |
213
|
|
|
* @return string The access token being used |
214
|
|
|
* |
215
|
|
|
* @since 0.1.1 |
216
|
|
|
*/ |
217
|
|
|
public function getOAuth2Token () |
218
|
|
|
{ |
219
|
|
|
return $this->oAuth2Token; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set the OAuth 2.0 access token |
224
|
|
|
* |
225
|
|
|
* @param string $token The access token to be used in queries |
226
|
|
|
* |
227
|
|
|
* @since 0.1.1 |
228
|
|
|
*/ |
229
|
|
|
public function setOAuth2Token ($token) |
230
|
|
|
{ |
231
|
|
|
$this->oAuth2Token = $token; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|