/** * @inheritDoc */ public function injectSelfLink(LinkCollectionAwareInterface $resource, $route, $routeIdentifier = 'id') { $links = $resource->getLinks(); if ($links->has('self')) { return; } $selfLink = $this->createSelfLink($resource, $route, $routeIdentifier); $links->add($selfLink, true); }
/** * Inject a "self" relational link based on the route and identifier * * @param LinkCollectionAwareInterface $resource * @param string $route * @param string $routeIdentifier */ public function injectSelfLink(LinkCollectionAwareInterface $resource, $route, $routeIdentifier = 'id') { $links = $resource->getLinks(); if ($links->has('self')) { return; } $self = new Link('self'); $self->setRoute($route); $routeParams = []; $routeOptions = []; if ($resource instanceof Entity && null !== $resource->id) { $routeParams = [$routeIdentifier => $resource->id]; } if ($resource instanceof Collection) { $routeParams = $resource->getCollectionRouteParams(); $routeOptions = $resource->getCollectionRouteOptions(); } if (!empty($routeParams)) { $self->setRouteParams($routeParams); } if (!empty($routeOptions)) { $self->setRouteOptions($routeOptions); } $links->add($self, true); }
/** * Create HAL links "object" from an entity or collection * * @param LinkCollectionAwareInterface $resource * @return array */ public function fromResource(LinkCollectionAwareInterface $resource) { return $this->fromLinkCollection($resource->getLinks()); }