summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortheres <dmarkiew@CNU3429M9W.nsn-intra.net>2014-07-15 09:44:07 +0200
committertheres <dmarkiew@CNU3429M9W.nsn-intra.net>2014-07-15 09:44:07 +0200
commit090e64bd20a40ed55c79025e611ba2c6a45c80df (patch)
treeb9a284b086b69e057386e11333d7ff15a130834d /src
parent1884e50d534f9ca82527d06861cb7064fe1d9b40 (diff)
downloadphp.curl-090e64bd20a40ed55c79025e611ba2c6a45c80df.zip
php.curl-090e64bd20a40ed55c79025e611ba2c6a45c80df.tar.gz
php.curl-090e64bd20a40ed55c79025e611ba2c6a45c80df.tar.bz2
PCurl will now return PCurlResponse object instead of raw body. PJsonCurl
also, but will try to parse JSON and throw an exception when fail to parse.
Diffstat (limited to 'src')
-rw-r--r--src/PurpleCode/PCurl/PCurl.php15
-rw-r--r--src/PurpleCode/PCurl/PCurlJsonResponse.php32
-rw-r--r--src/PurpleCode/PCurl/PCurlResponse.php39
-rw-r--r--src/PurpleCode/PCurl/PJsonCurl.php20
-rw-r--r--src/PurpleCode/PCurl/PObjectCurl.php8
5 files changed, 81 insertions, 33 deletions
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php
index 9af612c..4e9d4b6 100644
--- a/src/PurpleCode/PCurl/PCurl.php
+++ b/src/PurpleCode/PCurl/PCurl.php
@@ -12,8 +12,10 @@
namespace PurpleCode\PCurl;
require_once 'PCurlException.php';
+require_once 'PCurlResponse.php';
use PurpleCode\PCurl\PCurlException;
+use PurpleCode\PCurl\PCurlResponse;
class PCurl {
@@ -110,9 +112,7 @@ class PCurl {
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
- $processedResponse = $this->postProcessResponseBody($header, $body);
-
- return $body;
+ return new PCurlResponse($header, $body);
}
/**
@@ -145,15 +145,6 @@ class PCurl {
}
/**
- * Allow to postprocess response body, ex. parse to specified format.
- * @param string $header
- * @param string $body
- */
- protected function postProcessResponseBody($header, $body){
- return $body;
- }
-
- /**
* @return PCurl
*/
public function headers(array $headers) {
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));
+ }
+
+}
diff --git a/src/PurpleCode/PCurl/PCurlResponse.php b/src/PurpleCode/PCurl/PCurlResponse.php
new file mode 100644
index 0000000..d616a95
--- /dev/null
+++ b/src/PurpleCode/PCurl/PCurlResponse.php
@@ -0,0 +1,39 @@
+<?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;
+
+
+class PCurlResponse {
+ private $body;
+ private $header;
+
+ public function __construct($header, $body){
+ $this->header = $header;
+ $this->body = $body;
+ }
+
+ public function setBody($body){
+ $this->body = $body;
+ }
+
+ public function getBody(){
+ return $this->body;
+ }
+
+ public function setHeader($header){
+ $this->header = $header;
+ }
+
+ public function getHeader(){
+ return $this->header;
+ }
+}
diff --git a/src/PurpleCode/PCurl/PJsonCurl.php b/src/PurpleCode/PCurl/PJsonCurl.php
index 30776a0..aad8bd4 100644
--- a/src/PurpleCode/PCurl/PJsonCurl.php
+++ b/src/PurpleCode/PCurl/PJsonCurl.php
@@ -11,39 +11,27 @@
namespace PurpleCode\PCurl;
+use PurpleCode\PCurl\PCurlJsonResponse;
+
class PJsonCurl extends PCurl {
private $arrayResponse;
- private $alwaysParseResponse;
public function __construct($host) {
parent::__construct($host);
$this->arrayResponse = false;
- $this->alwaysParseResponse = true;
$this->contentTypeJson();
}
public function call($method, $url, $payload = '') {
$payload = json_encode($payload);
- return parent::call($method, $url, $payload);
- }
-
- protected function postProcessResponseBody($body, $header) {
- $body = parent::postProcessResponseBody($body, $header);
- if ($this->alwaysParseResponse || strpos($header,'application/json')!==false) {
- return json_decode($body, $this->arrayResponse);
- }
-
- return $body;
+ $response = parent::call($method, $url, $payload);
+ return new PCurlJsonResponse($response->getHeader(), $response->getBody(), $this->arrayResponse);
}
public function arrayResponse($arrayResponse = true) {
$this->arrayResponse = $arrayResponse;
}
- public function alwaysParseResponse($alwaysParseResponse = true) {
- $this->alwaysParseResponse = $alwaysParseResponse;
- }
-
}
diff --git a/src/PurpleCode/PCurl/PObjectCurl.php b/src/PurpleCode/PCurl/PObjectCurl.php
index 238bf11..5fdca29 100644
--- a/src/PurpleCode/PCurl/PObjectCurl.php
+++ b/src/PurpleCode/PCurl/PObjectCurl.php
@@ -35,11 +35,9 @@ class PObjectCurl extends PCurl {
public function call($method, $url, $payload = '') {
$payload = $this->serialize($payload);
- return parent::call($method, $url, $payload);
- }
-
- protected function postProcessResponseBody($body, $header) {
- return $this->deserialize($body);
+ $response = parent::call($method, $url, $payload);
+ $response->setBody($this->deserialize($response));
+ return $response;
}
private function serialize($payload) {