/** * Attach download link to asset HAL metadata * * @param Event $event */ public function attachAssetLink(Event $event) { $halEntity = $event->getParam('entity'); $uri = $this->getServiceLocator()->get('Router')->getRequestUri(); $link = new HalLink('download'); $link->setUrl($uri->getScheme() . '://' . $uri->getHost() . '/' . $halEntity->entity->getBaseurl() . '/' . $halEntity->entity->getFilename()); $halEntity->getLinks()->add($link); }
/** * Fetch a resource * * @param mixed $id * @return ApiProblem|mixed */ public function fetch($id) { $stream = $this->streamService->fetch($id); $streamOwner = $this->streamService->fetchOwner($id); if (!$stream) { $entity = new Entity(array(), $id); } else { if ($streamOwner) { $channel = $this->channelService->fetch($streamOwner->channel_id); $stream->channel = $channel; unset($stream->channel_id); } $entity = new Entity($stream, $id); } if ($streamOwner) { $channelLink = new Link("channel"); $channelLink->setUrl("/channel/" . $streamOwner->channel_id); $entity->getLinks()->add($channelLink); $userLink = new Link("user"); $userLink->setUrl("/user/" . $streamOwner->user_id); $entity->getLinks()->add($userLink); } return $entity; }
/** * Creates a link object, given metadata and a resource * * @param Metadata $metadata * @param object $object * @param null|string $id * @param null|string $routeIdentifierName * @param string $relation * @return Link * @throws Exception\RuntimeException */ public function marshalLinkFromMetadata(Metadata $metadata, $object, $id = null, $routeIdentifierName = null, $relation = 'self') { $link = new Link($relation); if ($metadata->hasUrl()) { $link->setUrl($metadata->getUrl()); return $link; } if (!$metadata->hasRoute()) { throw new Exception\RuntimeException(sprintf('Unable to create a self link for resource of type "%s"; metadata does not contain a route or a url', get_class($object))); } $params = $metadata->getRouteParams(); // process any callbacks foreach ($params as $key => $param) { // bind to the object if supported if ($param instanceof Closure && version_compare(PHP_VERSION, '5.4.0') >= 0) { $param = $param->bindTo($object); } // pass the object for callbacks and non-bound closures if (is_callable($param)) { $params[$key] = call_user_func_array($param, [$object]); } } if ($routeIdentifierName) { $params = array_merge($params, [$routeIdentifierName => $id]); } if ($relation !== 'self' && $id) { $link->setProps(array_merge($link->getProps(), ['id' => $id])); } $link->setRoute($metadata->getRoute(), $params, $metadata->getRouteOptions()); return $link; }
/** * @group 71 */ public function testRenderingCollectionRendersAllLinksInEmbeddedEntities() { $embedded = new Entity((object) ['id' => 'foo', 'name' => 'foo'], 'foo'); $links = $embedded->getLinks(); $self = new Link('self'); $self->setRoute('hostname/users', ['id' => 'foo']); $links->add($self); $phones = new Link('phones'); $phones->setUrl('http://localhost.localdomain/users/foo/phones'); $links->add($phones); $collection = new Collection([$embedded]); $collection->setCollectionName('users'); $self = new Link('self'); $self->setRoute('hostname/users'); $collection->getLinks()->add($self); $rendered = $this->plugin->renderCollection($collection); $this->assertRelationalLinkContains('/users', 'self', $rendered); $this->assertArrayHasKey('_embedded', $rendered); $this->assertInternalType('array', $rendered['_embedded']); $this->assertArrayHasKey('users', $rendered['_embedded']); $users = $rendered['_embedded']['users']; $this->assertInternalType('array', $users); $user = array_shift($users); $this->assertRelationalLinkContains('/users/foo', 'self', $user); $this->assertRelationalLinkContains('/users/foo/phones', 'phones', $user); }
public function testIsCompleteReturnsTrueWhenUrlIsSet() { $link = new Link('describedby'); $link->setUrl('http://example.com/api/docs.html'); $this->assertTrue($link->isComplete()); }
/** * @group 97 */ public function testLocationHeaderGeneratedDuringCreateContainsOnlyLinkHref() { $self = new Link('self'); $self->setUrl('http://localhost.localdomain/resource/foo'); $self->setProps(['vary' => 'true']); $entity = new HalEntity(['id' => 'foo', 'bar' => 'baz'], 'foo'); $links = $entity->getLinks(); $links->add($self, true); $this->resource->getEventManager()->attach('create', function ($e) use($entity) { return $entity; }); $result = $this->controller->create([]); $this->assertSame($entity, $result); $response = $this->controller->getResponse(); $headers = $response->getHeaders(); $this->assertTrue($headers->has('Location')); $this->assertNotContains('true', $headers->get('Location')->getFieldValue()); }