diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/PurpleCode/PCurl/Json/PJsonCurl.php | 57 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/Json/PJsonCurlResponse.php | 28 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/Object/PObjectCurl.php (renamed from src/PurpleCode/PCurl/PObjectCurl.php) | 15 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/Object/PObjectCurlResponse.php | 28 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurl.php | 11 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlException.php | 7 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlJsonResponse.php | 32 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlResponse.php | 27 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PJsonCurl.php | 37 |
9 files changed, 156 insertions, 86 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; + } + +} diff --git a/src/PurpleCode/PCurl/PObjectCurl.php b/src/PurpleCode/PCurl/Object/PObjectCurl.php index 10c465d..e215919 100644 --- a/src/PurpleCode/PCurl/PObjectCurl.php +++ b/src/PurpleCode/PCurl/Object/PObjectCurl.php @@ -9,10 +9,12 @@ * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
*/
-namespace PurpleCode\PCurl;
+namespace PurpleCode\PCurl\Object;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\SerializerInterface;
+use PurpleCode\PCurl\PCurl;
+use PurpleCode\PCurl\PCurlException;
class PObjectCurl extends PCurl {
@@ -33,11 +35,14 @@ class PObjectCurl extends PCurl { $this->responseClass = $class;
}
+ /**
+ * @return PObjectCurlResponse
+ */
public function call($method, $url, $payload = '') {
$payload = $this->serialize($payload);
$response = parent::call($method, $url, $payload);
- $response->setBody($this->deserialize($response->getBody()));
- return $response;
+ $object = $this->deserialize($response->getBody());
+ return new PObjectCurlResponse($response->getHeader(), $response->getBody(), $response->getHttpCode(), $object);
}
private function serialize($payload) {
@@ -49,10 +54,12 @@ class PObjectCurl extends PCurl { }
private function deserialize($response) {
+ if (empty($this->responseClass)) {
+ return $response;
+ }
try {
return $this->responseClass ? $this->serializer->deserialize($response, $this->responseClass, 'json') : $response;
} catch (RuntimeException $e) {
- var_dump($response);
throw new PCurlException(sprintf('Unable to deserialize response - %s : %s', $e->getMessage(), $response));
}
}
diff --git a/src/PurpleCode/PCurl/Object/PObjectCurlResponse.php b/src/PurpleCode/PCurl/Object/PObjectCurlResponse.php new file mode 100644 index 0000000..ceee743 --- /dev/null +++ b/src/PurpleCode/PCurl/Object/PObjectCurlResponse.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\Object;
+
+use PurpleCode\PCurl\PCurlResponse;
+
+class PObjectCurlResponse extends PCurlResponse {
+
+ public function __construct($header, $body, $httpCode, $object) {
+ parent::__construct($header, $body, $httpCode);
+
+ $this->object = $object;
+ }
+
+ public function getObject() {
+ return $this->object;
+ }
+
+}
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php index bd0e5f4..cd442d0 100644 --- a/src/PurpleCode/PCurl/PCurl.php +++ b/src/PurpleCode/PCurl/PCurl.php @@ -104,13 +104,15 @@ class PCurl { throw new PCurlException($error);
}
- $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
+ $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
+ $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+
curl_close($curl);
- $header = substr($response, 0, $header_size);
- $body = substr($response, $header_size);
+ $header = substr($response, 0, $headerSize);
+ $body = substr($response, $headerSize);
- return new PCurlResponse($header, $body);
+ return new PCurlResponse($header, $body, $httpCode);
}
/**
@@ -179,6 +181,7 @@ class PCurl { */
public function ignoreSSLCertificate($bool = true) {
$this->setOption(CURLOPT_SSL_VERIFYPEER, !$bool);
+ $this->setOption(CURLOPT_SSL_VERIFYHOST, 0);
return $this;
}
diff --git a/src/PurpleCode/PCurl/PCurlException.php b/src/PurpleCode/PCurl/PCurlException.php index abed40a..0e06e6d 100644 --- a/src/PurpleCode/PCurl/PCurlException.php +++ b/src/PurpleCode/PCurl/PCurlException.php @@ -1,4 +1,5 @@ <?php
+
/**
* PCurl is a REST client libary for PHP.
*
@@ -18,7 +19,7 @@ class PCurlException extends \Exception { public static function assert($condition, $message, $arguments = array(), $code = 400) {
if (!$condition) {
- $arguments = array_map('json_encode', ArrayUtils::ensureArray($arguments));
+ $arguments = array_map('json_encode', self::ensureArray($arguments));
$arguments = array_merge(array($message), $arguments);
$message = call_user_func_array('sprintf', $arguments);
$class = self::getClassName();
@@ -30,11 +31,11 @@ class PCurlException extends \Exception { parent::__construct($message, $code, $previous);
}
- public function ensureArray($item) {
+ private static function ensureArray($item) {
if (is_null($item)) {
return array();
}
return is_array($item) ? $item : array($item);
}
-}
\ No newline at end of file +}
diff --git a/src/PurpleCode/PCurl/PCurlJsonResponse.php b/src/PurpleCode/PCurl/PCurlJsonResponse.php deleted file mode 100644 index df36ea4..0000000 --- a/src/PurpleCode/PCurl/PCurlJsonResponse.php +++ /dev/null @@ -1,32 +0,0 @@ -<?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 index d616a95..2f249ee 100644 --- a/src/PurpleCode/PCurl/PCurlResponse.php +++ b/src/PurpleCode/PCurl/PCurlResponse.php @@ -11,29 +11,44 @@ namespace PurpleCode\PCurl; - class PCurlResponse { + private $body; private $header; - public function __construct($header, $body){ + public function __construct($header, $body, $httpCode) { $this->header = $header; $this->body = $body; + $this->httpCode = $httpCode; } - public function setBody($body){ + public function setBody($body) { $this->body = $body; } - public function getBody(){ + public function getBody() { return $this->body; } - public function setHeader($header){ + public function setHeader($header) { $this->header = $header; } - public function getHeader(){ + public function getHeader() { return $this->header; } + + public function setHttpCode($httpCode) { + $this->httpCode = $httpCode; + } + + public function getHttpCode() { + return $this->httpCode; + } + + public function assertSuccess() { + PCurlException::assert($this->getHttpCode() == 200, "Request failed (" . $this->getHttpCode() . ")"); + return $this; + } + } diff --git a/src/PurpleCode/PCurl/PJsonCurl.php b/src/PurpleCode/PCurl/PJsonCurl.php deleted file mode 100644 index aad8bd4..0000000 --- a/src/PurpleCode/PCurl/PJsonCurl.php +++ /dev/null @@ -1,37 +0,0 @@ -<?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\PCurlJsonResponse;
-
-class PJsonCurl extends PCurl {
-
- private $arrayResponse;
-
- public function __construct($host) {
- parent::__construct($host);
-
- $this->arrayResponse = false;
- $this->contentTypeJson();
- }
-
- public function call($method, $url, $payload = '') {
- $payload = json_encode($payload);
- $response = parent::call($method, $url, $payload);
- return new PCurlJsonResponse($response->getHeader(), $response->getBody(), $this->arrayResponse);
- }
-
- public function arrayResponse($arrayResponse = true) {
- $this->arrayResponse = $arrayResponse;
- }
-
-}
|