/** * @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]; }
/** * @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; }