summaryrefslogtreecommitdiffstats
path: root/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-01-11 12:35:47 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2022-01-11 12:35:47 +0100
commit19985dbb8c0aa66dc4bf7905abc1148de909097d (patch)
tree2cd5a5d20d7e80fc2a51adf60d838d8a2c40999e /vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php
download1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.gz
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.bz2
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.lz
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.xz
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.zst
1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.zip
Diffstat (limited to '')
-rw-r--r--vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php114
1 files changed, 114 insertions, 0 deletions
diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php
new file mode 100644
index 0000000..b7158a7
--- /dev/null
+++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace PayPalHttp;
+
+use PayPalHttp\Serializer\Form;
+use PayPalHttp\Serializer\Json;
+use PayPalHttp\Serializer\Multipart;
+use PayPalHttp\Serializer\Text;
+
+/**
+ * Class Encoder
+ * @package PayPalHttp
+ *
+ * Encoding class for serializing and deserializing request/response.
+ */
+class Encoder
+{
+ private $serializers = [];
+
+ function __construct()
+ {
+ $this->serializers[] = new Json();
+ $this->serializers[] = new Text();
+ $this->serializers[] = new Multipart();
+ $this->serializers[] = new Form();
+ }
+
+
+
+ public function serializeRequest(HttpRequest $request)
+ {
+ if (!array_key_exists('content-type', $request->headers)) {
+ $message = "HttpRequest does not have Content-Type header set";
+ echo $message;
+ throw new \Exception($message);
+ }
+
+ $contentType = $request->headers['content-type'];
+ /** @var Serializer $serializer */
+ $serializer = $this->serializer($contentType);
+
+ if (is_null($serializer)) {
+ $message = sprintf("Unable to serialize request with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings()));
+ echo $message;
+ throw new \Exception($message);
+ }
+
+ if (!(is_string($request->body) || is_array($request->body))) {
+ $message = "Body must be either string or array";
+ echo $message;
+ throw new \Exception($message);
+ }
+
+ $serialized = $serializer->encode($request);
+
+ if (array_key_exists("content-encoding", $request->headers) && $request->headers["content-encoding"] === "gzip") {
+ $serialized = gzencode($serialized);
+ }
+ return $serialized;
+ }
+
+
+ public function deserializeResponse($responseBody, $headers)
+ {
+
+ if (!array_key_exists('content-type', $headers)) {
+ $message = "HTTP response does not have Content-Type header set";
+ echo $message;
+ throw new \Exception($message);
+ }
+
+ $contentType = $headers['content-type'];
+ /** @var Serializer $serializer */
+ $serializer = $this->serializer($contentType);
+
+ if (is_null($serializer)) {
+ throw new \Exception(sprintf("Unable to deserialize response with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings())));
+ }
+
+ if (array_key_exists("content-encoding", $headers) && $headers["content-encoding"] === "gzip") {
+ $responseBody = gzdecode($responseBody);
+ }
+
+ return $serializer->decode($responseBody);
+ }
+
+ private function serializer($contentType)
+ {
+ /** @var Serializer $serializer */
+ foreach ($this->serializers as $serializer) {
+ try {
+ if (preg_match($serializer->contentType(), $contentType) == 1) {
+ return $serializer;
+ }
+ } catch (\Exception $ex) {
+ $message = sprintf("Error while checking content type of %s: %s", get_class($serializer), $ex->getMessage());
+ echo $message;
+ throw new \Exception($message, $ex->getCode(), $ex);
+ }
+ }
+
+ return NULL;
+ }
+
+ private function supportedEncodings()
+ {
+ $values = [];
+ /** @var Serializer $serializer */
+ foreach ($this->serializers as $serializer) {
+ $values[] = $serializer->contentType();
+ }
+ return $values;
+ }
+}