|
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
|
|
|
class ListResource extends ApiResource implements IteratorAggregate { |
|
16
|
|
|
|
|
17
|
|
|
const PAGE_LENGTH = 20; |
|
18
|
|
|
const PAGE_PARAM = 'page'; |
|
19
|
|
|
const LIMIT_PARAM = 'limit'; |
|
20
|
|
|
|
|
21
|
|
|
private string $_url; |
|
22
|
|
|
private array $_params; |
|
23
|
|
|
private array $_items; |
|
24
|
|
|
private int $_max_items; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ListResource constructor. |
|
28
|
|
|
* @param Connector $client |
|
29
|
|
|
* @param string $url |
|
30
|
|
|
* @param array $params |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Connector $client, string $url, array $params = []) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($client); |
|
35
|
|
|
$this->setURL($url); |
|
36
|
|
|
$this->setParams($params); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $params |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setParams(array $params = []): ListResource |
|
44
|
|
|
{ |
|
45
|
|
|
$this->_params = $params; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getParams(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->_params; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $url |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setURL(string $url): ListResource |
|
62
|
|
|
{ |
|
63
|
|
|
$this->_url = $url; |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getURL(): string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->_url; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param int $page |
|
77
|
|
|
* @return array |
|
78
|
|
|
* @throws OAuthException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getItems(int $page = 1): array |
|
81
|
|
|
{ |
|
82
|
|
|
if (!array_key_exists($page, $this->_items)) { |
|
83
|
|
|
$response = $this->getClient()->curl_api($this->getURL(), 'GET', [ |
|
84
|
|
|
self::PAGE_PARAM => $page, |
|
85
|
|
|
self::LIMIT_PARAM => self::PAGE_LENGTH |
|
86
|
|
|
]); |
|
87
|
|
|
|
|
88
|
|
|
$data = ($response->isValid() && $response->isJSON()) ? $response->getJSON() : []; |
|
89
|
|
|
$this->_max_items = $data['available_items']; |
|
90
|
|
|
|
|
91
|
|
|
foreach ($data['items'] as $item) { |
|
92
|
|
|
$obj = ObjectFactory::make($this->getClient(), $item); |
|
|
|
|
|
|
93
|
|
|
if (!$obj) continue; |
|
94
|
|
|
|
|
95
|
|
|
$this->_items[$page][] = $obj; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->_items[$page]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param mixed $offset |
|
104
|
|
|
* @return ObjectResource|null |
|
105
|
|
|
* @throws OAuthException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function offsetGet($offset): ?ObjectResource |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->getItems()[$offset] ?? null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param mixed $offset |
|
114
|
|
|
* @param mixed $value |
|
115
|
|
|
* @throws OAuthException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function offsetSet($offset, $value) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->getItems()[$offset] = $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return int |
|
124
|
|
|
*/ |
|
125
|
|
|
public function count(): int |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->_max_items; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param mixed $offset |
|
132
|
|
|
* @return bool |
|
133
|
|
|
* @throws OAuthException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function offsetExists($offset): bool |
|
136
|
|
|
{ |
|
137
|
|
|
return array_key_exists($offset, $this->getItems()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param mixed $offset |
|
142
|
|
|
* @throws OAuthException |
|
143
|
|
|
*/ |
|
144
|
|
|
public function offsetUnset($offset) |
|
145
|
|
|
{ |
|
146
|
|
|
unset($this->getItems()[$offset]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return Generator |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getIterator(): Generator |
|
153
|
|
|
{ |
|
154
|
|
|
return (function () { |
|
155
|
|
|
$i = 0; |
|
156
|
|
|
$x = 0; |
|
157
|
|
|
$m = $this->count(); |
|
158
|
|
|
|
|
159
|
|
|
while ($i < $m) { |
|
160
|
|
|
if ($x === 0) { |
|
161
|
|
|
$x = self::PAGE_LENGTH; |
|
162
|
|
|
$t = $this->getItems(ceil($i / self::PAGE_LENGTH)); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
yield $i => $t[self::PAGE_LENGTH - $x]; |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$x -= 1; |
|
168
|
|
|
} |
|
169
|
|
|
})(); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|