diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/PurpleCode/PCurl/PCurl.php | 3 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PJsonCurl.php | 6 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PObjectCurl.php | 25 |
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));
+ }
}
}
|