public function testContentRangeToStringReturnsHeaderFormattedString() { $this->markTestIncomplete('ContentRange needs to be completed'); $contentRangeHeader = new ContentRange(); // @todo set some values, then test output $this->assertEmpty('Content-Range: xxx', $contentRangeHeader->toString()); }
public function getList(MvcEvent $event) { $options = $event->getTarget()->getOptions(); $documentManager = $options->getModelManager(); $metadata = $documentManager->getClassMetadata($options->getClass()); if ($list = $event->getParam('list')) { $list = $list->getValues(); } unset($this->range); $criteria = $this->getCriteria($event); //filter list on criteria if (count($criteria) > 0 && $list) { $list = $this->applyCriteriaToList($list, $criteria, $metadata); } if ($list) { $total = count($list); } else { //load the total from the db $totalQuery = $documentManager->createQueryBuilder()->find($metadata->name); $total = $this->addCriteriaToQuery($totalQuery, $criteria, $metadata, $documentManager)->getQuery()->execute()->count(); } if ($total == 0) { $result = new Result([]); $result->setStatusCode(204); $event->setResult($result); return $result; } $offset = $this->getOffset($event); if ($offset > $total - 1) { throw new Exception\BadRangeException(); } $sort = $this->getSort($event); if ($list) { //apply any required sort to the result if (count($sort) > 0) { $this->applySortToList($list, $sort, $metadata); } $list = array_slice($list, $offset, $this->getLimit($event)); } else { $resultsQuery = $documentManager->createQueryBuilder()->find($metadata->name); $this->addCriteriaToQuery($resultsQuery, $criteria, $metadata, $documentManager); $resultsQuery->limit($this->getLimit($event))->skip($offset); $list = $this->addSortToQuery($resultsQuery, $sort)->eagerCursor(true)->getQuery()->execute(); } $max = $offset + count($list) - 1; $result = new Result($list); $result->addHeader(ContentRange::fromString("Content-Range: {$offset}-{$max}/{$total}")); $event->setResult($result); return $result; }
/** * Hydrate $object with the provided $data. * * @param array $data * @param object $object * @return object */ public function hydrate(array $data, $object) { if (!$object instanceof Headers) { throw new InvalidArgumentException("Invalid object, Headers object expected"); } if (count(array_diff(array('from', 'to'), array_keys($data)))) { throw new InvalidArgumentException("Invalid data array, must have at least from and to keys."); } $count = 0; if (array_key_exists('totalCount', $data)) { $count = $data['totalCount']; } $string = "Content-Range: items {$data['from']}-{$data['to']}/{$count}"; $rangeHeader = ContentRange::fromString($string); $object->addHeader($rangeHeader); return $object; }
/** * If list array is supplied, it will be filtered and sorted in php. * If list is empty, it will be loaded from the db, (filter and sort will be applied by the db). * * If metadata is not suppled, it will be retrieved using $this->options->getDocumentClass() * * @param array $list * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $metadata * @return type */ protected function doGetList(ClassMetadata $metadata, $list = null) { $criteria = $this->getCriteria($metadata); //filter list on criteria if (count($criteria) > 0 && $list) { $list = $this->applyCriteriaToList($list, $criteria); } if ($list) { $total = count($list); } else { //load the total from the db $totalQuery = $this->options->getDocumentManager()->createQueryBuilder()->find($metadata->name); $total = $this->addCriteriaToQuery($totalQuery, $criteria)->getQuery()->execute()->count(); } if ($total == 0) { return []; } $offset = $this->getOffset(); if ($offset > $total - 1) { throw new Exception\BadRangeException(); } $sort = $this->getSort(); $serializer = $this->options->getSerializer(); if ($list) { //apply any required sort to the result if (count($sort) > 0) { $this->applySortToList($list, $sort); } $list = array_slice($list, $offset, $this->getLimit()); foreach ($list as $item) { $items[] = $serializer->applySerializeMetadataToArray($item, $metadata->name); } } else { $resultsQuery = $this->options->getDocumentManager()->createQueryBuilder()->find($metadata->name); $this->addCriteriaToQuery($resultsQuery, $criteria); $resultsQuery->limit($this->getLimit())->skip($offset); $resultsCursor = $this->addSortToQuery($resultsQuery, $sort)->eagerCursor(true)->getQuery()->execute(); foreach ($resultsCursor as $result) { $items[] = $this->options->getSerializer()->toArray($result, $metadata->name); } } $max = $offset + count($items) - 1; $this->response->getHeaders()->addHeader(ContentRange::fromString("Content-Range: {$offset}-{$max}/{$total}")); return $items; }