/** * The response message body may be a string * representation of a Resource representing the error. * * This is basically a shortcut for Resource::fromJson(getResponseBody()). * @return \Finix\Hal\Resource The Resource returned by the response (may be empty). */ public function getResponseResource() { return Resource::fromJson($this->getResponseBody()); }
/** * @param Hal\Resource $resource */ private function setResource($resource) { $this->resource = $resource; $this->state = new ArrayProxy($resource->getState()); }
private static function extractByRel(array $json, $rel) { $out = []; if (!isset($json[$rel])) { return $out; } foreach ($json[$rel] as $name => $uniqueOrArray) { if (isset($uniqueOrArray[0])) { // Array $aArrayOfLinks = []; foreach ($uniqueOrArray as $unique) { if ($rel == '_links') { $aArrayOfLinks[] = Link::fromJson($unique); } else { $aArrayOfLinks[] = Resource::fromJson($unique); } } $out[$name] = $aArrayOfLinks; } else { // Unique if ($rel == '_links') { $out[$name] = Link::fromJson($uniqueOrArray); } else { $out[$name] = Resource::fromJson($uniqueOrArray); } } } return $out; }
/** * @param Request $request * @return \Finix\Hal\Resource * @throws HalClientErrorException * @throws HalException * @throws HalRedirectionException * @throws HalServerErrorException * @throws \Exception */ public function sendRequest($request) { // Create the HTTP request $httpRequest = $this->createHttpRequest($request); // Send the request $httpResponse = $this->executeHttpRequest($httpRequest); // Check the status code (must be 2xx) $statusCode = $httpResponse->getStatusCode(); if ($statusCode >= 200 && $statusCode < 300) { return Resource::fromJson((string) $httpResponse->getBody()); } // Exception depending on status code for 3xx, 4xx and 5xx if ($statusCode >= 300 && $statusCode < 400) { throw new HalRedirectionException($httpRequest, $httpResponse); } elseif ($statusCode >= 400 && $statusCode < 500) { throw new HalClientErrorException($httpRequest, $httpResponse); } elseif ($statusCode >= 500 && $statusCode < 600) { throw new HalServerErrorException($httpRequest, $httpResponse); } else { throw new HalException($httpRequest, $httpResponse); } }
public function test_equalResources() { $resource1 = Resource::fromJson(self::JSON_REPRESENTATION); $resource2 = Resource::fromJson(self::JSON_REPRESENTATION); $this->assertTrue($resource1->getState() == $resource2->getState(), 'States are not equal.'); $this->assertTrue($resource1->getAllLinks() == $resource2->getAllLinks(), 'Links are not equal.'); $this->assertTrue($resource1->getAllEmbeddedResources() == $resource2->getAllEmbeddedResources(), 'Embedded resources are not equal.'); $this->assertTrue($resource1 == $resource2, 'Resources are not equal.'); }