Skip to content

Commit

Permalink
Add PaginatorIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Oct 31, 2023
1 parent 9d2811a commit eaab3b2
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/Zendesk/API/Resources/Core/SharingAgreements.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\FindAll;
use Zendesk\API\Traits\Utility\Pagination\ObpStrategy;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;

/**
Expand Down
13 changes: 9 additions & 4 deletions src/Zendesk/API/Traits/Resource/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Zendesk\API\Traits\Utility\Pagination\PaginationIterator;

trait Pagination {

/**
* Usage:
* foreach ($ticketsIterator as $ticket) {
Expand All @@ -19,15 +18,21 @@ trait Pagination {
public function iterator()
{
$strategyClass = $this->paginationStrategyClass();
$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);
}

/**
* Override this method in your resources
*
* @return string subclass of AbstractStrategy used for fetching pages
*/

protected function paginationStrategyClass() {
return CbpStrategy::class;
}

/*
/**
* @return string eg: "job_statuses"
*/
protected function resourcesKey() {
Expand Down
12 changes: 3 additions & 9 deletions src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
$this->clientList = $clientList;
$this->resourcesKey = $resourcesKey;
$this->pageSize = $pageSize;
}

abstract public function getPage();
abstract public function getPage($pageFn);
abstract public function shouldGetPage($position);
}
8 changes: 6 additions & 2 deletions src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace Zendesk\API\Traits\Utility\Pagination;

/**
* Cursor Based Pagination
* Used in paginationStrategyClass
*/
class CbpStrategy extends AbstractStrategy
{
private $afterCursor = null;
private $started = false;

public function getPage()
public function getPage($pageFn)
{
$this->started = true;
$params = ['page[size]' => $this->pageSize];
if ($this->afterCursor) {
$params['page[after]'] = $this->afterCursor;
}
$response = $this->clientList->findAll($params);
$response = $pageFn($params);

$this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null;
return $response->{$this->resourcesKey};
Expand Down
6 changes: 3 additions & 3 deletions src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Zendesk\API\Traits\Utility\Pagination;


/**
* Offset Based Pagination
* Used in paginationStrategyClass
*/
class ObpStrategy extends AbstractStrategy
{
private $pageNumber = 0;

public function getPage()
public function getPage($pageFn)
{
++$this->pageNumber;
$params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize];
$response = $this->clientList->findAll($params);
$response = $pageFn($params);

return $response->{$this->resourcesKey};
}
Expand Down
17 changes: 13 additions & 4 deletions src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -32,7 +37,7 @@ public function rewind()

public function valid()
{
$this->getPageIfNecessary();
$this->getPageIfNeeded();
return !!$this->current();
}

Expand All @@ -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);
};

$this->page = array_merge($this->page, $this->strategy->getPage($pageFn));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

/**
* Single Page (no pagination)
* Used in paginationStrategyClass
*/
class SinglePageStrategy extends AbstractStrategy
{
protected $started = false;

public function getPage()
public function getPage($pageFn)
{
$this->started = true;
$response = $this->clientList->findAll();
$response = $pageFn();

return $response->{$this->resourcesKey};
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function testFetchesTickets()
[['id' => 1], ['id' => 2]],
[['id' => 3], ['id' => 4]]
]);
$strategy = new CbpStrategy($mockTickets, 'tickets', 2);
$iterator = new PaginationIterator($strategy);
$strategy = new CbpStrategy('tickets', 2);
$iterator = new PaginationIterator($mockTickets, $strategy);

$tickets = iterator_to_array($iterator);

Expand All @@ -62,8 +62,8 @@ public function testFetchesUsers()
[['id' => 1, 'name' => 'User 1'], ['id' => 2, 'name' => 'User 2']],
[['id' => 3, 'name' => 'User 3'], ['id' => 4, 'name' => 'User 4']]
]);
$strategy = new CbpStrategy($mockUsers, 'users', 2);
$iterator = new PaginationIterator($strategy);
$strategy = new CbpStrategy('users', 2);
$iterator = new PaginationIterator($mockUsers, $strategy);

$users = iterator_to_array($iterator);

Expand Down

0 comments on commit eaab3b2

Please sign in to comment.