summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode/PCurl/PCurlJsonResponse.php
diff options
context:
space:
mode:
authorMateusz Jaworski <niespammnie@gmail.com>2014-07-15 09:51:56 +0200
committerMateusz Jaworski <niespammnie@gmail.com>2014-07-15 09:51:56 +0200
commit2ebe98e90e172b2c824f4263b9367346392227c5 (patch)
treeb9a284b086b69e057386e11333d7ff15a130834d /src/PurpleCode/PCurl/PCurlJsonResponse.php
parent0e0aa739ec43eb6146bd483b0edd12b5928d93ee (diff)
parent090e64bd20a40ed55c79025e611ba2c6a45c80df (diff)
downloadphp.curl-2ebe98e90e172b2c824f4263b9367346392227c5.zip
php.curl-2ebe98e90e172b2c824f4263b9367346392227c5.tar.gz
php.curl-2ebe98e90e172b2c824f4263b9367346392227c5.tar.bz2
Merge pull request #1 from theres/master
PCurlResponse with body & headers
Diffstat (limited to 'src/PurpleCode/PCurl/PCurlJsonResponse.php')
-rw-r--r--src/PurpleCode/PCurl/PCurlJsonResponse.php32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/PurpleCode/PCurl/PCurlJsonResponse.php b/src/PurpleCode/PCurl/PCurlJsonResponse.php
new file mode 100644
index 0000000..df36ea4
--- /dev/null
+++ b/src/PurpleCode/PCurl/PCurlJsonResponse.php
@@ -0,0 +1,32 @@
+<?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;
+
+use PurpleCode\PCurl\PCurlResponse;
+
+class PCurlJsonResponse extends PCurlResponse {
+ private $parsedResponse;
+
+ public function __construct($header, $body, $arrayResponse){
+ if(! PCurlJsonResponse::isValidJson($body))
+ throw new PCurlException("Invalid JSON response format");
+ parent::__construct($header, json_decode($body, $arrayResponse));
+ }
+
+ /**
+ * Fast way to check if response body is in JSon format - see RFC4627, regexp part.
+ */
+ private static function isValidJson($text){
+ return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $text));
+ }
+
+}