-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination Iterator with params #516
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,18 @@ abstract class AbstractStrategy | |
{ | ||
public const DEFAULT_PAGE_SIZE = 100; | ||
|
||
/* | ||
* @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()` | ||
*/ | ||
protected $clientList; | ||
|
||
/* | ||
/** | ||
* @var string The response key where the data is returned | ||
*/ | ||
protected $resourcesKey; | ||
protected $pageSize; | ||
|
||
public function __construct($clientList, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) | ||
public function __construct($resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now the Strategies are more cohesive. |
||
{ | ||
$this->clientList = $clientList; | ||
$this->resourcesKey = $resourcesKey; | ||
$this->pageSize = $pageSize; | ||
} | ||
|
||
abstract public function getPage(); | ||
abstract public function getPage($pageFn); | ||
abstract public function shouldGetPage($position); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,14 @@ class PaginationIterator implements Iterator | |
private $position = 0; | ||
private $page = []; | ||
private $strategy; | ||
/** | ||
* @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()` | ||
*/ | ||
private $clientList; | ||
|
||
public function __construct(AbstractStrategy $strategy) | ||
public function __construct($clientList, AbstractStrategy $strategy) | ||
{ | ||
$this->clientList = $clientList; | ||
$this->strategy = $strategy; | ||
} | ||
|
||
|
@@ -32,7 +37,7 @@ public function rewind() | |
|
||
public function valid() | ||
{ | ||
$this->getPageIfNecessary(); | ||
$this->getPageIfNeeded(); | ||
return !!$this->current(); | ||
} | ||
|
||
|
@@ -45,12 +50,16 @@ public function current() | |
} | ||
} | ||
|
||
private function getPageIfNecessary() | ||
private function getPageIfNeeded() | ||
{ | ||
if (!$this->strategy->shouldGetPage($this->position)) { | ||
return; | ||
} | ||
|
||
$this->page = array_merge($this->page, $this->strategy->getPage()); | ||
$pageFn = function ($params = []) { | ||
return $this->clientList->findAll($params); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more DRY, so adding params and later maybe making the method dynamic ( |
||
|
||
$this->page = array_merge($this->page, $this->strategy->getPage($pageFn)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of passing
$this
for the strategy to runfindAll()
duplicated in every strategy insidegetPage()
, extract it in the Iterator as a function for the strategy to run (see below).