summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/PurpleCode/PCurl/PCurl.php30
-rw-r--r--src/PurpleCode/PCurl/PJsonCurl.php20
-rw-r--r--src/PurpleCode/PCurl/PObjectCurl.php40
3 files changed, 80 insertions, 10 deletions
diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php
index 7102661..d3560b6 100644
--- a/src/PurpleCode/PCurl/PCurl.php
+++ b/src/PurpleCode/PCurl/PCurl.php
@@ -1,4 +1,5 @@
<?php
+
/**
* PCurl is a REST client libary for PHP.
*
@@ -46,36 +47,34 @@ class PCurl {
*/
public function call($method, $url, $payload = '') {
if ($method == "POST") {
- return $this->post($url, $payload);
+ $this->setOption(CURLOPT_POST, true);
+ $this->setOption(CURLOPT_POSTFIELDS, $payload);
} else if ($method == "PUT") {
- return $this->put($url, $payload);
+ $this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT');
+ $this->setOption(CURLOPT_POSTFIELDS, $payload);
}
- return $this->get($url);
+ return $this->exec($url);
}
/**
* @return string
*/
public function get($url) {
- return $this->exec($url);
+ return $this->call('GET', $url);
}
/**
* @return string
*/
public function post($url, $data) {
- $this->setOption(CURLOPT_POST, 1);
- $this->setOption(CURLOPT_POSTFIELDS, $data);
- return $this->exec($url);
+ return $this->call('POST', $url, $data);
}
/**
* @return string
*/
public function put($url, $data) {
- $this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT');
- $this->setOption(CURLOPT_POSTFIELDS, $data);
- return $this->exec($url);
+ return $this->call('PUT', $url, $data);
}
/**
@@ -186,8 +185,19 @@ class PCurl {
return $this;
}
+ /**
+ * @return PCurl
+ */
public function setOption($optionKey, $optionValue) {
$this->options[$optionKey] = $optionValue;
+ 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
new file mode 100644
index 0000000..e8d813c
--- /dev/null
+++ b/src/PurpleCode/PCurl/PJsonCurl.php
@@ -0,0 +1,20 @@
+<?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 PJsonCurl extends PCurl {
+
+ 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
new file mode 100644
index 0000000..d874486
--- /dev/null
+++ b/src/PurpleCode/PCurl/PObjectCurl.php
@@ -0,0 +1,40 @@
+<?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 JMS\Serializer\SerializerInterface;
+
+class PObjectCurl extends PCurl {
+
+ /**
+ * @var SerializerInterface
+ */
+ private $serializer;
+ private $responseClass;
+
+ public function __construct($host, SerializerInterface $serializer) {
+ parent::__construct($host);
+ $this->serializer = $serializer;
+
+ $this->contentTypeJson();
+ }
+
+ public function responseClass($class) {
+ $this->responseClass = $class;
+ }
+
+ 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;
+ }
+
+}