ResponseHandler.php
193 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Core\Response; |
| 6 | |
| 7 | use Core\Response\Types\DeserializableType; |
| 8 | use Core\Response\Types\ErrorType; |
| 9 | use Core\Response\Types\ResponseMultiType; |
| 10 | use Core\Response\Types\ResponseType; |
| 11 | use Core\Utils\XmlDeserializer; |
| 12 | use CoreInterfaces\Core\Format; |
| 13 | |
| 14 | class ResponseHandler |
| 15 | { |
| 16 | private $format = Format::SCALAR; |
| 17 | private $deserializableType; |
| 18 | private $responseType; |
| 19 | private $responseMultiType; |
| 20 | private $responseError; |
| 21 | private $useApiResponse = false; |
| 22 | private $nullableType = false; |
| 23 | |
| 24 | public function __construct() |
| 25 | { |
| 26 | $this->responseError = new ResponseError(); |
| 27 | $this->deserializableType = new DeserializableType(); |
| 28 | $this->responseType = new ResponseType(); |
| 29 | $this->responseMultiType = new ResponseMultiType(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Associates an ErrorType object to the statusCode provided. |
| 34 | */ |
| 35 | public function throwErrorOn(string $statusCode, ErrorType $error): self |
| 36 | { |
| 37 | $this->responseError->addError($statusCode, $error); |
| 38 | return $this; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Wrap the actual success/failure response in ApiResponse |
| 43 | */ |
| 44 | public function returnApiResponse(): self |
| 45 | { |
| 46 | $this->useApiResponse = true; |
| 47 | $this->responseError->returnApiResponse(); |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Sets the nullOn404 flag in ResponseError. |
| 53 | */ |
| 54 | public function nullOn404(): self |
| 55 | { |
| 56 | $this->responseError->nullOn404(); |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Sets the return type as nullable. |
| 62 | */ |
| 63 | public function nullableType(): self |
| 64 | { |
| 65 | $this->nullableType = true; |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Sets the deserializer method to the one provided, for deserializableType. |
| 71 | */ |
| 72 | public function deserializerMethod(callable $deserializerMethod): self |
| 73 | { |
| 74 | $this->deserializableType->setDeserializerMethod($deserializerMethod); |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets response type to the one provided and format to JSON. |
| 80 | * |
| 81 | * @param string $responseClass Response type class |
| 82 | * @param int $dimensions Dimensions to be provided in case of an array |
| 83 | */ |
| 84 | public function type(string $responseClass, int $dimensions = 0): self |
| 85 | { |
| 86 | $this->format = Format::JSON; |
| 87 | $this->responseType->setResponseClass($responseClass); |
| 88 | $this->responseType->setDimensions($dimensions); |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets response type to the one provided and format to XML. |
| 94 | * |
| 95 | * @param string $responseClass Type of Response class |
| 96 | * @param string $rootName Name of the root in xml response |
| 97 | */ |
| 98 | public function typeXml(string $responseClass, string $rootName): self |
| 99 | { |
| 100 | $this->format = Format::XML; |
| 101 | $this->responseType->setResponseClass($responseClass); |
| 102 | $this->responseType->setXmlDeserializer(function ($value, $class) use ($rootName) { |
| 103 | return (new XmlDeserializer())->deserialize($value, $rootName, $class); |
| 104 | }); |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets response type to the one provided and format to XML. |
| 110 | * |
| 111 | * @param string $responseClass Type of Response class |
| 112 | * @param string $rootName Name of the root for map in xml response |
| 113 | */ |
| 114 | public function typeXmlMap(string $responseClass, string $rootName): self |
| 115 | { |
| 116 | $this->format = Format::XML; |
| 117 | $this->responseType->setResponseClass($responseClass); |
| 118 | $this->responseType->setXmlDeserializer(function ($value, $class) use ($rootName): ?array { |
| 119 | return (new XmlDeserializer())->deserializeToMap($value, $rootName, $class); |
| 120 | }); |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sets response type to the one provided and format to XML. |
| 126 | * |
| 127 | * @param string $responseClass Type of Response class |
| 128 | * @param string $rootName Name of the root for array in xml response |
| 129 | * @param string $itemName Name of each item in array |
| 130 | */ |
| 131 | public function typeXmlArray(string $responseClass, string $rootName, string $itemName): self |
| 132 | { |
| 133 | $this->format = Format::XML; |
| 134 | $this->responseType->setResponseClass($responseClass); |
| 135 | $this->responseType->setXmlDeserializer(function ($value, $class) use ($rootName, $itemName): ?array { |
| 136 | return (new XmlDeserializer())->deserializeToArray($value, $rootName, $itemName, $class); |
| 137 | }); |
| 138 | return $this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @param string $typeGroup Group of types in string format i.e. oneOf(...), anyOf(...) |
| 143 | * @param string[] $typeGroupDeserializers Methods required for deserialization of specific types in |
| 144 | * in the provided typeGroup, should be an array in the format: |
| 145 | * ['path/to/method returnType', ...]. Default: [] |
| 146 | */ |
| 147 | public function typeGroup(string $typeGroup, array $typeGroupDeserializers = []): self |
| 148 | { |
| 149 | $this->format = Format::JSON; |
| 150 | $this->responseMultiType->setTypeGroup($typeGroup); |
| 151 | $this->responseMultiType->setDeserializers($typeGroupDeserializers); |
| 152 | return $this; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns current set format. |
| 157 | */ |
| 158 | public function getFormat(): string |
| 159 | { |
| 160 | return $this->format; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns response from the context provided. |
| 165 | * |
| 166 | * @param Context $context |
| 167 | * @return mixed |
| 168 | */ |
| 169 | public function getResult(Context $context) |
| 170 | { |
| 171 | if ($context->isFailure()) { |
| 172 | return $this->responseError->getResult($context); |
| 173 | } |
| 174 | if ($this->nullableType && $context->isBodyMissing()) { |
| 175 | return $this->getResponse($context, null); |
| 176 | } |
| 177 | $result = $this->deserializableType->getFrom($context); |
| 178 | $result = $result ?? $this->responseType->getFrom($context); |
| 179 | $result = $result ?? $this->responseMultiType->getFrom($context); |
| 180 | $result = $result ?? $context->getResponseBody(); |
| 181 | |
| 182 | return $this->getResponse($context, $result); |
| 183 | } |
| 184 | |
| 185 | private function getResponse(Context $context, $result) |
| 186 | { |
| 187 | if ($this->useApiResponse) { |
| 188 | return $context->toApiResponse($result); |
| 189 | } |
| 190 | return $result; |
| 191 | } |
| 192 | } |
| 193 |