Serializer
5 years ago
Curl.php
5 years ago
Encoder.php
4 years ago
Environment.php
5 years ago
HttpClient.php
4 years ago
HttpException.php
4 years ago
HttpRequest.php
5 years ago
HttpResponse.php
4 years ago
IOException.php
5 years ago
Injector.php
4 years ago
Serializer.php
5 years ago
Encoder.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PayPalHttp; |
| 4 | |
| 5 | use PayPalHttp\Serializer\Form; |
| 6 | use PayPalHttp\Serializer\Json; |
| 7 | use PayPalHttp\Serializer\Multipart; |
| 8 | use PayPalHttp\Serializer\Text; |
| 9 | |
| 10 | /** |
| 11 | * Class Encoder |
| 12 | * @package PayPalHttp |
| 13 | * |
| 14 | * Encoding class for serializing and deserializing request/response. |
| 15 | */ |
| 16 | class Encoder |
| 17 | { |
| 18 | private $serializers = []; |
| 19 | |
| 20 | function __construct() |
| 21 | { |
| 22 | $this->serializers[] = new Json(); |
| 23 | $this->serializers[] = new Text(); |
| 24 | $this->serializers[] = new Multipart(); |
| 25 | $this->serializers[] = new Form(); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | public function serializeRequest(HttpRequest $request) |
| 31 | { |
| 32 | if (!array_key_exists('content-type', $request->headers)) { |
| 33 | $message = "HttpRequest does not have Content-Type header set"; |
| 34 | echo $message; |
| 35 | throw new \Exception($message); |
| 36 | } |
| 37 | |
| 38 | $contentType = $request->headers['content-type']; |
| 39 | /** @var Serializer $serializer */ |
| 40 | $serializer = $this->serializer($contentType); |
| 41 | |
| 42 | if (is_null($serializer)) { |
| 43 | $message = sprintf("Unable to serialize request with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings())); |
| 44 | echo $message; |
| 45 | throw new \Exception($message); |
| 46 | } |
| 47 | |
| 48 | if (!(is_string($request->body) || is_array($request->body))) { |
| 49 | $message = "Body must be either string or array"; |
| 50 | echo $message; |
| 51 | throw new \Exception($message); |
| 52 | } |
| 53 | |
| 54 | $serialized = $serializer->encode($request); |
| 55 | |
| 56 | if (array_key_exists("content-encoding", $request->headers) && $request->headers["content-encoding"] === "gzip") { |
| 57 | $serialized = gzencode($serialized); |
| 58 | } |
| 59 | return $serialized; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | public function deserializeResponse($responseBody, $headers) |
| 64 | { |
| 65 | |
| 66 | if (!array_key_exists('content-type', $headers)) { |
| 67 | $message = "HTTP response does not have Content-Type header set"; |
| 68 | echo $message; |
| 69 | throw new \Exception($message); |
| 70 | } |
| 71 | |
| 72 | $contentType = $headers['content-type']; |
| 73 | $contentType = strtolower($contentType); |
| 74 | /** @var Serializer $serializer */ |
| 75 | $serializer = $this->serializer($contentType); |
| 76 | |
| 77 | if (is_null($serializer)) { |
| 78 | throw new \Exception(sprintf("Unable to deserialize response with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings()))); |
| 79 | } |
| 80 | |
| 81 | if (array_key_exists("content-encoding", $headers) && $headers["content-encoding"] === "gzip") { |
| 82 | $responseBody = gzdecode($responseBody); |
| 83 | } |
| 84 | |
| 85 | return $serializer->decode($responseBody); |
| 86 | } |
| 87 | |
| 88 | private function serializer($contentType) |
| 89 | { |
| 90 | /** @var Serializer $serializer */ |
| 91 | foreach ($this->serializers as $serializer) { |
| 92 | try { |
| 93 | if (preg_match($serializer->contentType(), $contentType) == 1) { |
| 94 | return $serializer; |
| 95 | } |
| 96 | } catch (\Exception $ex) { |
| 97 | $message = sprintf("Error while checking content type of %s: %s", get_class($serializer), $ex->getMessage()); |
| 98 | echo $message; |
| 99 | throw new \Exception($message, $ex->getCode(), $ex); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | private function supportedEncodings() |
| 107 | { |
| 108 | $values = []; |
| 109 | /** @var Serializer $serializer */ |
| 110 | foreach ($this->serializers as $serializer) { |
| 111 | $values[] = $serializer->contentType(); |
| 112 | } |
| 113 | return $values; |
| 114 | } |
| 115 | } |
| 116 |