Beispiel #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;
 }
 /**
  * @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']);
     }
     $path = call_user_func($this->urlHelper, $object->getRoute(), $object->getRouteParams(), $options, $reuseMatchedParams);
     if (substr($path, 0, 4) == 'http') {
         $representation['href'] = $path;
     } else {
         $representation['href'] = $this->getServerUrl() . $path;
     }
     return $representation;
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * Create a URL from a Link
  *
  * @param  Link $linkDefinition
  * @return array
  * @throws Exception\DomainException if Link is incomplete
  */
 public function fromLink(Link $linkDefinition)
 {
     if (!$linkDefinition->isComplete()) {
         throw new DomainException(sprintf('Link from resource provided to %s was incomplete; must contain a URL or a route', __METHOD__));
     }
     $representation = $linkDefinition->getProps();
     if ($linkDefinition->hasUrl()) {
         return array_merge($representation, array('href' => $linkDefinition->getUrl()));
     }
     $reuseMatchedParams = true;
     $options = $linkDefinition->getRouteOptions();
     if (isset($options['reuse_matched_params'])) {
         $reuseMatchedParams = (bool) $options['reuse_matched_params'];
         unset($options['reuse_matched_params']);
     }
     $path = call_user_func($this->urlHelper, $linkDefinition->getRoute(), $linkDefinition->getRouteParams(), $options, $reuseMatchedParams);
     if (substr($path, 0, 4) == 'http') {
         return array_merge($representation, array('href' => $path));
     }
     return array_merge($representation, array('href' => call_user_func($this->serverUrlHelper, $path)));
 }