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);
 }
Beispiel #2
0
 public function testUrls()
 {
     $numItems = 123;
     $currentPage = 5;
     $numItemsPerPage = 10;
     $currentPageInput = new PageInput($currentPage, $numItemsPerPage);
     $pageRange = new PageRange($currentPageInput);
     $paginator = new Paginator($pageRange->setTotalItems($numItems));
     $paginator->setBaseUrl('/my-url')->setPageParam('page')->addParam('aa', 'abc')->addParams(array('bb' => 22));
     #print_r($paginator->toArray()); exit;
     $this->assertFalse($paginator->isOutOfBounds(), 'isOutOfBounds()');
     $this->assertSame(123, $paginator->getTotalItems(), 'getTotalItems()');
     $this->assertSame(10, $paginator->getNumItemsPerPage(), 'getNumItemsPerPage()');
     $this->assertSame(10, $paginator->getNumItemsOnCurrentPage(), 'getNumItemsOnCurrentPage()');
     $this->assertSame(41, $paginator->getFirstItemOnCurrentPage(), 'getFirstItemOnCurrentPage()');
     $this->assertSame(50, $paginator->getLastItemOnCurrentPage(), 'getLastItemOnCurrentPage()');
     $this->assertSame(13, $paginator->getNumPages(), 'getNumPages()');
     $this->assertSame(5, $paginator->getCurrentPage(), 'getCurrentPage()');
     $this->assertSame(4, $paginator->getPreviousPage(), 'getPreviousPage()');
     $this->assertSame(6, $paginator->getNextPage(), 'getNextPage()');
     $this->assertSame(1, $paginator->getFirstPage(), 'getFirstPage()');
     $this->assertSame(13, $paginator->getLastPage(), 'getLastPage()');
     $this->assertSame(40, $paginator->getOffset(), 'getOffset()');
     $this->assertSame(49, $paginator->getLast(), 'getLast()');
     $this->assertSame('/my-url?aa=abc&bb=22&page=5', $paginator->getCurrentPageUrl(), 'getCurrentPageUrl()');
     $this->assertSame('/my-url?aa=abc&bb=22&page=4', $paginator->getPreviousPageUrl(), 'getPreviousPageUrl()');
     $this->assertSame('/my-url?aa=abc&bb=22&page=6', $paginator->getNextPageUrl(), 'getNextPageUrl()');
     $this->assertSame('/my-url?aa=abc&bb=22&page=1', $paginator->getFirstPageUrl(), 'getFirstPageUrl()');
     $this->assertSame('/my-url?aa=abc&bb=22&page=13', $paginator->getLastPageUrl(), 'getLastPageUrl()');
 }