1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaignApi\Paginator; |
7
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\Operation\ListableResourceInterface; |
9
|
|
|
use CommerceLeague\ActiveCampaignApi\Client\HttpClientInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Page |
13
|
|
|
*/ |
14
|
|
|
class Page implements PageInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var PageFactoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $pageFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var HttpClientInterface |
23
|
|
|
*/ |
24
|
|
|
private $httpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ListableResourceInterface |
28
|
|
|
*/ |
29
|
|
|
private $listableResource; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $totalCount; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int|null |
38
|
|
|
*/ |
39
|
|
|
private $limit; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int|null |
43
|
|
|
*/ |
44
|
|
|
private $offset; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $items; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param PageFactoryInterface $pageFactory |
53
|
|
|
* @param HttpClientInterface $httpClient |
54
|
|
|
* @param ListableResourceInterface $listableResource |
55
|
|
|
* @param int $totalCount |
56
|
|
|
* @param int|null $limit |
57
|
|
|
* @param int|null $offset |
58
|
|
|
* @param array $items |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
PageFactoryInterface $pageFactory, |
62
|
|
|
HttpClientInterface $httpClient, |
63
|
|
|
ListableResourceInterface $listableResource, |
64
|
|
|
int $totalCount, |
65
|
|
|
?int $limit, |
66
|
|
|
?int $offset, |
67
|
|
|
array $items |
68
|
|
|
) { |
69
|
|
|
$this->pageFactory = $pageFactory; |
70
|
|
|
$this->httpClient = $httpClient; |
71
|
|
|
$this->listableResource = $listableResource; |
72
|
|
|
$this->totalCount = $totalCount; |
73
|
|
|
$this->limit = $limit; |
74
|
|
|
$this->offset = $offset; |
75
|
|
|
$this->items = $items; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function getNextPage(): ?PageInterface |
82
|
|
|
{ |
83
|
|
|
return $this->hasNextPage() ? $this->getPage($this->offset + $this->limit) : null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function getItems(): array |
90
|
|
|
{ |
91
|
|
|
return $this->items; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function hasNextPage(): bool |
98
|
|
|
{ |
99
|
|
|
if ($this->limit === null && $this->offset === null) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->limit + $this->offset < $this->totalCount; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int $offset |
108
|
|
|
* @return PageInterface |
109
|
|
|
*/ |
110
|
|
|
private function getPage(int $offset): PageInterface |
111
|
|
|
{ |
112
|
|
|
return $this->listableResource->listPerPage($this->limit, $offset); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|