Beispiel #1
0
 public function testCanSetRouteParamsSeparately()
 {
     $route  = 'api/docs';
     $params = array('version' => '1.1');
     $link = new Link('describedby');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $this->assertEquals($route, $link->getRoute());
     $this->assertEquals($params, $link->getRouteParams());
 }
 public function setUpChildEntity($id, $name)
 {
     $this->child = (object) array('id' => $id, 'name' => $name);
     $entity = new Entity($this->child, $id);
     $link = new Link('self');
     $link->setRoute('parent/child');
     $link->setRouteParams(array('child' => $id));
     $entity->getLinks()->add($link);
     return $entity;
 }
Beispiel #3
0
 private function createSelfLink($resource, $route, $routeIdentifier)
 {
     $link = new Link('self');
     $link->setRoute($route);
     $routeParams = $this->getRouteParams($resource, $routeIdentifier);
     if (!empty($routeParams)) {
         $link->setRouteParams($routeParams);
     }
     $routeOptions = $this->getRouteOptions($resource);
     if (!empty($routeOptions)) {
         $link->setRouteOptions($routeOptions);
     }
     return $link;
 }
 /**
  * Return a HAL entity with just a self link
  */
 public function extract($value)
 {
     if (is_null($value)) {
         return $value;
     }
     $entityValues = $this->getHydratorForEntity($value)->extract($value);
     $entityMetadata = $this->getMetadataMap()[ClassUtils::getRealClass(get_class($value))];
     $link = new Link('self');
     $link->setRoute($entityMetadata['route_name']);
     $link->setRouteParams(array($entityMetadata['route_identifier_name'] => $entityValues[$entityMetadata['entity_identifier_name']]));
     $linkCollection = new LinkCollection();
     $linkCollection->add($link);
     $halEntity = new HalEntity(new stdClass());
     $halEntity->setLinks($linkCollection);
     return $halEntity;
 }
 public function extract($value)
 {
     if (!method_exists($value, 'getTypeClass')) {
         return;
     }
     $config = $this->getMetadataMap()[$value->getTypeClass()->name];
     $filter = new FilterChain();
     $filter->attachByName('WordCamelCaseToUnderscore')->attachByName('StringToLower');
     // Better way to create mapping name?
     // FIXME: use zf-hal collection_name
     $link = new Link('self');
     $link->setRoute($config['route_name']);
     $link->setRouteParams(array('id' => null));
     $filterValue = array('field' => $value->getMapping()['mappedBy'] ?: $value->getMapping()['inversedBy'], 'type' => isset($value->getMapping()['joinTable']) ? 'ismemberof' : 'eq', 'value' => $value->getOwner()->getId());
     $link->setRouteOptions(array('query' => array($this->getFilterKey() => array($filterValue))));
     $linkCollection = new LinkCollection();
     $linkCollection->add($link);
     $halEntity = new HalEntity(new stdClass());
     $halEntity->setLinks($linkCollection);
     return $halEntity;
 }
 public function extract($value)
 {
     $config = $this->getServiceManager()->get('Config');
     if (!method_exists($value, 'getTypeClass') || !isset($config['zf-hal']['metadata_map'][$value->getTypeClass()->name])) {
         return;
     }
     $config = $config['zf-hal']['metadata_map'][$value->getTypeClass()->name];
     $mapping = $value->getMapping();
     $filter = new FilterChain();
     $filter->attachByName('WordCamelCaseToUnderscore')->attachByName('StringToLower');
     $link = new Link($filter($mapping['fieldName']));
     $link->setRoute($config['route_name']);
     $link->setRouteParams(array('id' => null));
     if (isset($config['zf-doctrine-querybuilder-options']['filter_key'])) {
         $filterKey = $config['zf-doctrine-querybuilder-options']['filter_key'];
     } else {
         $filterKey = 'filter';
     }
     $link->setRouteOptions(array('query' => array($filterKey => array(array('field' => $mapping['mappedBy'], 'type' => 'eq', 'value' => $value->getOwner()->getId())))));
     return $link;
 }
Beispiel #7
0
 /**
  * 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);
 }
 public function setUpChildResource($id, $name)
 {
     $this->child = (object) ['id' => $id, 'name' => $name];
     $resource = new HalEntity($this->child, $id);
     $link = new Link('self');
     $link->setRoute('parent/child');
     $link->setRouteParams(['child' => $id]);
     $resource->getLinks()->add($link);
     return $resource;
 }
Beispiel #9
0
 /**
  * Generate HAL links for a paginated collection
  *
  * @param  Collection $halCollection
  * @return boolean
  */
 protected function injectPaginationLinks(Collection $halCollection)
 {
     $collection = $halCollection->getCollection();
     $page = $halCollection->getPage();
     $pageSize = $halCollection->getPageSize();
     $route = $halCollection->getCollectionRoute();
     $params = $halCollection->getCollectionRouteParams();
     $options = $halCollection->getCollectionRouteOptions();
     $collection->setItemCountPerPage($pageSize);
     $collection->setCurrentPageNumber($page);
     $count = count($collection);
     if (!$count) {
         return true;
     }
     if ($page < 1 || $page > $count) {
         return new ApiProblem(409, 'Invalid page provided');
     }
     $links = $halCollection->getLinks();
     $next = $page < $count ? $page + 1 : false;
     $prev = $page > 1 ? $page - 1 : false;
     // self link
     $link = new Link('self');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $page]]));
     $links->add($link, true);
     // first link
     $link = new Link('first');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => null]]));
     $links->add($link);
     // last link
     $link = new Link('last');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $count]]));
     $links->add($link);
     // prev link
     if ($prev) {
         $link = new Link('prev');
         $link->setRoute($route);
         $link->setRouteParams($params);
         $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $prev]]));
         $links->add($link);
     }
     // next link
     if ($next) {
         $link = new Link('next');
         $link->setRoute($route);
         $link->setRouteParams($params);
         $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $next]]));
         $links->add($link);
     }
     return true;
 }