/** * Handle different forms of data to be returned in JSON format so it can be sent to Socrata. * * Data will always be transmitted as JSON to Socrata even though different forms are accepted. * * @param array|Converter|JSON $payload The data that will be upserted to the Socrata dataset as a PHP array, an * instance of a Converter child class, or a JSON string * * @return string A JSON encoded string available to be used for UrlQuery requsts */ private function handleJson($payload) { $uploadData = $payload; if (is_array($payload)) { $uploadData = json_encode($payload); } else { if ($payload instanceof Converter) { $uploadData = $payload->toJson(); } else { if (!StringUtilities::isJson($payload)) { throw new \InvalidArgumentException("The given data is not valid JSON"); } } } return $uploadData; }
/** * @dataProvider invalidJSON * * @param string $data The string being tested to check if it's null or whitespace */ public function testIsJsonWithInvalidJSON($data) { $this->assertFalse(StringUtilities::isJson($data)); }
private function handleResponseBody($body, $result) { // We somehow got a server error from Socrata without a JSON object with details if (!StringUtilities::isJson($body)) { $httpCode = curl_getinfo($this->cURL, CURLINFO_HTTP_CODE); throw new HttpException($httpCode, $result); } $resultArray = json_decode($body, true); // We got an error JSON object back from Socrata if (array_key_exists('error', $resultArray) && $resultArray['error']) { throw new SodaException($resultArray); } return $resultArray; }