This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
/
AbstractResultSet.php
278 lines (247 loc) · 6.77 KB
/
AbstractResultSet.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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Service
*/
namespace ZendService\Technorati;
use DomDocument;
use DOMXPath;
use OutOfBoundsException;
use SeekableIterator;
use ZendXml\Security as XmlSecurity;
/**
* This is the most essential result set.
* The scope of this class is to be extended by a query-specific child result set class,
* and it should never be used to initialize a standalone object.
*
* Each of the specific result sets represents a collection of query-specific
* Result objects.
*
* @category Zend
* @package Zend_Service
* @subpackage Technorati
* @abstract
*/
abstract class AbstractResultSet implements SeekableIterator
{
/**
* The total number of results available
*
* @var int
* @access protected
*/
protected $totalResultsAvailable;
/**
* The number of results in this result set
*
* @var int
* @access protected
*/
protected $totalResultsReturned;
/**
* The offset in the total result set of this search set
*
* @var int
*/
//TODO public $firstResultPosition;
/**
* A DomNodeList of results
*
* @var DomNodeList
* @access protected
*/
protected $results;
/**
* Technorati API response document
*
* @var DomDocument
* @access protected
*/
protected $dom;
/**
* Object for $this->dom
*
* @var DOMXpath
* @access protected
*/
protected $xpath;
/**
* XML string representation for $this->dom
*
* @var string
* @access protected
*/
protected $xml;
/**
* Current Item
*
* @var int
* @access protected
*/
protected $currentIndex = 0;
/**
* Parses the search response and retrieves the results for iteration.
*
* @param DomDocument $dom the ReST fragment for this object
* @param array $options query options as associative array
*/
public function __construct(DomDocument $dom, $options = array())
{
$this->init($dom, $options);
// Technorati loves to make developer's life really hard
// I must read query options in order to normalize a single way
// to display start and limit.
// The value is printed out in XML using many different tag names,
// too hard to get it from XML
// Additionally, the following tags should be always available
// according to API documentation but... this is not the truth!
// - querytime
// - limit
// - start (sometimes rankingstart)
// query tag is only available for some requests, the same for url.
// For now ignore them.
//$start = isset($options['start']) ? $options['start'] : 1;
//$limit = isset($options['limit']) ? $options['limit'] : 20;
//$this->firstResultPosition = $start;
}
/**
* Initializes this object from a DomDocument response.
*
* Because __construct and __wakeup shares some common executions,
* it's useful to group them in a single initialization method.
* This method is called once each time a new instance is created
* or a serialized object is unserialized.
*
* @param DomDocument $dom the ReST fragment for this object
* @param array $options query options as associative array
* * @return void
*/
protected function init(DomDocument $dom, $options = array())
{
$this->dom = $dom;
$this->xpath = new DOMXPath($dom);
$this->results = $this->xpath->query("//item");
}
/**
* Number of results returned.
*
* @return int total number of results returned
*/
public function totalResults()
{
return (int) $this->totalResultsReturned;
}
/**
* Number of available results.
*
* @return int total number of available results
*/
public function totalResultsAvailable()
{
return (int) $this->totalResultsAvailable;
}
/**
* Implements SeekableIterator::current().
*
* @return void
* @throws ZendService\Exception
* @abstract
*/
// abstract public function current();
/**
* Implements SeekableIterator::key().
*
* @return int
*/
public function key()
{
return $this->currentIndex;
}
/**
* Implements SeekableIterator::next().
*
* @return void
*/
public function next()
{
$this->currentIndex += 1;
}
/**
* Implements SeekableIterator::rewind().
*
* @return bool
*/
public function rewind()
{
$this->currentIndex = 0;
return true;
}
/**
* Implement SeekableIterator::seek().
*
* @param int $index
* @return void
* @throws OutOfBoundsException
*/
public function seek($index)
{
$indexInt = (int) $index;
if ($indexInt >= 0 && $indexInt < $this->results->length) {
$this->currentIndex = $indexInt;
} else {
throw new OutOfBoundsException("Illegal index '$index'");
}
}
/**
* Implement SeekableIterator::valid().
*
* @return boolean
*/
public function valid()
{
return null !== $this->results && $this->currentIndex < $this->results->length;
}
/**
* Returns the response document as XML string.
*
* @return string the response document converted into XML format
*/
public function getXml()
{
return $this->dom->saveXML();
}
/**
* Overwrites standard __sleep method to make this object serializable.
*
* DomDocument and DOMXpath objects cannot be serialized.
* This method converts them back to an XML string.
*
* @return void
*/
public function __sleep()
{
$this->xml = $this->getXml();
$vars = array_keys(get_object_vars($this));
return array_diff($vars, array('_dom', '_xpath'));
}
/**
* Overwrites standard __wakeup method to make this object unserializable.
*
* Restores object status before serialization.
* Converts XML string into a DomDocument object and creates a valid
* DOMXpath instance for given DocDocument.
*
* @return void
*/
public function __wakeup()
{
$dom = new DOMDocument();
$dom = XmlSecurity::scan($this->xml, $dom);
$this->init($dom);
$this->xml = null; // reset XML content
}
}