summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode/PCurl/Json
diff options
context:
space:
mode:
Diffstat (limited to 'src/PurpleCode/PCurl/Json')
-rw-r--r--src/PurpleCode/PCurl/Json/PJsonCurl.php57
-rw-r--r--src/PurpleCode/PCurl/Json/PJsonCurlResponse.php28
2 files changed, 85 insertions, 0 deletions
diff --git a/src/PurpleCode/PCurl/Json/PJsonCurl.php b/src/PurpleCode/PCurl/Json/PJsonCurl.php
new file mode 100644
index 0000000..60f151b
--- /dev/null
+++ b/src/PurpleCode/PCurl/Json/PJsonCurl.php
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * PCurl is a REST client libary for PHP.
+ *
+ * See http://github.com/purplecode/php.curl for details.
+ *
+ * This code is licensed for use, modification, and distribution
+ * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
+ */
+
+namespace PurpleCode\PCurl\Json;
+
+use PurpleCode\PCurl\Json\PJsonCurlResponse;
+use PurpleCode\PCurl\PCurl;
+use PurpleCode\PCurl\PCurlException;
+
+class PJsonCurl extends PCurl {
+
+ public function __construct($host) {
+ parent::__construct($host);
+
+ $this->deserializeToArray = false;
+ $this->contentTypeJson();
+ }
+
+ public function deserializeToArray($bool = true) {
+ $this->deserializeToArray = $bool;
+ }
+
+ /**
+ * @return PJsonCurlResponse
+ */
+ public function call($method, $url, $payload = '') {
+ $payload = json_encode($payload);
+ $response = parent::call($method, $url, $payload);
+ $body = $response->getBody();
+
+ return new PJsonCurlResponse($response->getHeader(), $body, $response->getHttpCode(), $this->deserialize($body));
+ }
+
+ private function deserialize($response) {
+ $json = json_decode($response, $this->deserializeToArray);
+
+ PCurlException::assert($this->isValidJson($response) && json_last_error() == JSON_ERROR_NONE, "Invalid JSON response format");
+
+ return $json;
+ }
+
+ /**
+ * Fast way to check if response body is in JSon format - see RFC4627, regexp part.
+ */
+ private function isValidJson($text) {
+ return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $text));
+ }
+
+}
diff --git a/src/PurpleCode/PCurl/Json/PJsonCurlResponse.php b/src/PurpleCode/PCurl/Json/PJsonCurlResponse.php
new file mode 100644
index 0000000..459e2b8
--- /dev/null
+++ b/src/PurpleCode/PCurl/Json/PJsonCurlResponse.php
@@ -0,0 +1,28 @@
+<?php
+
+/**
+ * PCurl is a REST client libary for PHP.
+ *
+ * See http://github.com/purplecode/php.curl for details.
+ *
+ * This code is licensed for use, modification, and distribution
+ * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
+ */
+
+namespace PurpleCode\PCurl\Json;
+
+use PurpleCode\PCurl\PCurlResponse;
+
+class PJsonCurlResponse extends PCurlResponse {
+
+ public function __construct($header, $body, $httpCode, $json) {
+ parent::__construct($header, $body, $httpCode);
+
+ $this->json = $json;
+ }
+
+ public function getJson() {
+ return $this->json;
+ }
+
+}