Ejemplo n.º 1
0
 /**
  * @param PaginatedDataInterface $data
  * @param string                 $uri
  *
  * @return array
  */
 protected function getRelationshipDescription(PaginatedDataInterface $data, $uri)
 {
     if ($data->hasMoreItems() === false) {
         return [static::DATA => $data->getData()];
     }
     $buildUrl = function ($offset) use($data, $uri) {
         $paramsWithPaging = [PaginationStrategyInterface::PARAM_PAGING_SKIP => $offset, PaginationStrategyInterface::PARAM_PAGING_SIZE => $data->getLimit()];
         $fullUrl = $uri . '?' . http_build_query($paramsWithPaging);
         return $fullUrl;
     };
     $links = [];
     // It looks like relationship can only hold first data rows so we might need `next` link but never `prev`
     if ($data->hasMoreItems() === true) {
         $offset = $data->getOffset() + $data->getLimit();
         $links[DocumentInterface::KEYWORD_NEXT] = $this->createLink($buildUrl($offset));
     }
     return [static::DATA => $data->getData(), static::LINKS => $links];
 }
Ejemplo n.º 2
0
 /**
  * @param PaginatedDataInterface $data
  *
  * @return void
  */
 private function addPagingLinksIfNeeded(PaginatedDataInterface $data)
 {
     if ($data->isCollection() === true && (0 < $data->getOffset() || $data->hasMoreItems() === true) && $this->getOriginalUri() !== null) {
         $links = [];
         $linkClosure = $this->createLinkClosure($data->getLimit());
         $prev = DocumentInterface::KEYWORD_PREV;
         $next = DocumentInterface::KEYWORD_NEXT;
         $data->getOffset() <= 0 ?: ($links[$prev] = $linkClosure(max(0, $data->getOffset() - $data->getLimit())));
         $data->hasMoreItems() === false ?: ($links[$next] = $linkClosure($data->getOffset() + $data->getLimit()));
         $this->withLinks($links);
     }
 }
Ejemplo n.º 3
0
 /**
  * @param PaginatedDataInterface      $data
  * @param IncludeParameterInterface[] $paths
  *
  * @return RelationshipStorageInterface
  *
  * @SuppressWarnings(PHPMD.ElseExpression)
  */
 private function readRelationships(PaginatedDataInterface $data, array $paths)
 {
     $result = $this->getFactory()->createRelationshipStorage();
     if (empty($data->getData()) === false && empty($paths) === false) {
         $modelStorage = $this->getFactory()->createModelStorage($this->getModelSchemes());
         $modelsAtPath = $this->getFactory()->createTagStorage();
         // we gonna send this storage via function params so it is an equivalent for &array
         $classAtPath = new ArrayObject();
         $model = null;
         if ($data->isCollection() === true) {
             foreach ($data->getData() as $model) {
                 $uniqueModel = $modelStorage->register($model);
                 if ($uniqueModel !== null) {
                     $modelsAtPath->register($uniqueModel, static::$rootPath);
                 }
             }
         } else {
             $model = $data->getData();
             $uniqueModel = $modelStorage->register($model);
             if ($uniqueModel !== null) {
                 $modelsAtPath->register($uniqueModel, static::$rootPath);
             }
         }
         $classAtPath[static::$rootPath] = get_class($model);
         foreach ($this->getPaths($paths) as list($parentPath, $childPaths)) {
             $this->loadRelationshipsLayer($result, $modelsAtPath, $classAtPath, $modelStorage, $parentPath, $childPaths);
         }
     }
     return $result;
 }