Skip to content

Commit f411993

Browse files
committed
ENHANCE Prevent possible loop if the API does return the last link while it should not
1 parent 92ea7a0 commit f411993

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Pagination/Page.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,30 @@ class Page implements PageInterface
2828
/** @var string */
2929
protected $nextLink;
3030

31+
/** @var string|null */
32+
protected $selfLink;
33+
3134
/** @var int */
3235
protected $count;
3336

3437
/** @var array */
35-
protected $items;
38+
protected $items;
3639

3740
/**
3841
* @param string $firstLink
3942
* @param null|string $previousLink
4043
* @param null|string $nextLink
44+
* @param null|string $selfLink
4145
* @param null|int $count
4246
*/
43-
public function __construct(PageFactoryInterface $pageFactory, HttpClientInterface $httpClient, $firstLink, $previousLink, $nextLink, $count, array $items)
47+
public function __construct(PageFactoryInterface $pageFactory, HttpClientInterface $httpClient, $firstLink, $previousLink, $nextLink, $selfLink, $count, array $items)
4448
{
4549
$this->pageFactory = $pageFactory;
4650
$this->httpClient = $httpClient;
4751
$this->firstLink = $firstLink;
4852
$this->previousLink = $previousLink;
4953
$this->nextLink = $nextLink;
54+
$this->selfLink = $selfLink;
5055
$this->count = $count;
5156
$this->items = $items;
5257
}
@@ -96,7 +101,7 @@ public function getItems()
96101
*/
97102
public function hasNextPage()
98103
{
99-
return null !== $this->nextLink;
104+
return null !== $this->nextLink && $this->selfLink != $this->nextLink;
100105
}
101106

102107
/**

src/Pagination/PageFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function createPage(array $data)
3535
$nextLink = $data['_links']['next']['href'] ?? null;
3636
$previousLink = $data['_links']['previous']['href'] ?? null;
3737
$firstLink = $data['_links']['first']['href'] ?? null;
38+
$selftLink = $data['_links']['self']['href'] ?? null;
3839
$count = $data['total'] ?? null;
3940
$items = $data['_embedded']['items'] ?? [];
4041

@@ -44,6 +45,7 @@ public function createPage(array $data)
4445
$firstLink,
4546
$previousLink,
4647
$nextLink,
48+
$selftLink,
4749
$count,
4850
$items
4951
);

0 commit comments

Comments
 (0)