/** * Get records from DB - REST ready * @param array $parameters * @param array $options */ public function restGet(array $parameters = null, array $options = null, $page = 1, $limit = 10) { //This method calls the apropriate find method, according to the subclass that the restGet() call originated from. //For example if restGet() was called from a HashtahManager object then the HashtagManager::find() method is called. $objects = $this->find($parameters); if (is_array($objects)) { $result = $objects; } else { //Apply the toArray() method to each object(article/category/hashtag/user) to get all related information $result = $objects->filter(function ($object) { return $object->toArray(); }); } $paginator = new \Phalcon\Paginator\Adapter\NativeArray(['data' => $result, 'limit' => $limit, 'page' => $page]); $data = $paginator->getPaginate(); if ($data->total_items > 0) { return $data; } //If the query contains an id but no result with that id was found if (isset($parameters['bind']['id'])) { throw new \Exception('Not found', 404); } else { throw new \Exception('No Content', 204); } }
public function testArrayPaginator_t445() { $paginator = new \Phalcon\Paginator\Adapter\NativeArray(array("data" => array_fill(0, 30, 'banana'), "limit" => 25, "page" => 1)); $page = $paginator->getPaginate(); $this->assertEquals(get_class($page), 'stdClass'); $this->assertEquals(count($page->items), 25); $this->assertEquals($page->before, 1); $this->assertEquals($page->next, 2); $this->assertEquals($page->last, 2); $this->assertEquals($page->current, 1); $this->assertEquals($page->total_pages, 2); $paginator->setCurrentPage(2); $page = $paginator->getPaginate(); $this->assertEquals(get_class($page), 'stdClass'); $this->assertEquals(count($page->items), 5); $this->assertEquals($page->before, 1); $this->assertEquals($page->next, 2); $this->assertEquals($page->last, 2); $this->assertEquals($page->current, 2); $this->assertEquals($page->total_pages, 2); }
/** * Paginate results * * @access public * @return array * * @author Christian Esperar <*****@*****.**> */ public function paginate($list, $currentPage, $limit = 10) { //Passing an array as data $paginator = new \Phalcon\Paginator\Adapter\NativeArray(array("data" => $list, "limit" => $limit, "page" => $currentPage)); // Return the paginated results return $paginator->getPaginate(); }