|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lifeboat\Resource; |
|
4
|
|
|
|
|
5
|
|
|
use Lifeboat\Connector; |
|
6
|
|
|
use Lifeboat\Exceptions\OAuthException; |
|
7
|
|
|
use Lifeboat\Models\Model; |
|
8
|
|
|
use Lifeboat\Factory\ObjectFactory; |
|
9
|
|
|
use Generator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class SimpleList |
|
13
|
|
|
* |
|
14
|
|
|
* @package Lifeboat\Resource |
|
15
|
|
|
* @property Connector $_client |
|
16
|
|
|
*/ |
|
17
|
|
|
class SimpleList extends ListResource { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ListResource constructor. |
|
21
|
|
|
* @param Connector $client |
|
22
|
|
|
* @param string $url |
|
23
|
|
|
* @param array $params |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Connector $client, string $url, array $params = []) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($client, $url, $params, 0); |
|
28
|
|
|
$this->_items = ['_fetch_']; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param int $page |
|
33
|
|
|
* @return array |
|
34
|
|
|
* @throws OAuthException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getItems(int $page = 1): array |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->_items[0] === '_fetch_') { |
|
39
|
|
|
$this->_items = []; |
|
40
|
|
|
$response = $this->getClient()->curl_api($this->getURL(), 'GET', $this->getParams()); |
|
41
|
|
|
$list = ($response->isValid() && $response->isJSON()) ? $response->getJSON() : []; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($list as $item) { |
|
44
|
|
|
$obj = ObjectFactory::make($this->getClient(), $item); |
|
45
|
|
|
if (!$obj) continue; |
|
46
|
|
|
|
|
47
|
|
|
$this->_items[] = $obj; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->_max_items = count($this->_items); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->_items; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Generator |
|
58
|
|
|
* @throws OAuthException If client has a problem connecting to the API |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getIterator(): Generator |
|
61
|
|
|
{ |
|
62
|
|
|
$t = $this->getItems(); |
|
63
|
|
|
$c = $this->count(); |
|
64
|
|
|
|
|
65
|
|
|
return (function () use ($t, $c) { |
|
66
|
|
|
$i = 0; |
|
67
|
|
|
|
|
68
|
|
|
while ($i < $c) { |
|
69
|
|
|
yield $i => $t[$i]; |
|
70
|
|
|
} |
|
71
|
|
|
})(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return Model|null |
|
76
|
|
|
*/ |
|
77
|
|
|
public function first(): ?Model |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this as $obj) return $obj; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|