1 | <?php |
||
24 | class SodaDataset |
||
25 | { |
||
26 | /** |
||
27 | * The client with all the authentication and configuration set |
||
28 | * |
||
29 | * @var SodaClient |
||
30 | */ |
||
31 | private $sodaClient; |
||
32 | |||
33 | /** |
||
34 | * The object used to make URL jobs for common requests |
||
35 | * |
||
36 | * @var UrlQuery |
||
37 | */ |
||
38 | private $urlQuery; |
||
39 | |||
40 | /** |
||
41 | * The 4x4 resource ID of a dataset |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $resourceId; |
||
46 | |||
47 | /** |
||
48 | * The API version of the dataset being worked with |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | private $apiVersion; |
||
53 | |||
54 | /** |
||
55 | * The API's cached metadata |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $metadata; |
||
60 | |||
61 | /** |
||
62 | * Create an object for interacting with a Socrata dataset |
||
63 | * |
||
64 | * @param SodaClient $sodaClient The SodaClient with all of the authentication information and settings for access |
||
65 | * @param string $resourceID The 4x4 resource ID of the dataset that will be referenced |
||
66 | * |
||
67 | * @throws InvalidResourceException If the given resource ID does not match the pattern of a resource ID |
||
68 | * |
||
69 | * @since 0.1.0 |
||
70 | */ |
||
71 | public function __construct ($sodaClient, $resourceID) |
||
87 | |||
88 | /** |
||
89 | * Get the API version this dataset is using |
||
90 | * |
||
91 | * @since 0.1.0 |
||
92 | * |
||
93 | * @return double The API version number |
||
|
|||
94 | */ |
||
95 | public function getApiVersion () |
||
109 | |||
110 | /** |
||
111 | * Get the metadata of a dataset |
||
112 | * |
||
113 | * @param bool $forceFetch Set to true if the cached metadata for the dataset is outdata or needs to be refreshed |
||
114 | * |
||
115 | * @see SodaClient::enableAssociativeArrays() |
||
116 | * @see SodaClient::disableAssociativeArrays() |
||
117 | * |
||
118 | * @since 0.1.0 |
||
119 | * |
||
120 | * @return array The metadata as a PHP array. The array will contain associative arrays or stdClass objects from |
||
121 | * the decoded JSON received from the data set. |
||
122 | */ |
||
123 | public function getMetadata ($forceFetch = false) |
||
135 | |||
136 | /** |
||
137 | * Fetch a dataset based on a resource ID. |
||
138 | * |
||
139 | * @param string|SoqlQuery $filterOrSoqlQuery A simple filter or a SoqlQuery to filter the results |
||
140 | * |
||
141 | * @see SodaClient::enableAssociativeArrays() |
||
142 | * @see SodaClient::disableAssociativeArrays() |
||
143 | * |
||
144 | * @since 0.1.0 |
||
145 | * |
||
146 | * @return array The data set as a PHP array. The array will contain associative arrays or stdClass objects from |
||
147 | * the decoded JSON received from the data set. |
||
148 | */ |
||
149 | public function getDataset ($filterOrSoqlQuery = "") |
||
164 | |||
165 | /** |
||
166 | * Delete an individual row based on their row identifier. For deleting more than a single row, use an upsert |
||
167 | * instead. |
||
168 | * |
||
169 | * @param int|string $rowID The row identifier of the row to fetch; if no identifier is set for the dataset, the |
||
170 | * internal row identifier should be used |
||
171 | * |
||
172 | * @link http://dev.socrata.com/publishers/direct-row-manipulation.html#deleting-a-row Deleting a Row |
||
173 | * |
||
174 | * @see SodaClient::enableAssociativeArrays() |
||
175 | * @see SodaClient::disableAssociativeArrays() |
||
176 | * @see upsert() |
||
177 | * |
||
178 | * @since 0.1.2 |
||
179 | * |
||
180 | * @return mixed An object with information about the deletion. The array will contain associative arrays or |
||
181 | * stdClass objects from the decoded JSON received from the data set. |
||
182 | */ |
||
183 | public function deleteRow ($rowID) |
||
187 | |||
188 | /** |
||
189 | * Fetch an individual row from a dataset. |
||
190 | * |
||
191 | * @param int|string $rowID The row identifier of the row to fetch; if no identifier is set for the dataset, the |
||
192 | * internal row identifier should be used |
||
193 | * |
||
194 | * @link http://dev.socrata.com/publishers/direct-row-manipulation.html#retrieving-an-individual-row Retrieving |
||
195 | * An Individual Row |
||
196 | * |
||
197 | * @see SodaClient::enableAssociativeArrays() |
||
198 | * @see SodaClient::disableAssociativeArrays() |
||
199 | * |
||
200 | * @since 0.1.2 |
||
201 | * |
||
202 | * @return array The data set as a PHP array. The array will contain associative arrays or stdClass objects from |
||
203 | * the decoded JSON received from the data set. |
||
204 | */ |
||
205 | public function getRow ($rowID) |
||
209 | |||
210 | /** |
||
211 | * Replace the entire dataset with the new payload provided |
||
212 | * |
||
213 | * Data will always be transmitted as JSON to Socrata even though different forms are accepted. In order to pass |
||
214 | * other forms of data, you must use a Converter class that has a `toJson()` method, such as the CsvConverter. |
||
215 | * |
||
216 | * @param array|Converter|JSON $payload The data that will be upserted to the Socrata dataset as a PHP array, an |
||
217 | * instance of a Converter child class, or a JSON string |
||
218 | * |
||
219 | * @link http://dev.socrata.com/publishers/replace.html Replacing a dataset with Replace |
||
220 | * |
||
221 | * @see Converter |
||
222 | * @see CsvConverter |
||
223 | * |
||
224 | * @since 0.1.0 |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | public function replace ($payload) |
||
234 | |||
235 | /** |
||
236 | * Create, update, and delete rows in a single operation, using their row identifiers. |
||
237 | * |
||
238 | * Data will always be transmitted as JSON to Socrata even though different forms are accepted. In order to pass |
||
239 | * other forms of data, you must use a Converter class that has a `toJson()` method, such as the CsvConverter. |
||
240 | * |
||
241 | * @param array|Converter|JSON $payload The data that will be upserted to the Socrata dataset as a PHP array, an |
||
242 | * instance of a Converter child class, or a JSON string |
||
243 | * |
||
244 | * @link http://dev.socrata.com/publishers/upsert.html Updating Rows in Bulk with Upsert |
||
245 | * |
||
246 | * @see Converter |
||
247 | * @see CsvConverter |
||
248 | * |
||
249 | * @since 0.1.0 |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | public function upsert ($payload) |
||
259 | |||
260 | /** |
||
261 | * Build the API URL that will be used to access the dataset |
||
262 | * |
||
263 | * @return string The apt API URL |
||
264 | */ |
||
265 | private function buildResourceUrl () |
||
269 | |||
270 | /** |
||
271 | * Build the API URL that will be used to access the metadata for the dataset |
||
272 | * |
||
273 | * @return string The apt API URL |
||
274 | */ |
||
275 | private function buildViewUrl () |
||
279 | |||
280 | /** |
||
281 | * Build the URL that will be used to access the API for the respective action |
||
282 | * |
||
283 | * @param string $location The location of where to get information from |
||
284 | * @param string|null $identifier The part of the URL that will end with .json. This will either be the resource |
||
285 | * ID or it will be a row ID prepended with the resource ID |
||
286 | * |
||
287 | * @return string The API URL |
||
288 | */ |
||
289 | private function buildApiUrl ($location, $identifier = NULL) |
||
298 | |||
299 | /** |
||
300 | * Handle different forms of data to be returned in JSON format so it can be sent to Socrata. |
||
301 | * |
||
302 | * Data will always be transmitted as JSON to Socrata even though different forms are accepted. |
||
303 | * |
||
304 | * @param array|Converter|JSON $payload The data that will be upserted to the Socrata dataset as a PHP array, an |
||
305 | * instance of a Converter child class, or a JSON string |
||
306 | * |
||
307 | * @return string A JSON encoded string available to be used for UrlQuery requsts |
||
308 | */ |
||
309 | private function handleJson ($payload) |
||
328 | |||
329 | /** |
||
330 | * Interact with an individual row. Either to retrieve it or to delete it; both actions use the same API endpoint |
||
331 | * with the exception of what type of request is sent. |
||
332 | * |
||
333 | * @param string $rowID The 4x4 resource ID of the dataset to work with |
||
334 | * @param string $method Either `get` or `delete` |
||
335 | * |
||
336 | * @return mixed |
||
337 | */ |
||
338 | private function individualRow ($rowID, $method) |
||
354 | |||
355 | /** |
||
356 | * Send the appropriate request header based on the method that's required |
||
357 | * |
||
358 | * @param UrlQuery $urlQuery The object for the API endpoint |
||
359 | * @param string $method Either `get` or `delete` |
||
360 | * @param bool $associativeArrays Whether or not to return the information as an associative array |
||
361 | * @param array $headers An array with the cURL headers received |
||
362 | * |
||
363 | * @return mixed |
||
364 | */ |
||
365 | private function sendIndividualRequest ($urlQuery, $method, $associativeArrays, &$headers) |
||
378 | |||
379 | /** |
||
380 | * Determine and save the API version if it does not exist for easy access later |
||
381 | * |
||
382 | * @param array $headers An array with the cURL headers received |
||
383 | */ |
||
384 | private function setApiVersion ($headers) |
||
392 | |||
393 | /** |
||
394 | * Determine the version number of the API this dataset is using |
||
395 | * |
||
396 | * @param array $responseHeaders An array with the cURL headers received |
||
397 | * |
||
398 | * @return double The Socrata API version number this dataset uses |
||
399 | */ |
||
400 | private function parseApiVersion ($responseHeaders) |
||
426 | } |
||
427 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.