summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode
diff options
context:
space:
mode:
authorpurplecode <niespammnie@gmail.com>2013-12-19 22:18:04 +0100
committerpurplecode <niespammnie@gmail.com>2013-12-19 22:18:04 +0100
commit82dea3bc4dc4c516723f4b99bc863d3b01ff2840 (patch)
tree9971f1e0ede4a1f8fbfb9a31a8dd82a6347a6e88 /src/PurpleCode
parent64edc0e6d5e146dbe56b846677b2e2ff31eb371f (diff)
downloadphp.curl-82dea3bc4dc4c516723f4b99bc863d3b01ff2840.zip
php.curl-82dea3bc4dc4c516723f4b99bc863d3b01ff2840.tar.gz
php.curl-82dea3bc4dc4c516723f4b99bc863d3b01ff2840.tar.bz2
PObjectCurl improvements
Diffstat (limited to 'src/PurpleCode')
-rw-r--r--src/PurpleCode/PCurl/PCurl.php3
-rw-r--r--src/PurpleCode/PCurl/PJsonCurl.php6
-rw-r--r--src/PurpleCode/PCurl/PObjectCurl.php25
3 files changed, 28 insertions, 6 deletions
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php
index d3560b6..09d1b25 100644
--- a/src/PurpleCode/PCurl/PCurl.php
+++ b/src/PurpleCode/PCurl/PCurl.php
@@ -193,9 +193,6 @@ class PCurl {
return $this;
}
- /**
- * @return PCurl
- */
public function getOption($optionKey) {
return $this->options[$optionKey];
}
diff --git a/src/PurpleCode/PCurl/PJsonCurl.php b/src/PurpleCode/PCurl/PJsonCurl.php
index e8d813c..b1cd642 100644
--- a/src/PurpleCode/PCurl/PJsonCurl.php
+++ b/src/PurpleCode/PCurl/PJsonCurl.php
@@ -13,6 +13,12 @@ namespace PurpleCode\PCurl;
class PJsonCurl extends PCurl {
+ public function __construct($host) {
+ parent::__construct($host);
+
+ $this->contentTypeJson();
+ }
+
public function call($method, $url, $payload = '') {
return json_decode(parent::call($method, $url, json_encode($payload)));
}
diff --git a/src/PurpleCode/PCurl/PObjectCurl.php b/src/PurpleCode/PCurl/PObjectCurl.php
index d874486..3a2f09c 100644
--- a/src/PurpleCode/PCurl/PObjectCurl.php
+++ b/src/PurpleCode/PCurl/PObjectCurl.php
@@ -11,6 +11,7 @@
namespace PurpleCode\PCurl;
+use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\SerializerInterface;
class PObjectCurl extends PCurl {
@@ -24,7 +25,7 @@ class PObjectCurl extends PCurl {
public function __construct($host, SerializerInterface $serializer) {
parent::__construct($host);
$this->serializer = $serializer;
-
+
$this->contentTypeJson();
}
@@ -33,8 +34,26 @@ class PObjectCurl extends PCurl {
}
public function call($method, $url, $payload = '') {
- $response = parent::call($method, $url, empty($payload) ? $payload : $this->serializer->serialize($payload, 'json'));
- return $this->responseClass ? $this->serializer->deserialize($response, $this->responseClass, 'json') : $response;
+ $payload = $this->serialize($payload);
+ $response = parent::call($method, $url, $payload);
+ return $this->deserialize($response);
+ }
+
+ private function serialize($payload) {
+ try {
+ return empty($payload) ? $payload : $this->serializer->serialize($payload, 'json');
+ } catch (RuntimeException $e) {
+ throw new PCurlException(sprintf('Unable to serialize payload - %s : %s', $e->getMessage(), $payload));
+ }
+ }
+
+ private function deserialize($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));
+ }
}
}