Skip to content

Commit

Permalink
feat: better paginated results
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Feb 7, 2024
1 parent f0b9020 commit 877a609
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/PaginatedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,68 @@

namespace Heyday\Elastica;

use SilverStripe\Control\HTTPRequest;
use \SilverStripe\ORM\PaginatedList as SilverStripePaginatedList;
/**
* Class PaginatedList
*
* @package Heyday\Elastica
*/
class PaginatedList extends \SilverStripe\ORM\PaginatedList
class PaginatedList extends SilverStripePaginatedList
{
protected $list;
protected $resultList;

public function __construct(ResultList $list, $request = [])
public function __construct(ResultList $resultList, $request = [])
{
$this->resultList = $resultList;
$this->setRequest($request);
$this->list = $list;

$start = 0;

if ($request instanceof HTTPRequest) {
if ($request->getVar('start') !== null) {
$start = (int) $request->getVar('start');
}
}

$this->list = $resultList->limit($this->getPageLength(), $start)->toArrayList();

$list = new SilverStripePaginatedList($this->list, $request);

parent::__construct($list, $request);

$this->setTotalItems($resultList->getTotalItems());
$this->setPageStart($this->getPageStart());
$this->setLimitItems(false);
}


/**
* Use the ResultList's total items method to determine this value
*
* @return int
*/
public function getTotalItems()
public function setPageStart($start)
{
if ($this->list instanceof ResultList) {
return $this->list->getTotalItems();
}
$this->list = $this->resultList->limit($this->getPageLength(), $start)->toArrayList();

return parent::getTotalItems();
return $this;
}


public function FirstItem()
public function setPageLength($length)
{
if ($this->list instanceof ResultList) {
return $this->list->getFirstItem();
}
$this->setPageLength($length);
$this->list = $this->resultList->limit($length, $this->getPageStart())->toArrayList();

return parent::getTotalItems();
return $this;
}


public function LastItem()
/**
* Use the ResultList's total items method to determine this value
*
* @return int
*/
public function getTotalItems()
{
if ($this->list instanceof ResultList) {
return $this->list->getLastItem();
if ($this->resultList instanceof ResultList) {
return $this->resultList->getTotalItems();
}

return parent::getTotalItems();
Expand Down
1 change: 1 addition & 0 deletions src/ResultList.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public function getFirstItem()
try {
$from = $this->getQuery()->getParam('from');
} catch (Exception $e) {
dd($e);
$from = 1;
}

Expand Down

0 comments on commit 877a609

Please sign in to comment.