1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 Dirk Groenen |
4
|
|
|
* |
5
|
|
|
* (c) Dirk Groenen <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DirkGroenen\Pinterest\Models; |
12
|
|
|
|
13
|
|
|
use DirkGroenen\Pinterest\Exceptions\PinterestException; |
14
|
|
|
|
15
|
|
|
use \DirkGroenen\Pinterest\Pinterest; |
16
|
|
|
use \DirkGroenen\Pinterest\Transport\Response; |
17
|
|
|
|
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() |
103
|
|
|
{ |
104
|
1 |
|
return $this->items; |
105
|
|
|
} |
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) |
115
|
|
|
{ |
116
|
19 |
|
$modelcollection = []; |
117
|
|
|
|
118
|
19 |
|
foreach ($items as $item) { |
119
|
19 |
|
$class = new \ReflectionClass("\\DirkGroenen\\Pinterest\\Models\\" . $this->model); |
120
|
19 |
|
$modelcollection[] = $class->newInstanceArgs([$this->master, $item]); |
121
|
|
|
} |
122
|
|
|
|
123
|
19 |
|
return $modelcollection; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if their is a next page available |
128
|
|
|
* |
129
|
|
|
* @access public |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
1 |
|
public function hasNextPage() |
133
|
|
|
{ |
134
|
1 |
|
return ($this->response != null && isset($this->response->page['next'])); |
135
|
|
|
} |
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) |
145
|
|
|
{ |
146
|
15 |
|
return $this->items[$index]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Convert the collection to an array |
151
|
|
|
* |
152
|
|
|
* @access public |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
2 |
|
public function toArray() |
156
|
|
|
{ |
157
|
2 |
|
$items = []; |
158
|
|
|
|
159
|
2 |
|
foreach ($this->items as $item) { |
160
|
2 |
|
$items[] = $item->toArray(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return array( |
164
|
2 |
|
"data" => $items, |
165
|
2 |
|
"page" => $this->pagination |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Convert the collection to JSON |
171
|
|
|
* |
172
|
|
|
* @access public |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
1 |
|
public function toJson() |
176
|
|
|
{ |
177
|
1 |
|
return json_encode($this->toArray(), true); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Convert the object into something JSON serializable. |
182
|
|
|
* |
183
|
|
|
* @access public |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function jsonSerialize() |
187
|
|
|
{ |
188
|
|
|
return $this->toArray(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Convert the collection to its string representation |
193
|
|
|
* |
194
|
|
|
* @access public |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function __toString() |
198
|
|
|
{ |
199
|
|
|
return $this->toJson(); |
200
|
|
|
} |
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) |
210
|
|
|
{ |
211
|
|
|
return isset($this->items[$offset]); |
212
|
|
|
} |
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) |
221
|
|
|
{ |
222
|
|
|
return $this->items[$offset]; |
223
|
|
|
} |
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) |
233
|
|
|
{ |
234
|
|
|
$this->items[$offset] = $value; |
235
|
|
|
} |
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) |
244
|
|
|
{ |
245
|
|
|
unset($this->items[$offset]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Make the collection items iteratable |
250
|
|
|
* |
251
|
|
|
* @access public |
252
|
|
|
* @return ArrayIterator |
253
|
|
|
*/ |
254
|
|
|
public function getIterator() { |
255
|
|
|
return new \ArrayIterator($this->items); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} |