forked from alchemy-fr/Phlickr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommentList.php
214 lines (196 loc) · 6.04 KB
/
CommentList.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
<?php
/**
* @version $Id: CommentList.php 515 2006-12-28 00:29:20Z drewish $
* @author Martin Legris <[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 ICommentList.
*/
require_once dirname(__FILE__) . '/Framework/ICommentList.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 Martin Legris <[email protected]>
* @see Phlickr_PhotoListIterator
* @since 0.5.1
*/
class Phlickr_CommentList implements Phlickr_Framework_ICommentList {
/**
* The name of the XML element in the response that defines the object.
*
* @var string
*/
const XML_RESPONSE_LIST_ELEMENT = 'comments';
/**
* The name of the XML element in the response that defines a member of the
* list.
*
* @var string
*/
const XML_RESPONSE_ELEMENT = 'comment';
/**
* The name of the Flickr API method that provides the info on this object.
*
* @var string
*/
const XML_METHOD_NAME = 'flickr.photos.comments.getList';
/**
* Request the CommentList is based on.
*
* @var object Phlickr_Request
*/
protected $_request = null;
/**
* XML from Flickr.
*
* @var object SimpleXMLElement
*/
protected $_cachedXml = array();
/**
* 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) {
$this->_request = $request;
$this->load();
}
static protected function getResponseListElement() {
return self::XML_RESPONSE_LIST_ELEMENT;
}
static protected function getResponseElement() {
return self::XML_RESPONSE_ELEMENT;
}
static public function getCommentList(Phlickr_Api $api, $photo_id) {
$request = $api->createRequest(
self::XML_METHOD_NAME,
array("photo_id" => $photo_id)
);
if (is_null($request)) {
throw new Phlickr_Exception('Could not create a Request flickr.photos.comments.getList.');
} else {
return new Phlickr_CommentList($request);
}
}
/**
* 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 CommentList 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) {
$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) {
// allow cached results
$this->_cachedXml = $this->requestXml(true);
}
/**
* Connect to Flickr and get the current, complete information on this
* object.
*
* @return void
* @see load(), requestXml()
*/
public function refresh() {
// force a non-cached update
$this->_cachedXml = $this->requestXml(false);
}
/**
* Return the total number of photos in the photolist.
*
* @return integer
*/
public function getCount() {
if (!isset($this->_cachedXml)) {
$this->load();
}
return (integer) count($this->_cachedXml->comment);
}
/**
* Return an array of the comment ids on this page of the list.
*
* @return array of string ids
*/
public function getIds(){
$ids = array();
$comments = $this->getComments(true);
foreach($comments as $comment) {
$ids[] = $comment->getId();
}
return $ids;
}
/**
* Return an array of photos on a given page.
*
* This function is designed to allow iterators access into the class.
*
* @param boolean $allowCached Should cached data be allowed?
* @return array object Phlickr_Comment objects.
*/
public function getComments($allowCached = true) {
if ($allowCached) {
$this->load();
} else {
$this->refresh();
}
$ret = array();
foreach ($this->_cachedXml->{self::getResponseElement()} as $xmlComment) {
$ret[] = new Phlickr_Comment($this->getApi(), $xmlComment);
}
return $ret;
}
}