1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\eCurring\Resource; |
6
|
|
|
|
7
|
|
|
use Iterator; |
8
|
|
|
use LauLamanApps\eCurring\eCurringClientInterface; |
9
|
|
|
use LauLamanApps\eCurring\Resource\Exception\NonExistentPageNumberException; |
10
|
|
|
|
11
|
|
|
abstract class Cursor implements Iterator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var eCurringClientInterface |
15
|
|
|
*/ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $currentPage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $totalPages; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $position; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Customer[]|Subscription[]|Product[]|Transaction[] |
35
|
|
|
*/ |
36
|
|
|
private $objects; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
* |
41
|
|
|
* Automatically load next pages when iterating over objects |
42
|
|
|
*/ |
43
|
|
|
private $autoload; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
private $itemsPerPage; |
49
|
|
|
|
50
|
|
|
public function __construct( |
51
|
|
|
eCurringClientInterface $client, |
52
|
|
|
int $currentPage, |
53
|
|
|
int $totalPages, |
54
|
|
|
array $objects, |
55
|
|
|
?int $itemsPerPage = 10, |
56
|
|
|
?bool $autoload = true |
57
|
|
|
) { |
58
|
|
|
$this->client = $client; |
59
|
|
|
$this->currentPage = $currentPage; |
60
|
|
|
$this->totalPages = $totalPages; |
61
|
|
|
$this->position = 0; |
62
|
|
|
$this->objects = $objects; |
63
|
|
|
$this->itemsPerPage = $itemsPerPage; |
64
|
|
|
$this->autoload = $autoload; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getAll() |
68
|
|
|
{ |
69
|
|
|
return $this->objects; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function current() |
73
|
|
|
{ |
74
|
|
|
return $this->objects[$this->position]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function next(): void |
78
|
|
|
{ |
79
|
|
|
++$this->position; |
80
|
|
|
|
81
|
|
|
if (!$this->valid() && $this->autoload) { |
82
|
|
|
$this->nextPage(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function key(): int |
87
|
|
|
{ |
88
|
|
|
return $this->position; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function valid(): bool |
92
|
|
|
{ |
93
|
|
|
return isset($this->objects[$this->position]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function rewind(): void |
97
|
|
|
{ |
98
|
|
|
$this->position = 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function disableAutoload(): void |
102
|
|
|
{ |
103
|
|
|
$this->autoload = false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function enableAutoload(): void |
107
|
|
|
{ |
108
|
|
|
$this->autoload = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @throws NonExistentPageNumberException |
113
|
|
|
*/ |
114
|
|
|
public function firstPage(): void |
115
|
|
|
{ |
116
|
|
|
$this->loadPage(0, $this->itemsPerPage); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @throws NonExistentPageNumberException |
121
|
|
|
*/ |
122
|
|
|
public function nextPage(): void |
123
|
|
|
{ |
124
|
|
|
$pageNumber = $this->currentPage +1; |
125
|
|
|
if ($pageNumber >= $this->totalPages) { |
126
|
|
|
$this->loadPage($pageNumber, $this->itemsPerPage); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @throws NonExistentPageNumberException |
132
|
|
|
*/ |
133
|
|
|
public function previousPage(): void |
134
|
|
|
{ |
135
|
|
|
$pageNumber = $this->currentPage -1; |
136
|
|
|
if ($pageNumber < 0) { |
137
|
|
|
$this->loadPage($pageNumber, $this->itemsPerPage); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @throws NonExistentPageNumberException |
143
|
|
|
*/ |
144
|
|
|
public function goToPage(int $pageNumber): void |
145
|
|
|
{ |
146
|
|
|
if ($pageNumber < 0 && $pageNumber >= $this->totalPages) { |
147
|
|
|
$this->loadPage($pageNumber, $this->itemsPerPage); |
148
|
|
|
} else { |
149
|
|
|
throw new NonExistentPageNumberException(sprintf('Page number #%s is invalid', $pageNumber)); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @throws NonExistentPageNumberException |
155
|
|
|
*/ |
156
|
|
|
public function lastPage(): void |
157
|
|
|
{ |
158
|
|
|
$this->loadPage($this->totalPages, $this->itemsPerPage); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @throws NonExistentPageNumberException |
163
|
|
|
*/ |
164
|
|
|
private function loadPage(int $pageNumber, int $itemsPerPage): void |
165
|
|
|
{ |
166
|
|
|
$this->objects = $this->getPageData($pageNumber, $itemsPerPage)->getAll(); |
167
|
|
|
$this->position = 0; |
168
|
|
|
$this->currentPage = $pageNumber; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
abstract protected function getPageData(int $pageNumber, int $itemsPerPage): self; |
172
|
|
|
} |
173
|
|
|
|