1 | <?php |
||
18 | class Collection implements \JsonSerializable, \ArrayAccess, \IteratorAggregate{ |
||
19 | |||
20 | /** |
||
21 | * The items in the collection |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $items = []; |
||
26 | |||
27 | /** |
||
28 | * The model of each collection item |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $model; |
||
33 | |||
34 | /** |
||
35 | * Stores the pagination object |
||
36 | * |
||
37 | * @var array|boolean |
||
38 | */ |
||
39 | public $pagination; |
||
40 | |||
41 | /** |
||
42 | * Instance of Pinterest master class |
||
43 | * |
||
44 | * @var Pinterest |
||
45 | */ |
||
46 | private $master; |
||
47 | |||
48 | /** |
||
49 | * Response instance |
||
50 | * |
||
51 | * @var Response |
||
52 | */ |
||
53 | private $response; |
||
54 | |||
55 | /** |
||
56 | * Construct |
||
57 | * |
||
58 | * @access public |
||
59 | * @param Pinterest $master |
||
60 | * @param array|Response $items |
||
61 | * @param string $model |
||
62 | * @throws InvalidModelException |
||
63 | */ |
||
64 | 19 | public function __construct(Pinterest $master, $items, $model) { |
|
65 | 19 | $this->master = $master; |
|
66 | |||
67 | // Create class path |
||
68 | 19 | $this->model = ucfirst(strtolower($model)); |
|
69 | |||
70 | 19 | if (!class_exists("\\DirkGroenen\\Pinterest\\Models\\" . $this->model)) { |
|
71 | throw new InvalidModelException; |
||
72 | } |
||
73 | |||
74 | // Get items and response instance |
||
75 | 19 | if (is_array($items)) { |
|
76 | 1 | $this->response = null; |
|
77 | 1 | $this->items = $items; |
|
78 | 18 | } else if ($items instanceof \DirkGroenen\Pinterest\Transport\Response) { |
|
79 | 18 | $this->response = $items; |
|
80 | 18 | $this->items = $items->data; |
|
81 | } else { |
||
82 | throw new PinterestException("$items needs to be an instance of Transport\Response or an array."); |
||
83 | } |
||
84 | |||
85 | // Transform the raw collection data to models |
||
86 | 19 | $this->items = $this->buildCollectionModels($this->items); |
|
87 | |||
88 | // Add pagination object |
||
89 | 19 | if (isset($this->response->page) && !empty($this->response->page['next'])) { |
|
90 | 9 | $this->pagination = $this->response->page; |
|
91 | } else { |
||
92 | 10 | $this->pagination = false; |
|
93 | } |
||
94 | 19 | } |
|
95 | |||
96 | /** |
||
97 | * Get all items from the collection |
||
98 | * |
||
99 | * @access public |
||
100 | * @return array |
||
101 | */ |
||
102 | 1 | public function all() |
|
106 | |||
107 | /** |
||
108 | * Transform each raw item into a model |
||
109 | * |
||
110 | * @access private |
||
111 | * @param array $items |
||
112 | * @return array |
||
113 | */ |
||
114 | 19 | private function buildCollectionModels(array $items) |
|
125 | |||
126 | /** |
||
127 | * Check if their is a next page available |
||
128 | * |
||
129 | * @access public |
||
130 | * @return boolean |
||
131 | */ |
||
132 | 1 | public function hasNextPage() |
|
136 | |||
137 | /** |
||
138 | * Return the item at the given index |
||
139 | * |
||
140 | * @access public |
||
141 | * @param int $index |
||
142 | * @return Model |
||
143 | */ |
||
144 | 15 | public function get($index) |
|
148 | |||
149 | /** |
||
150 | * Convert the collection to an array |
||
151 | * |
||
152 | * @access public |
||
153 | * @return array |
||
154 | */ |
||
155 | 2 | public function toArray() |
|
168 | |||
169 | /** |
||
170 | * Convert the collection to JSON |
||
171 | * |
||
172 | * @access public |
||
173 | * @return string |
||
174 | */ |
||
175 | 1 | public function toJson() |
|
179 | |||
180 | /** |
||
181 | * Convert the object into something JSON serializable. |
||
182 | * |
||
183 | * @access public |
||
184 | * @return array |
||
185 | */ |
||
186 | public function jsonSerialize() |
||
190 | |||
191 | /** |
||
192 | * Convert the collection to its string representation |
||
193 | * |
||
194 | * @access public |
||
195 | * @return string |
||
196 | */ |
||
197 | public function __toString() |
||
201 | |||
202 | /** |
||
203 | * Determine if the given item exists. |
||
204 | * |
||
205 | * @access public |
||
206 | * @param mixed $offset |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function offsetExists($offset) |
||
213 | /** |
||
214 | * Get the value for a given offset. |
||
215 | * |
||
216 | * @access public |
||
217 | * @param mixed $offset |
||
218 | * @return mixed |
||
219 | */ |
||
220 | public function offsetGet($offset) |
||
224 | /** |
||
225 | * Set the value for a given offset. |
||
226 | * |
||
227 | * @access public |
||
228 | * @param mixed $offset |
||
229 | * @param mixed $value |
||
230 | * @return void |
||
231 | */ |
||
232 | public function offsetSet($offset, $value) |
||
236 | /** |
||
237 | * Unset the value for a given offset. |
||
238 | * |
||
239 | * @access public |
||
240 | * @param mixed $offset |
||
241 | * @return void |
||
242 | */ |
||
243 | public function offsetUnset($offset) |
||
247 | |||
248 | /** |
||
249 | * Make the collection items iteratable |
||
250 | * |
||
251 | * @access public |
||
252 | * @return ArrayIterator |
||
253 | */ |
||
254 | public function getIterator() { |
||
257 | |||
258 | } |