1 | <?php |
||
10 | class Collection implements \Iterator, \Countable |
||
11 | { |
||
12 | /** |
||
13 | * API Client. |
||
14 | * |
||
15 | * @var Client |
||
16 | */ |
||
17 | private $client; |
||
18 | |||
19 | /** |
||
20 | * Limit to give to API. |
||
21 | * |
||
22 | * @var integer |
||
23 | */ |
||
24 | private $limit; |
||
25 | |||
26 | /** |
||
27 | * Offset to give to API. |
||
28 | * |
||
29 | * @var integer |
||
30 | */ |
||
31 | private $offset; |
||
32 | |||
33 | /** |
||
34 | * Resource name for collection. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $resource; |
||
39 | |||
40 | /** |
||
41 | * Array of filters to pass to API. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $filters; |
||
46 | |||
47 | /** |
||
48 | * Total number of elements in the collection. |
||
49 | * |
||
50 | * @var integer |
||
51 | */ |
||
52 | private $total; |
||
53 | |||
54 | /** |
||
55 | * Pointer in the paginated results. |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | private $position; |
||
60 | |||
61 | /** |
||
62 | * A paginated set of elements from the API. |
||
63 | * |
||
64 | * @var null|array |
||
65 | */ |
||
66 | private $results; |
||
67 | |||
68 | /** |
||
69 | * A custom callable to return a defined type when iterating over the collection. |
||
70 | * |
||
71 | * @var callable |
||
72 | */ |
||
73 | private $loader; |
||
74 | |||
75 | /** |
||
76 | * Create a new collection. |
||
77 | * |
||
78 | * @param Client $client A client connection to the API. |
||
79 | * @param string $resource The name of API resource to request. |
||
80 | * @param array $filters A key value pair array of search filters. |
||
81 | * @param callable $loader A custom callable to use when iterating over the collection. |
||
82 | */ |
||
83 | final public function __construct(Client $client, string $resource, array $filters = [], callable $loader = null) |
||
84 | { |
||
85 | $this->client = $client; |
||
86 | $this->resource = $resource; |
||
87 | $this->filters = $filters; |
||
88 | $this->loader = $loader ?: function ($input) { |
||
89 | return $input; |
||
90 | }; |
||
91 | $this->rewind(); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Return the count elements in this collection, @see Countable::count(). |
||
96 | * |
||
97 | * @return integer |
||
98 | */ |
||
99 | final public function count() |
||
107 | |||
108 | /** |
||
109 | * Rewind the Iterator to the first element, @see Iterator::rewind(). |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | final public function rewind() |
||
121 | |||
122 | /** |
||
123 | * Return the key of the current element, @see Iterator::key(). |
||
124 | * |
||
125 | * @return integer |
||
126 | */ |
||
127 | final public function key() |
||
136 | |||
137 | /** |
||
138 | * Checks if current position is valid, @see Iterator::valid(). |
||
139 | * |
||
140 | * @return boolean |
||
141 | */ |
||
142 | final public function valid() |
||
150 | |||
151 | /** |
||
152 | * Move forward to next element, @see Iterator::next(). |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | final public function next() |
||
175 | |||
176 | /** |
||
177 | * Return the current element, @see Iterator::current(). |
||
178 | * |
||
179 | * @return mixed Returns the element in the results array or a custom type defined by $loader. |
||
180 | */ |
||
181 | final public function current() |
||
196 | } |
||
197 |