Skip to content

Commit b7522f3

Browse files
author
Jamie Hannaford
committed
Add new iterator to handle differences
1 parent 7f45e9b commit b7522f3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace OpenCloud\LoadBalancer\Collection;
4+
5+
use OpenCloud\Common\Collection\PaginatedIterator;
6+
7+
class LoadBalancerIterator extends PaginatedIterator
8+
{
9+
private $nextElement;
10+
11+
public function constructNextUrl()
12+
{
13+
$url = parent::constructNextUrl();
14+
15+
// We need to return n+1 items in order to grab the relevant marker value
16+
$query = $url->getQuery();
17+
$query['limit'] = $query['limit'] + 1;
18+
$url->setQuery($query);
19+
20+
return $url;
21+
}
22+
23+
public function updateMarkerToCurrent()
24+
{
25+
$this->setMarkerFromElement($this->nextElement);
26+
}
27+
28+
public function parseResponseBody($body)
29+
{
30+
$response = parent::parseResponseBody($body);
31+
32+
if (count($response) >= $this->getOption('limit.page')) {
33+
// Save last element (we will need it for the next marker)
34+
$this->nextElement = end($response);
35+
36+
// Since we previously asked for n+1 elements, pop the unwanted element
37+
array_pop($response);
38+
reset($response);
39+
}
40+
41+
return $response;
42+
}
43+
}

0 commit comments

Comments
 (0)