forked from wbobeirne/Phlickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoListIterator.php
207 lines (194 loc) · 5.05 KB
/
PhotoListIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* @version $Id$
* @author Andrew Morton <[email protected]>
* @license http://opensource.org/licenses/lgpl-license.php
* GNU Lesser General Public License, Version 2.1
* @package Phlickr
*/
/**
* Phlickr_Api includes the core classes.
*/
require_once dirname(__FILE__) . '/Api.php';
/**
* This class uses the Phlickr_PhotoList object.
*/
require_once dirname(__FILE__) . '/PhotoList.php';
/**
* Phlickr_PhotoListIterator is used to iterate through the pages of a
* Phlickr_PhotoList. It also can act as a Phlickr_IPhotoList effectively
* combining a paged list into a single photo list.
*
* Sample usage:
* <code>
* <?php
* include_once '/Api.php';
* include_once '/Group.php';
* include_once '/PhotoListIterator.php';
*
* $api = new Phlickr_Api(FLICKR_API_KEY, FLICKR_API_SECRET, FLICKR_TOKEN);
*
* $group = new Phlickr_Group($api, '98274710@N00');
* $photolist = $group->getPhotoList();
*
* // iterate over all the pages
* $iterator = new Phlickr_PhotoListIterator($photolist);
* foreach($iterator as $page => $photos) {
* print "Page number: $page\n";
* foreach ($photos as $photo) {
* print "Photo Id: {$photo->getId()} Title: '{$photo->getTitle()}'\n";
* }
* }
*
* // or, just extract all the photos. it does the same thing
* foreach ($iterator->getPhotos() as $photo) {
* print "Photo Id: {$photo->getId()} Title: '{$photo->getTitle()}'\n";
* }
*
* ?>
* </code>
*
* @package Phlickr
* @author Andrew Morton <[email protected]>
* @todo Rework the class to use a Phlickr_Request instead of a
* Phlickr_PhotoList.
* @todo Modify the constructor to accept either a Phlickr_Request or
* Phlickr_PhotoList.
* @since 0.1.8
*/
class Phlickr_PhotoListIterator implements Iterator, Phlickr_Framework_IPhotoList {
/**
* Photo list being iterated.
*
* @var object Phlickr_PhotoList
*/
private $_pl;
/**
* Number of photos per page.
*
* This is copied from the photo list because it shouldn't change.
*
* @var integer
*/
private $_perPage;
/**
*
* @var boolean
*/
private $_isCachedAllowed;
/**
* The current photos from the current page.
*
* @var array of Phlickr_Photo objects
*/
private $_photos;
/**
* The current page number.
*
* @var integer
*/
private $_page;
/**
* Constructor.
*
* @param object Phlickr_PhotoList $pl The photo list to iterate.
* @param boolean $isCachedAllowed Is cached data acceptable?
*/
public function __construct(Phlickr_PhotoList $pl, $isCachedAllowed = true) {
$this->_pl = $pl;
$this->_isCachedAllowed = $isCachedAllowed;
$this->_perPage = $pl->getPhotosPerPage();
$this->rewind();
}
/**
* Return an array of photos from the current page.
*
* @return array Phlickr_Photo
*/
public function current() {
if (!isset($this->_photos)) {
$this->_photos = $this->_pl->getPhotosFromPage($this->_page);
}
return $this->_photos;
}
/**
* Get the page number, which serves as the key.
*
* @return void
* @see current()
*/
public function key() {
return (integer) $this->_page;
}
/**
* Move the iterator to the next page.
*
* @return void
* @see rewind(), valid()
*/
public function next() {
$this->_photos = null;
$this->_page++;
}
/**
* Reset the iterator to the first page.
*
* @return void
*/
public function rewind() {
$this->_photos = null;
$this->_page = 1;
}
/**
* Is the current page valid?
*
* @return boolean
*/
public function valid() {
return ($this->_page <= $this->_pl->getPageCount());
}
/**
* Return the underlieing photo list we're iterating.
*
* @return object Phlickr_PhotoList
*/
public function getPhotoList() {
return $this->_pl;
}
/**
* Return the total number of photos on every page in the list.
*
* @return integer
*/
function getCount() {
return $this->_pl->getCount();
}
/**
* Return an array of the photo ids on every page in the list.
*
* @return object Phlickr_PhotoList
*/
public function getIds() {
$count = $this->_pl->getPageCount();
$ret = array();
for ($page = 1; $page <= $count; $page++) {
$ids = $this->_pl->getIdsFromPage($page);
$ret = array_merge($ret, $ids);
}
return $ret;
}
/**
* Return an array of photos from every page in the list.
*
* @return object Phlickr_PhotoList
*/
public function getPhotos() {
$count = $this->_pl->getPageCount();
$ret = array();
for ($page = 1; $page <= $count; $page++) {
$photos = $this->_pl->getPhotosFromPage($page);
$ret = array_merge($ret, $photos);
}
return $ret;
}
}