Example #1
0
 /**
  * @inheritDoc
  */
 public function extract(Link $object)
 {
     if (!$object->isComplete()) {
         throw new DomainException(sprintf('Link from resource provided to %s was incomplete; must contain a URL or a route', __METHOD__));
     }
     $representation = $object->getProps();
     if ($object->hasUrl()) {
         $representation['href'] = $object->getUrl();
         return $representation;
     }
     $reuseMatchedParams = true;
     $options = $object->getRouteOptions();
     if (isset($options['reuse_matched_params'])) {
         $reuseMatchedParams = (bool) $options['reuse_matched_params'];
         unset($options['reuse_matched_params']);
     }
     $representation['href'] = $this->linkUrlBuilder->buildLinkUrl($object->getRoute(), $object->getRouteParams(), $options, $reuseMatchedParams);
     return $representation;
 }
Example #2
0
 /**
  * Create a fully qualified URI for a link
  *
  * Triggers the "createLink" event with the route, id, entity, and a set of
  * params that will be passed to the route; listeners can alter any of the
  * arguments, which will then be used by the method to generate the url.
  *
  * @todo   Remove 'resource' from the event parameters prior to 1.0.0.
  * @param  string $route
  * @param  null|false|int|string $id
  * @param  null|mixed $entity
  * @return string
  */
 public function createLink($route, $id = null, $entity = null)
 {
     $params = new ArrayObject();
     $reUseMatchedParams = true;
     if (false === $id) {
         $reUseMatchedParams = false;
     } elseif (null !== $id) {
         $params['id'] = $id;
     }
     $events = $this->getEventManager();
     $eventParams = $events->prepareArgs(['route' => $route, 'id' => $id, 'entity' => $entity, 'resource' => $entity, 'params' => $params]);
     $events->trigger(__FUNCTION__, $this, $eventParams);
     return $this->linkUrlBuilder->buildLinkUrl($route, $params->getArrayCopy(), [], $reUseMatchedParams);
 }