1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A SimplePaginatedList is a list that is paged using the `offset` |
7
|
|
|
* and `limit` parameters and that provides a `totalRecordCount` in the first response, |
8
|
|
|
* so that we can return a count without having to retrieve all the pages. |
9
|
|
|
* A list which is not of this type is the Analytics report row list. |
10
|
|
|
*/ |
11
|
|
|
abstract class SimplePaginatedList extends LazyResourceList |
12
|
|
|
{ |
13
|
|
|
use PaginatedList; |
14
|
|
|
|
15
|
|
|
/* @var integer */ |
16
|
|
|
protected $offset = 0; |
17
|
|
|
|
18
|
|
|
/* @var integer */ |
19
|
|
|
protected $limit = 10; |
20
|
|
|
|
21
|
|
|
protected function fetchBatch() |
22
|
|
|
{ |
23
|
|
|
if (!is_null($this->totalRecordCount) && $this->offset >= $this->totalRecordCount) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$url = $this->url('', [ |
28
|
|
|
'offset' => $this->offset, |
29
|
|
|
'limit' => $this->limit, |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$response = $this->client->getJSON($url); |
33
|
|
|
|
34
|
|
|
if (is_null($response)) { |
35
|
|
|
throw new \RuntimeException("Empty response from URL: $url"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->onData($response); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function fetchData() |
42
|
|
|
{ |
43
|
|
|
do { |
44
|
|
|
$this->fetchBatch(); |
45
|
|
|
} while (!$this->isInitialized($this->resources)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function onData($data) |
49
|
|
|
{ |
50
|
|
|
$before = count($this->resources); |
51
|
|
|
parent::onData($data); |
52
|
|
|
$after = count($this->resources); |
53
|
|
|
$this->offset += $after - $before; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check if we have the full representation of our data object. |
58
|
|
|
* |
59
|
|
|
* @param \stdClass $data |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
protected function isInitialized($data) |
64
|
|
|
{ |
65
|
|
|
if (is_countable($data)) { |
66
|
|
|
return count($data) === $this->totalRecordCount; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Total number of resources. |
74
|
|
|
* |
75
|
|
|
* @link http://php.net/manual/en/countable.count.php |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function count() |
80
|
|
|
{ |
81
|
|
|
if (is_null($this->totalRecordCount)) { |
82
|
|
|
$this->fetchBatch(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->totalRecordCount; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Mutate the pagination limit |
90
|
|
|
* |
91
|
|
|
* @param int $limit Maximum number of items per page (0 - 100) |
92
|
|
|
* @throws \RuntimeException |
93
|
|
|
*/ |
94
|
|
|
public function setPaginationLimit($limit) |
95
|
|
|
{ |
96
|
|
|
if ((int)$limit < 0 || (int)$limit > 100) { |
97
|
|
|
throw new \RuntimeException('Invalid limit value (0 - 100): '.$limit); |
98
|
|
|
} |
99
|
|
|
$this->limit = (int) $limit; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|