/** * @param HalCollection $halCollection * @return array */ protected function getCollectionPayload(HalCollection $halCollection) { $collection = $halCollection->getCollection(); $collectionName = $halCollection->getCollectionName(); $attributes = $halCollection->getAttributes(); $normalizationGroups = $this->getNormalizationGroups($halCollection); if ($collection instanceof Paginator) { $pageSize = (int) (isset($attributes['page_size']) ? $attributes['page_size'] : $halCollection->getPageSize()); $collection->setItemCountPerPage($pageSize); $items = (array) $collection->getCurrentItems(); /** @todo Force snake case as collection name? */ $payload = array($collectionName => $this->getNormalizer()->normalize($items, $normalizationGroups), 'page_count' => (int) (isset($attributes['page_count']) ? $attributes['page_count'] : $collection->count()), 'page_size' => $pageSize, 'total_items' => (int) (isset($attributes['total_items']) ? $attributes['total_items'] : $collection->getTotalItemCount())); } else { $payload = array($collectionName => $this->getNormalizer()->normalize($collection, $normalizationGroups)); if (is_array($collection) || $collection instanceof Countable) { $payload['total_items'] = isset($attributes['total_items']) ? $attributes['total_items'] : count($collection); } } $payload = array_merge($attributes, $payload); return $payload; }
/** * "Render" a Collection * * Injects pagination links, if the composed collection is a Paginator, and * then loops through the collection to create the data structure representing * the collection. * * For each entity in the collection, the event "renderCollection.entity" is * triggered, with the following parameters: * * - "collection", which is the $halCollection passed to the method * - "entity", which is the current entity * - "route", the resource route that will be used to generate links * - "routeParams", any default routing parameters/substitutions to use in URL assembly * - "routeOptions", any default routing options to use in URL assembly * * This event can be useful particularly when you have multi-segment routes * and wish to ensure that route parameters are injected, or if you want to * inject query or fragment parameters. * * Event parameters are aggregated in an ArrayObject, which allows you to * directly manipulate them in your listeners: * * <code> * $params = $e->getParams(); * $params['routeOptions']['query'] = ['format' => 'json']; * </code> * * @param Collection $halCollection * @return array|ApiProblem Associative array representing the payload to render; * returns ApiProblem if error in pagination occurs */ public function renderCollection(Collection $halCollection) { $this->getEventManager()->trigger(__FUNCTION__, $this, ['collection' => $halCollection]); $collection = $halCollection->getCollection(); $collectionName = $halCollection->getCollectionName(); if ($collection instanceof Paginator) { $status = $this->injectPaginationLinks($halCollection); if ($status instanceof ApiProblem) { return $status; } } $metadataMap = $this->getMetadataMap(); $maxDepth = is_object($collection) && $metadataMap->has($collection) ? $metadataMap->get($collection)->getMaxDepth() : null; $payload = $halCollection->getAttributes(); $payload['_links'] = $this->fromResource($halCollection); $payload['_embedded'] = [$collectionName => $this->extractCollection($halCollection, 0, $maxDepth)]; if ($collection instanceof Paginator) { $payload['page_count'] = isset($payload['page_count']) ? $payload['page_count'] : $collection->count(); $payload['page_size'] = isset($payload['page_size']) ? $payload['page_size'] : $halCollection->getPageSize(); $payload['total_items'] = isset($payload['total_items']) ? $payload['total_items'] : (int) $collection->getTotalItemCount(); $payload['page'] = $payload['page_count'] > 0 ? $halCollection->getPage() : 0; } elseif (is_array($collection) || $collection instanceof Countable) { $payload['total_items'] = isset($payload['total_items']) ? $payload['total_items'] : count($collection); } $payload = new ArrayObject($payload); $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, ['payload' => $payload, 'collection' => $halCollection]); return (array) $payload; }
public function testCollectionNameIsMutable() { $hal = new Collection(array(), 'item/route'); $hal->setCollectionName('records'); $this->assertEquals('records', $hal->getCollectionName()); }
/** * "Render" a Collection * * Injects pagination links, if the composed collection is a Paginator, and * then loops through the collection to create the data structure representing * the collection. * * For each entity in the collection, the event "renderCollection.entity" is * triggered, with the following parameters: * * - "collection", which is the $halCollection passed to the method * - "entity", which is the current entity * - "route", the resource route that will be used to generate links * - "routeParams", any default routing parameters/substitutions to use in URL assembly * - "routeOptions", any default routing options to use in URL assembly * * This event can be useful particularly when you have multi-segment routes * and wish to ensure that route parameters are injected, or if you want to * inject query or fragment parameters. * * Event parameters are aggregated in an ArrayObject, which allows you to * directly manipulate them in your listeners: * * <code> * $params = $e->getParams(); * $params['routeOptions']['query'] = array('format' => 'json'); * </code> * * @param Collection $halCollection * @return array|ApiProblem Associative array representing the payload to render; returns ApiProblem if error in pagination occurs */ public function renderCollection(Collection $halCollection) { $this->getEventManager()->trigger(__FUNCTION__, $this, array('collection' => $halCollection)); $collection = $halCollection->getCollection(); $collectionName = $halCollection->getCollectionName(); if ($collection instanceof Paginator) { $status = $this->injectPaginationLinks($halCollection); if ($status instanceof ApiProblem) { return $status; } } $payload = $halCollection->getAttributes(); $payload['_links'] = $this->fromResource($halCollection); $payload['_embedded'] = array($collectionName => $this->extractCollection($halCollection)); if ($collection instanceof Paginator) { $payload['page_count'] = isset($payload['page_count']) ? $payload['page_count'] : $collection->count(); $payload['page_size'] = isset($payload['page_size']) ? $payload['page_size'] : $halCollection->getPageSize(); $payload['total_items'] = isset($payload['total_items']) ? $payload['total_items'] : (int) $collection->getTotalItemCount(); } elseif (is_array($collection) || $collection instanceof Countable) { $payload['total_items'] = isset($payload['total_items']) ? $payload['total_items'] : count($collection); } return $payload; }