Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function decode(Response $response)
 {
     $headers = $response->getHeaders();
     if (!$headers->has('Content-Type')) {
         $exception = new Exception\InvalidResponseException('Content-Type missing');
         $exception->setResponse($response);
         throw $exception;
     }
     /* @var $contentType \Zend\Http\Header\ContentType */
     $contentType = $headers->get('Content-Type');
     switch (true) {
         case $contentType->match('*/json'):
             $payload = Json::decode($response->getBody(), Json::TYPE_ARRAY);
             break;
             //TODO: xml
             //             case $contentType->match('*/xml'):
             //                 $xml = Security::scan($response->getBody());
             //                 $payload = Json::decode(Json::encode((array) $xml), Json::TYPE_ARRAY);
             //                 break;
         //TODO: xml
         //             case $contentType->match('*/xml'):
         //                 $xml = Security::scan($response->getBody());
         //                 $payload = Json::decode(Json::encode((array) $xml), Json::TYPE_ARRAY);
         //                 break;
         default:
             throw new Exception\InvalidFormatException(sprintf('The "%s" media type is invalid or not supported', $contentType->getMediaType()));
             break;
     }
     $this->lastPayload = $payload;
     if ($contentType->match('*/hal+*')) {
         return $this->extractResourceFromHal($payload, $this->getPromoteTopCollection());
     }
     //else
     return (array) $payload;
 }
 public function testGetSetResponse()
 {
     $response = new Response();
     $ex = new InvalidResponseException();
     $ex->setResponse($response);
     $this->assertSame($response, $ex->getResponse());
 }
 /**
  * @param array $bodyDecodeResponse
  * @param Response $response
  * @return Exception\ApiProblem\DomainException|Exception\InvalidResponseException
  */
 protected function getInvalidResponseException(array $bodyDecodeResponse, Response $response)
 {
     $contentType = $response->getHeaders()->get('Content-Type');
     if ($contentType instanceof ContentType && $contentType->match('application/problem+*')) {
         $apiProblemDefaults = ['type' => $response->getReasonPhrase(), 'title' => '', 'status' => $response->getStatusCode(), 'detail' => '', 'instance' => ''];
         $bodyDecodeResponse += $apiProblemDefaults;
         //Setup remote exception
         $remoteExceptionStack = isset($bodyDecodeResponse['exception_stack']) && is_array($bodyDecodeResponse['exception_stack']) ? $bodyDecodeResponse['exception_stack'] : [];
         array_unshift($remoteExceptionStack, ['message' => $bodyDecodeResponse['detail'], 'code' => $bodyDecodeResponse['status'], 'trace' => isset($bodyDecodeResponse['trace']) ? $bodyDecodeResponse['trace'] : null]);
         //Setup exception
         $exception = new Exception\ApiProblem\DomainException($bodyDecodeResponse['detail'], $bodyDecodeResponse['status'], Exception\RemoteException::factory($remoteExceptionStack));
         $exception->setType($bodyDecodeResponse['type']);
         $exception->setTitle($bodyDecodeResponse['title']);
         foreach ($apiProblemDefaults as $key => $value) {
             unset($bodyDecodeResponse[$key]);
         }
         $exception->setAdditionalDetails($bodyDecodeResponse);
     } else {
         $exception = new Exception\InvalidResponseException($response->getReasonPhrase(), $response->getStatusCode());
         $exception->setResponse($response);
     }
     return $exception;
 }