forked from alchemy-fr/Phlickr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhotoList.php
331 lines (305 loc) · 9.11 KB
/
PhotoList.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?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 implements IPhotoList.
*/
require_once dirname(__FILE__) . '/Framework/IPhotoList.php';
/**
* Phlickr_PhotoList represents paged list of photos.
*
* <b>WATCH OUT</b>: there's still some problems with the caching in the class.
* if you call refresh() it'll force and update only to the current page. If
* you want the whole thing refreshed you'll need to call it on each page.
*
* @package Phlickr
* @author Andrew Morton <[email protected]>
* @see Phlickr_PhotoListIterator
* @since 0.1.0
*/
class Phlickr_PhotoList implements Phlickr_Framework_IPhotoList {
/**
* The name of the XML element in the response that defines the object.
*
* @var string
*/
const XML_RESPONSE_LIST_ELEMENT = 'photos';
/**
* The name of the XML element in the response that defines a member of the
* list.
*
* @var string
*/
const XML_RESPONSE_ELEMENT = 'photo';
/**
* Default number of photos per page.
*
* @var integer
*/
const PER_PAGE_DEFAULT = 100;
/**
* Maximum number of photos per page.
*
* @var integer
*/
const PER_PAGE_MAX = 500;
/**
* Request the PhotoList is based on.
*
* @var object Phlickr_Request
*/
protected $_request = null;
/**
* XML from Flickr.
*
* @var object SimpleXMLElement
*/
protected $_cachedXml = array();
/**
* Current page in the PhotoList.
*
* @var integer
*/
private $_page = 1;
/**
* Number of photos on a page.
*
* @var integer
*/
private $_perPage;
/**
* Constructor.
*
* @param object Phlickr_Request $request
* @param integer $photosPerPage Number of photos on each page.
* @throws Phlickr_Exception, Phlickr_ConnectionException,
* Phlickr_XmlParseException
*/
function __construct(Phlickr_Request $request,
$photosPerPage = self::PER_PAGE_DEFAULT)
{
$this->_request = $request;
// API limits the number of photos per page
$this->_perPage = ($photosPerPage > self::PER_PAGE_MAX)
? self::PER_PAGE_MAX : (integer) $photosPerPage;
$this->load();
}
static protected function getResponseListElement() {
return self::XML_RESPONSE_LIST_ELEMENT;
}
static protected function getResponseElement() {
return self::XML_RESPONSE_ELEMENT;
}
/**
* Return a reference to this object's Phlickr_Api.
*
* @return object Plickr_Api
*/
public function &getApi() {
return $this->_request->getApi();
}
/**
* Return the Phlickr_Request the PhotoList is based on.
*
* @return object Phlickr_Request
*/
public function getRequest() {
return $this->_request;
}
/**
* Connect to Flickr and retreive a page of photos.
*
* @param boolean $allowCached If a cached result exists, should it be
* returned?
* @param integer $page The page number to request.
* @return object SimpleXMLElement
* @throws Phlickr_ConnectionException, Phlickr_XmlParseException
* @see load(), refresh()
*/
protected function requestXml($allowCached = false, $page) {
$params =& $this->_request->getParams();
$params['page'] = $page;
$params['per_page'] = $this->getPhotosPerPage();
$response = $this->_request->execute($allowCached);
$xml = $response->xml->{self::getResponseListElement()};
if (is_null($xml)) {
throw new Exception(
sprintf(
"Could not load object with request: '%s'.",
$request->getMethod()
)
);
}
return $xml;
}
/**
* Load the complete information on object.
*
* @param integer $page The page number to load. Defaults to the current
* page.
* @return void
* @see refresh(), requestXml()
*/
public function load($page = null) {
// if page is null use the current page.
$page = (is_null($page)) ? $this->getPage() : (integer) $page ;
// allow cached results
$this->_cachedXml[$page] = $this->requestXml(true, $page);
}
/**
* Connect to Flickr and get the current, complete information on this
* object.
*
* @param integer $page The page number to load. Defaults to the current
* page.
* @return void
* @see load(), requestXml()
*/
public function refresh($page = null) {
// if page is null use the current page.
$page = (is_null($page)) ? $this->getPage() : (integer) $page ;
// force a non-cached update
$this->_cachedXml[$page] = $this->requestXml(false, $page);
}
/**
* Return the number of photos on a page.
*
* @return integer
* @since 0.1.5
*/
function getPhotosPerPage() {
return $this->_perPage;
}
/**
* Return the total number of pages in the list.
*
* @return integer
*/
function getPageCount() {
if (!isset($this->_cachedXml[$this->_page]['pages'])) {
$this->load();
}
return (integer) $this->_cachedXml[$this->_page]['pages'];
}
/**
* Return the current page.
*
* @return integer
*/
public function getPage() {
return (integer) $this->_page;
}
/**
* Set the current page number.
*
* @param integer $page The page in the photolist to view.
* @return void
*/
public function setPage($page) {
$this->_page = (integer) $page;
}
/**
* Return the total number of photos in the photolist.
*
* @return integer
*/
public function getCount() {
if (!isset($this->_cachedXml[$this->_page])) {
$this->load();
}
return (integer) $this->_cachedXml[$this->_page]['total'];
}
/**
* Return an array of the photo ids on a given page.
*
* This function is designed to allow iterators access into the class.
*
* @param integer $page is the page number 1 to getPageCount()
* @param boolean $allowCached Should cached data be allowed?
* @return array of string ids
* @since 0.2.3
*/
public function getIdsFromPage($page, $allowCached = true) {
if ($allowCached) {
$this->load($page);
} else {
$this->refresh($page);
}
$ret = array();
foreach ($this->_cachedXml[$page]->{self::getResponseElement()} as $xmlPhoto) {
$ret[] = (string) $xmlPhoto['id'];
}
return $ret;
}
/**
* Return an array of photos on a given page.
*
* This function is designed to allow iterators access into the class.
*
* @param integer $page is the page number 1 to getPageCount()
* @param boolean $allowCached Should cached data be allowed?
* @return array object Phlickr_AuthedPhoto or Phlickr_Photo depending
* on the owner.
*/
public function getPhotosFromPage($page, $allowCached = true) {
if ($allowCached) {
$this->load($page);
} else {
$this->refresh($page);
}
$ret = array();
foreach ($this->_cachedXml[$page]->{self::getResponseElement()} as $xmlPhoto) {
if ($xmlPhoto['owner'] == $this->getApi()->getUserId()) {
$ret[] = new Phlickr_AuthedPhoto($this->getApi(), $xmlPhoto);
} else {
$ret[] = new Phlickr_Photo($this->getApi(), $xmlPhoto);
}
}
return $ret;
}
/**
* Return an array of the photo ids on this page of the list.
*
* @return array of string ids
*/
public function getIds() {
if (!isset($this->_cachedXml[$this->_page]->{$this->getResponseElement()})) {
$this->load();
}
$ret = $this->getIdsFromPage($this->_page);
return $ret;
}
/**
* Return an array of the Phlickr_Photo objects on this page of the list.
*
* @return array object Phlickr_AuthedPhoto or Phlickr_Photo depending on
* the owners.
* @see getIds()
*/
public function getPhotos() {
return $this->getPhotosFromPage($this->_page, true);
}
/**
* Return a random photo from the photo list.
*
* The the photo can be from any one of the pages in the list.
*
* @return object Phlickr_AuthedPhoto or Phlickr_Photo depending on the
* owner.
* @since 0.1.7
*/
function getRandomPhoto() {
// this might be in-efficient if the number of photos per page is large
$photos = $this->getPhotosFromPage(rand(1, $this->getPageCount()), true);
return $photos[rand(0, count($photos) - 1)];
}
}