Skip to content

Commit d1f7c81

Browse files
authored
Merge pull request #524 from zendesk/RED-1955-iterator-page
PaginationIterator storing only one page at a time
2 parents 2ce6e7f + ef32bb4 commit d1f7c81

16 files changed

+60
-25
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ foreach ($iterator as $ticket) {
155155
* Refer to the docs for details, including allowed sort fields
156156
* Combine everything: `$params = ['page[size]' => 2, 'sort' => 'updated_at', 'extra' => 'param'];`
157157

158-
**Note**: Refer to the documentation for the correct params for sorting with the pagination type you're using.
158+
**Note**:
159+
160+
* Refer to the documentation for the correct params for sorting with the pagination type you're using
161+
* The helper method `iterator_to_array` doesn't work with this implementation
159162

160163
##### Iterator API call response
161164

src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ protected function pageSize()
5353
}
5454

5555
abstract public function page($getPageFn);
56-
abstract public function shouldGetPage($position);
56+
abstract public function shouldGetPage($current_page);
5757
}

src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function page($getPageFn)
2929
return $this->latestResponse->{$this->resourcesKey};
3030
}
3131

32-
public function shouldGetPage($position) {
32+
public function shouldGetPage($current_page) {
3333
return !$this->started || $this->hasMore;
3434
}
3535

src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function page($getPageFn)
1818
return $response->{$this->resourcesKey};
1919
}
2020

21-
public function shouldGetPage($position) {
22-
return $this->pageNumber == 0 || $position >= $this->pageNumber * $this->pageSize();
21+
public function shouldGetPage($current_page) {
22+
return $this->pageNumber == 0 || count($current_page) == 0;
2323
}
2424
}

src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PaginationIterator implements Iterator
1414
private $strategy;
1515
private $method;
1616
private $position = 0;
17-
private $items = [];
17+
private $page = [];
1818

1919
/**
2020
* @param mixed using trait FindAll. The resources collection, Eg: `$client->tickets()` which uses FindAll
@@ -56,8 +56,8 @@ public function valid()
5656
#[\ReturnTypeWillChange]
5757
public function current()
5858
{
59-
if (isset($this->items[$this->position])) {
60-
return $this->items[$this->position];
59+
if (isset($this->page[$this->position])) {
60+
return $this->page[$this->position];
6161
} else {
6262
return null;
6363
}
@@ -74,14 +74,14 @@ public function latestResponse()
7474
}
7575
private function getPageIfNeeded()
7676
{
77-
if (isset($this->items[$this->position]) || !$this->strategy->shouldGetPage($this->position)) {
77+
if (isset($this->page[$this->position]) || !$this->strategy->shouldGetPage($this->page)) {
7878
return;
7979
}
8080

8181
$getPageFn = function () {
8282
return $this->clientList->{$this->method}($this->strategy->params());
8383
};
84-
85-
$this->items = array_merge($this->items, $this->strategy->page($getPageFn));
84+
$this->page = $this->strategy->page($getPageFn);
85+
$this->position = 0;
8686
}
8787
}

src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function page($getPageFn)
1919
return $response->{$this->resourcesKey};
2020
}
2121

22-
public function shouldGetPage($position) {
22+
public function shouldGetPage($current_page) {
2323
return !$this->started;
2424
}
2525
}

tests/Zendesk/API/UnitTests/BasicTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,20 @@ protected function assertEndpointCalled($userFunction, $endpoint, $method = 'GET
236236
])
237237
);
238238
}
239+
240+
/**
241+
* replacement for iterator_to_array
242+
* which doesn't work when storing with single page
243+
*
244+
* @param \Iterator $iterator
245+
* @return array all the items
246+
*/
247+
protected function iterator_to_array($iterator)
248+
{
249+
$results = [];
250+
foreach ($iterator as $item) {
251+
$results[] = $item;
252+
}
253+
return $results;
254+
}
239255
}

tests/Zendesk/API/UnitTests/Core/AutomationsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testIterator()
4040

4141
$iterator = $this->client->automations()->iterator();
4242

43-
$actual = iterator_to_array($iterator);
43+
$actual = $this->iterator_to_array($iterator);
4444
$this->assertCount(3, $actual);
4545
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
4646
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
@@ -65,7 +65,7 @@ public function testIteratorFindActive()
6565

6666
$iterator = $this->client->automations()->iterator([], 'findActive');
6767

68-
$actual = iterator_to_array($iterator);
68+
$actual = $this->iterator_to_array($iterator);
6969

7070
$this->assertLastRequestIs(
7171
[

tests/Zendesk/API/UnitTests/Core/MacrosTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testIterator()
4141

4242
$iterator = $this->client->macros()->iterator();
4343

44-
$actual = iterator_to_array($iterator);
44+
$actual = $this->iterator_to_array($iterator);
4545
$this->assertCount(3, $actual);
4646
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
4747
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);

tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testIterator()
4040

4141
$iterator = $this->client->organizationMemberships()->iterator();
4242

43-
$actual = iterator_to_array($iterator);
43+
$actual = $this->iterator_to_array($iterator);
4444
$this->assertCount(3, $actual);
4545
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
4646
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);

0 commit comments

Comments
 (0)