7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Service
17
* @subpackage Technorati
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: ResultSet.php 8064 2008-02-16 10:58:39Z thomas $
25
* @see Zend_Service_Technorati_Result
27
require_once 'Zend/Service/Technorati/Result.php';
31
* This is the most essential result set.
32
* The scope of this class is to be extended by a query-specific child result set class,
33
* and it should never be used to initialize a standalone object.
35
* Each of the specific result sets represents a collection of query-specific
36
* Zend_Service_Technorati_Result objects.
39
* @package Zend_Service
40
* @subpackage Technorati
41
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
42
* @license http://framework.zend.com/license/new-bsd New BSD License
45
abstract class Zend_Service_Technorati_ResultSet implements SeekableIterator
48
* The total number of results available
53
protected $_totalResultsAvailable;
56
* The number of results in this result set
61
protected $_totalResultsReturned;
64
* The offset in the total result set of this search set
69
// public $firstResultPosition;
73
* A DomNodeList of results
81
* Technorati API response document
89
* Object for $this->_dom
97
* XML string representation for $this->_dom
110
protected $_currentIndex = 0;
114
* Parses the search response and retrieves the results for iteration.
116
* @param DomDocument $dom the ReST fragment for this object
117
* @param array $options query options as associative array
119
public function __construct(DomDocument $dom, $options = array())
121
$this->_init($dom, $options);
123
// Technorati loves to make developer's life really hard
124
// I must read query options in order to normalize a single way
125
// to display start and limit.
126
// The value is printed out in XML using many different tag names,
127
// too hard to get it from XML
129
// Additionally, the following tags should be always available
130
// according to API documentation but... this is not the truth!
133
// - start (sometimes rankingstart)
135
// query tag is only available for some requests, the same for url.
136
// For now ignore them.
138
//$start = isset($options['start']) ? $options['start'] : 1;
139
//$limit = isset($options['limit']) ? $options['limit'] : 20;
140
//$this->_firstResultPosition = $start;
144
* Initializes this object from a DomDocument response.
146
* Because __construct and __wakeup shares some common executions,
147
* it's useful to group them in a single initialization method.
148
* This method is called once each time a new instance is created
149
* or a serialized object is unserialized.
151
* @param DomDocument $dom the ReST fragment for this object
152
* @param array $options query options as associative array
155
protected function _init(DomDocument $dom, $options = array())
158
$this->_xpath = new DOMXPath($dom);
160
$this->_results = $this->_xpath->query("//item");
164
* Number of results returned.
166
* @return int total number of results returned
168
public function totalResults()
170
return (int) $this->_totalResultsReturned;
175
* Number of available results.
177
* @return int total number of available results
179
public function totalResultsAvailable()
181
return (int) $this->_totalResultsAvailable;
185
* Implements SeekableIterator::current().
188
* @throws Zend_Service_Exception
191
// abstract public function current();
194
* Implements SeekableIterator::key().
198
public function key()
200
return $this->_currentIndex;
204
* Implements SeekableIterator::next().
208
public function next()
210
$this->_currentIndex += 1;
214
* Implements SeekableIterator::rewind().
218
public function rewind()
220
$this->_currentIndex = 0;
225
* Implement SeekableIterator::seek().
229
* @throws OutOfBoundsException
231
public function seek($index)
233
$indexInt = (int) $index;
234
if ($indexInt >= 0 && $indexInt < $this->_results->length) {
235
$this->_currentIndex = $indexInt;
237
throw new OutOfBoundsException("Illegal index '$index'");
242
* Implement SeekableIterator::valid().
246
public function valid()
248
return null !== $this->_results && $this->_currentIndex < $this->_results->length;
252
* Returns the response document as XML string.
254
* @return string the response document converted into XML format
256
public function getXml()
258
return $this->_dom->saveXML();
262
* Overwrites standard __sleep method to make this object serializable.
264
* DomDocument and DOMXpath objects cannot be serialized.
265
* This method converts them back to an XML string.
269
public function __sleep() {
270
$this->_xml = $this->getXml();
271
$vars = array_keys(get_object_vars($this));
272
return array_diff($vars, array('_dom', '_xpath'));
276
* Overwrites standard __wakeup method to make this object unserializable.
278
* Restores object status before serialization.
279
* Converts XML string into a DomDocument object and creates a valid
280
* DOMXpath instance for given DocDocument.
284
public function __wakeup() {
285
$dom = new DOMDocument();
286
$dom->loadXml($this->_xml);
288
$this->_xml = null; // reset XML content