public function getList(Criteria $criteria = null, array $params = array()) { $page = array_key_exists('page', $params) ? (int) $params['page'] : 1; $itemsPerPage = array_key_exists('itemsPerPage', $params) ? (int) $params['itemsPerPage'] : $this->itemsPerPage; $pageInput = new PageInput($page, $itemsPerPage); $pageRange = new PageRange($pageInput); $sorting = array_key_exists('sorting', $params) ? $params['sorting'] : 'created'; $descending = array_key_exists('descending', $params) ? (bool) $params['descending'] : true; $criteria = $criteria ? $criteria : new Criteria(); $mode = $criteria->getMode() ? $criteria->getMode() : $this->mode; // criteria $criteria->setMode($mode); // get all scribbles $allScribbles = $this->getDirectory()->load()->getScribbles(); // set sorting if ($sorting == 'created') { $allScribbles->sortByCreationDate($descending); } else { if ($sorting == 'modified') { $allScribbles->sortByModificationDate($descending); } else { if ($sorting == 'slug') { $allScribbles->sortBySlug($descending); } } } // filter and paginate $filter = new Filter(); $scribbles = $filter->apply($allScribbles, $criteria)->getScribbles(); $paginator = new Paginator($pageRange->setTotalItems($scribbles->count())); $scribbles = new ScribbleCollection($scribbles->slice($pageRange->getOffset(), $pageRange->getNumItems())); return new ScribbleListBag($scribbles, $filter, $criteria, $paginator); }
public function testSecondPge() { $currentPage = 2; $numItemsPerPage = 10; $currentPageInput = new PageInput($currentPage, $numItemsPerPage); $pageRange = new PageRange($currentPageInput); $this->assertSame(10, $pageRange->getNumItems(), 'getNumItems()'); $this->assertSame(10, $pageRange->getOffset(), 'getOffset()'); $this->assertSame(19, $pageRange->getLast(), 'getLast()'); }
public function __construct(PageRange $pageRange) { $currentPage = $pageRange->getCurrentPage(); $numItemsPerPage = $pageRange->getNumItems(); $totalItems = $pageRange->getTotalItems(); if (!$totalItems) { $this->isOutOfBounds = true; $this->numItemsPerPage = $numItemsPerPage; return; } $isOutOfBounds = $currentPage > ceil($totalItems / $numItemsPerPage); // pages $numPages = ceil($totalItems / $numItemsPerPage); $currentPage = $isOutOfBounds ? 1 : $currentPage; // go to page 1 if out of bounds $previousPage = $currentPage == 1 ? $numPages : $currentPage - 1; // flip around if at the beginning $nextPage = $currentPage == $numPages ? 1 : $currentPage + 1; // flip around if at the end $firstPage = 1; $lastPage = $numPages; // num items on current page if ($currentPage == $numPages && $currentPage > 1) { $numItemsOnCurrentPage = $totalItems % $numItemsPerPage; } else { if ($totalItems < $numItemsPerPage) { $numItemsOnCurrentPage = $totalItems; } else { $numItemsOnCurrentPage = $numItemsPerPage; } } // indices (start at 0) $offset = ($currentPage - 1) * $numItemsPerPage; $last = $offset + $numItemsOnCurrentPage - 1; // items $firstItemOnCurrentPage = $offset + 1; $lastItemOnCurrentPage = $last + 1; // set instance vars $this->isOutOfBounds = $isOutOfBounds; $this->totalItems = (int) $totalItems; $this->numItemsPerPage = (int) $numItemsPerPage; $this->numItemsOnCurrentPage = (int) $numItemsOnCurrentPage; $this->firstItemOnCurrentPage = (int) $firstItemOnCurrentPage; $this->lastItemOnCurrentPage = (int) $lastItemOnCurrentPage; $this->numPages = (int) $numPages; $this->currentPage = (int) $currentPage; $this->previousPage = (int) $previousPage; $this->nextPage = (int) $nextPage; $this->firstPage = (int) $firstPage; $this->lastPage = (int) $lastPage; $this->offset = (int) $offset; $this->last = (int) $last; return $this; }