public function testAttributesAreMutable() { $hal = new Collection([], 'item/route'); $attributes = ['count' => 1376, 'order' => 'desc']; $hal->setAttributes($attributes); $this->assertEquals($attributes, $hal->getAttributes()); }
/** * @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; }