/**
  * Maps a response body to an object.
  *
  * @param ResponseInterface $response
  * @param string            $class
  * @param string            $format
  *
  * @return object
  */
 public function map(ResponseInterface $response, $class, $format)
 {
     if ('json' === $format) {
         $hal = Hal::fromJson((string) $response->getBody(), 10);
     } elseif ('xml' === $format) {
         $hal = Hal::fromXml((string) $response->getBody(), 10);
     } else {
         throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $format));
     }
     return $this->serializer->fromArray($this->getDataFromHal($hal, $class), $class);
 }
Esempio n. 2
0
 /**
  * Construtor.
  *
  * @param Zend\Http\Client   $client
  * @param Zend\Http\Response $response
  */
 public function __construct(ZendHttpClient $client, ZendHttpResponse $response, $depth = 0)
 {
     $this->httpClient = $client;
     $this->httpResponse = $response;
     if (!$this->httpResponse->isSuccess()) {
         $error = json_decode($this->httpResponse->getBody());
         if (empty($error)) {
             $error = new \stdClass();
             $error->status = $this->httpResponse->getStatusCode();
             $error->title = $this->httpResponse->getReasonPhrase();
             $error->detail = '';
         }
         if (!isset($error->status)) {
             $error->status = 500;
         }
         if (!isset($error->detail)) {
             $error->detail = 'An error occurred.';
         }
         throw new RuntimeException(json_encode($error, null, 100), $error->status);
     }
     if (!$this->httpResponse->getHeaders()->has('Content-Type')) {
         throw new RuntimeException("Missing 'Content-Type' header.", 500);
     }
     $contentType = $this->httpResponse->getHeaders()->get('Content-Type')->getFieldValue();
     $pos = strpos($contentType, ';');
     if ($pos !== false) {
         $contentType = substr($contentType, 0, $pos);
     }
     if (empty($this->httpResponse->getBody())) {
         $this->content = null;
     } elseif ($contentType == 'application/hal+json' || $contentType == 'application/json') {
         $this->content = new Resource(Hal::fromJson($this->httpResponse->getBody(), $depth));
     } elseif ($contentType == 'application/hal+xml' || $contentType == 'application/xml') {
         $this->content = new Resource(Hal::fromXml($this->httpResponse->getBody(), $depth));
     } else {
         throw new RuntimeException("Unable to handle content type '{$contentType}' for response: '{$this->httpResponse->getBody()}'.", 500);
     }
 }
Esempio n. 3
0
 public function testLinksWithAttributesUnserialiseCorrectlyXml()
 {
     $x = new Hal('/');
     $x->addCurie('x:test', 'http://test');
     $this->assertEquals($x->asXml(), Hal::fromXml($x->asXml())->asXml());
 }