/** * {@inheritdoc} */ public function execute($url, $method = 'get', $data = [], &$statusCode = null) { $parentRequest = $this->app['router']->getCurrentRequest(); if (mb_substr($url, 0, 4) === 'http') { $response = $this->performRemoteRequest($parentRequest, $url, $method, $data, $statusCode); } else { $response = $this->performLocalRequest($parentRequest, $url, $method, $data, $statusCode); } return Hal::fromJson($response, 3); }
/** * 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); }
/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $sample = <<<JSON { "id": 1, "name": "test", "_links":{ "self":{"href":"http:\\/\\/127.0.0.1\\/2"}, "prev":{"href":"http:\\/\\/127.0.0.1\\/1"}, "next":{"href":"http:\\/\\/127.0.0.1\\/3"} }, "_embedded":{ "item":[ { "_links":{ "self":{"href":"http:\\/\\/127.0.0.1\\/"}, "next":{"href":"http:\\/\\/127.0.0.1\\/3"} }, "key": "value1" }, { "_links":{ "self":{"href":"http:\\/\\/127.0.0.1\\/"}, "next":{"href":"http:\\/\\/127.0.0.1\\/3"} }, "key": "value2" } ], "item2":[ { "_links":{ "self":{"href":"http:\\/\\/127.0.0.1\\/"}, "next":{"href":"http:\\/\\/127.0.0.1\\/3"} }, "key": "value1" } ] } } JSON; $this->object = new Resource(Hal::fromJson($sample, 2)); }
/** * 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); } }
/** * @covers Hermes\Api\Response::__construct */ public function testAcceptXml() { $http = new Client(); $response = new ZendResponse(); $headers = new Headers(); $headers->addHeaderLine('Content-Type', 'application/xml'); $response->setHeaders($headers); $hal = Hal::fromJson(static::$sampleJson, 10); $xml = $hal->asXml(true); $response->setContent($xml); $this->object = new Response($http, $response); $content = $this->object->getContent(); $this->assertInstanceOf(Resource::class, $content); }
public function testLinksWithAttributesUnserialiseCorrectlyJson() { $x = new Hal('/'); $x->addCurie('x:test', 'http://test'); $this->assertEquals($x->asJson(), Hal::fromJson($x->asJson())->asJson()); }
/** * @expectedException RuntimeException */ public function testHalFromJsonThrowsExceptionOnInvalidJSON() { $invalidJson = 'foo'; Hal::fromJson($invalidJson); }