Skip to content
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

Merged
merged 2 commits into from
Nov 1, 2023
Merged

Pagination Iterator with params #516

merged 2 commits into from
Nov 1, 2023

Conversation

ecoologic
Copy link
Collaborator

@ecoologic ecoologic commented Oct 31, 2023

It's now possible to call the iterator with params as such:

$it = $client->tickets()->iterator(['extra' => 'params']);

foreach ($it as $i) {
    process($i); // Your implementation
}

I will update the README in the next card, as I'll probably start the upgrade guide then too.

$strategy = new $strategyClass($this, $this->resourcesKey(), AbstractStrategy::DEFAULT_PAGE_SIZE);
return new PaginationIterator($strategy);
$strategy = new $strategyClass($this->resourcesKey(), AbstractStrategy::DEFAULT_PAGE_SIZE);
return new PaginationIterator($this, $strategy);
Copy link
Collaborator Author

@ecoologic ecoologic Oct 31, 2023

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 run findAll() duplicated in every strategy inside getPage(), extract it in the Iterator as a function for the strategy to run (see below).

$this->page = array_merge($this->page, $this->strategy->getPage());
$pageFn = function ($params = []) {
return $this->clientList->findAll($params);
};
Copy link
Collaborator Author

@ecoologic ecoologic Oct 31, 2023

Choose a reason for hiding this comment

The 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 (findAll, find, active, etc) in one place only instead of each Strategy.

* @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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the Strategies are more cohesive.

@ecoologic ecoologic marked this pull request as ready for review November 1, 2023 05:34
@ecoologic ecoologic requested a review from a team November 1, 2023 05:34
@ecoologic ecoologic merged commit 3bf3aa8 into master Nov 1, 2023
2 checks passed
@ecoologic ecoologic deleted the RED-2001-params branch November 1, 2023 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants