1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat\Resource; |
4
|
|
|
|
5
|
|
|
use Lifeboat\Connector; |
6
|
|
|
use Lifeboat\Exceptions\OAuthException; |
7
|
|
|
use Lifeboat\Services\ObjectFactory; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use Generator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ResourceList |
13
|
|
|
* @package Lifeboat\Resource |
14
|
|
|
* |
15
|
|
|
* @property string $_url |
16
|
|
|
* @property array $_items |
17
|
|
|
*/ |
18
|
|
|
class ListResource extends ApiResource implements IteratorAggregate { |
19
|
|
|
|
20
|
|
|
const PAGE_LENGTH = 20; |
21
|
|
|
const PAGE_PARAM = 'page'; |
22
|
|
|
const LIMIT_PARAM = 'limit'; |
23
|
|
|
|
24
|
|
|
private $_url = ''; |
25
|
|
|
private $_items = []; |
26
|
|
|
private $_max_items = 0; |
27
|
|
|
|
28
|
|
|
public function __construct(Connector $connector, string $url) |
29
|
|
|
{ |
30
|
|
|
$this->setClient($connector); |
31
|
|
|
$this->setURL($url); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $url |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function setURL(string $url): ListResource |
39
|
|
|
{ |
40
|
|
|
$this->_url = $url; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getURL(): string |
48
|
|
|
{ |
49
|
|
|
return $this->_url; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $page |
54
|
|
|
* @return array |
55
|
|
|
* @throws OAuthException |
56
|
|
|
*/ |
57
|
|
|
public function getItems(int $page = 1): array |
58
|
|
|
{ |
59
|
|
|
if (!array_key_exists($page, $this->_items)) { |
60
|
|
|
$response = $this->getClient()->curl_api($this->getURL(), 'GET', [ |
61
|
|
|
self::PAGE_PARAM => $page, |
62
|
|
|
self::LIMIT_PARAM => self::PAGE_LENGTH |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$data = ($response->isValid() && $response->isJSON()) ? $response->getJSON() : []; |
66
|
|
|
$this->_max_items = $data['available_items']; |
67
|
|
|
|
68
|
|
|
foreach ($data['items'] as $item) { |
69
|
|
|
$obj = ObjectFactory::make($this->getClient(), $item); |
70
|
|
|
if (!$obj) continue; |
71
|
|
|
|
72
|
|
|
$this->_items[$page][] = $obj; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->_items[$page]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $offset |
81
|
|
|
* @return ObjectResource|null |
82
|
|
|
* @throws OAuthException |
83
|
|
|
*/ |
84
|
|
|
public function offsetGet($offset): ?ObjectResource |
85
|
|
|
{ |
86
|
|
|
return $this->getItems()[$offset] ?? null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $offset |
91
|
|
|
* @param mixed $value |
92
|
|
|
* @throws OAuthException |
93
|
|
|
*/ |
94
|
|
|
public function offsetSet($offset, $value) |
95
|
|
|
{ |
96
|
|
|
$this->getItems()[$offset] = $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function count(): int |
103
|
|
|
{ |
104
|
|
|
return $this->_max_items; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $offset |
109
|
|
|
* @return bool |
110
|
|
|
* @throws OAuthException |
111
|
|
|
*/ |
112
|
|
|
public function offsetExists($offset): bool |
113
|
|
|
{ |
114
|
|
|
return array_key_exists($offset, $this->getItems()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed $offset |
119
|
|
|
* @throws OAuthException |
120
|
|
|
*/ |
121
|
|
|
public function offsetUnset($offset) |
122
|
|
|
{ |
123
|
|
|
unset($this->getItems()[$offset]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Generator |
128
|
|
|
*/ |
129
|
|
|
public function getIterator(): Generator |
130
|
|
|
{ |
131
|
|
|
return (function () { |
132
|
|
|
$i = 0; |
133
|
|
|
$x = 0; |
134
|
|
|
$m = $this->count(); |
135
|
|
|
|
136
|
|
|
while ($i < $m) { |
137
|
|
|
if ($x === 0) { |
138
|
|
|
$x = self::PAGE_LENGTH; |
139
|
|
|
$t = $this->getItems(ceil($i / self::PAGE_LENGTH)); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
yield $i => $t[self::PAGE_LENGTH - $x]; |
|
|
|
|
143
|
|
|
|
144
|
|
|
$x -= 1; |
145
|
|
|
} |
146
|
|
|
})(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|