forked from alchemy-fr/Phlickr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhotosetPhotoList.php
129 lines (121 loc) · 3.37 KB
/
PhotosetPhotoList.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
<?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 extends Phlickr_ListBase.
*/
require_once dirname(__FILE__) . '/Framework/ListBase.php';
/**
* This class implements IPhotoList.
*/
require_once dirname(__FILE__) . '/Framework/IPhotoList.php';
/**
* Phlickr_PhotosetPhotoList represents all the photos in a photoset.
*
* Unlike Phlickr_PhotoList, the photoset list is not segmented into pages.
*
* @todo Add sample code.
* @package Phlickr
* @since 0.1.1
*/
class Phlickr_PhotosetPhotoList extends Phlickr_Framework_ListBase
implements Phlickr_Framework_IPhotoList
{
/**
* The name of the XML element in the response that defines the object.
*
* @var string
*/
const XML_RESPONSE_LIST_ELEMENT = 'photoset';
/**
* The name of the XML element in the response that defines a member of the
* list.
*
* @var string
*/
const XML_RESPONSE_ELEMENT = 'photo';
/**
* The name of the Flickr API method that provides the info on this object.
*
* @var string
*/
const XML_METHOD_NAME = 'flickr.photosets.getPhotos';
/**
* Constructor.
*
* @param object Phlickr_Request $request
*/
function __construct(Phlickr_Request $request) {
parent::__construct($request, self::XML_RESPONSE_ELEMENT,
self::XML_RESPONSE_LIST_ELEMENT);
}
/**
* Returns the name of this object's getInfo API method.
*
* @return string
*/
static function getRequestMethodName() {
return self::XML_METHOD_NAME;
}
/**
* Returns an array of parameters to be used when creating a
* Phlickr_Request to call this object's getInfo API method.
*
* @param string $id The id of this photoset.
* @return array
*/
static function getRequestMethodParams($id) {
return array('photoset_id' => (string) $id);
}
/**
* Return the number of photos in the photoset.
*
* The count on a Photoset should always be 1 or more.
*
* @return integer
*/
public function getCount() {
if (!isset($this->_cachedXml->photo)) {
$this->load();
}
$ret = 0;
foreach ($this->_cachedXml->photo as $xml) {
$ret++;
}
return $ret;
}
public function getPhotos() {
if (!isset($this->_cachedXml->{$this->getResponseElement()})) {
$this->load();
}
$ret = array();
foreach ($this->_cachedXml->{$this->getResponseElement()} as $xml) {
$photo = new Phlickr_Photo($this->getApi(), $xml);
$ret[] = $photo;
}
return $ret;
}
/**
* Return a random photo from the list.
*
* @return object Phlickr_Photo
* @since 0.1.6
*/
function getRandomPhoto() {
if (!isset($this->_cachedXml->{$this->getResponseElement()})) {
$this->load();
}
$photos = $this->_cachedXml->{$this->getResponseElement()};
$photoXml = $photos[rand(0, $this->getCount() - 1)];
return new Phlickr_Photo($this->getApi(), $photoXml);
}
}