PageRenderTime 63ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/Technorati/ResultSet.php

http://github.com/michael-romer/zf-boilerplate
PHP | 289 lines | 71 code | 32 blank | 186 comment | 4 complexity | b23e60f7476d15b587ed856f02de62e1 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Technorati
  18. * @copyright Copyright (c) 2005-2011 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 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Service_Technorati_Result
  24. */
  25. require_once 'Zend/Service/Technorati/Result.php';
  26. /**
  27. * This is the most essential result set.
  28. * The scope of this class is to be extended by a query-specific child result set class,
  29. * and it should never be used to initialize a standalone object.
  30. *
  31. * Each of the specific result sets represents a collection of query-specific
  32. * Zend_Service_Technorati_Result objects.
  33. *
  34. * @category Zend
  35. * @package Zend_Service
  36. * @subpackage Technorati
  37. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @abstract
  40. */
  41. abstract class Zend_Service_Technorati_ResultSet implements SeekableIterator
  42. {
  43. /**
  44. * The total number of results available
  45. *
  46. * @var int
  47. * @access protected
  48. */
  49. protected $_totalResultsAvailable;
  50. /**
  51. * The number of results in this result set
  52. *
  53. * @var int
  54. * @access protected
  55. */
  56. protected $_totalResultsReturned;
  57. /**
  58. * The offset in the total result set of this search set
  59. *
  60. * @var int
  61. */
  62. //TODO public $firstResultPosition;
  63. /**
  64. * A DomNodeList of results
  65. *
  66. * @var DomNodeList
  67. * @access protected
  68. */
  69. protected $_results;
  70. /**
  71. * Technorati API response document
  72. *
  73. * @var DomDocument
  74. * @access protected
  75. */
  76. protected $_dom;
  77. /**
  78. * Object for $this->_dom
  79. *
  80. * @var DOMXpath
  81. * @access protected
  82. */
  83. protected $_xpath;
  84. /**
  85. * XML string representation for $this->_dom
  86. *
  87. * @var string
  88. * @access protected
  89. */
  90. protected $_xml;
  91. /**
  92. * Current Item
  93. *
  94. * @var int
  95. * @access protected
  96. */
  97. protected $_currentIndex = 0;
  98. /**
  99. * Parses the search response and retrieves the results for iteration.
  100. *
  101. * @param DomDocument $dom the ReST fragment for this object
  102. * @param array $options query options as associative array
  103. */
  104. public function __construct(DomDocument $dom, $options = array())
  105. {
  106. $this->_init($dom, $options);
  107. // Technorati loves to make developer's life really hard
  108. // I must read query options in order to normalize a single way
  109. // to display start and limit.
  110. // The value is printed out in XML using many different tag names,
  111. // too hard to get it from XML
  112. // Additionally, the following tags should be always available
  113. // according to API documentation but... this is not the truth!
  114. // - querytime
  115. // - limit
  116. // - start (sometimes rankingstart)
  117. // query tag is only available for some requests, the same for url.
  118. // For now ignore them.
  119. //$start = isset($options['start']) ? $options['start'] : 1;
  120. //$limit = isset($options['limit']) ? $options['limit'] : 20;
  121. //$this->_firstResultPosition = $start;
  122. }
  123. /**
  124. * Initializes this object from a DomDocument response.
  125. *
  126. * Because __construct and __wakeup shares some common executions,
  127. * it's useful to group them in a single initialization method.
  128. * This method is called once each time a new instance is created
  129. * or a serialized object is unserialized.
  130. *
  131. * @param DomDocument $dom the ReST fragment for this object
  132. * @param array $options query options as associative array
  133. * * @return void
  134. */
  135. protected function _init(DomDocument $dom, $options = array())
  136. {
  137. $this->_dom = $dom;
  138. $this->_xpath = new DOMXPath($dom);
  139. $this->_results = $this->_xpath->query("//item");
  140. }
  141. /**
  142. * Number of results returned.
  143. *
  144. * @return int total number of results returned
  145. */
  146. public function totalResults()
  147. {
  148. return (int) $this->_totalResultsReturned;
  149. }
  150. /**
  151. * Number of available results.
  152. *
  153. * @return int total number of available results
  154. */
  155. public function totalResultsAvailable()
  156. {
  157. return (int) $this->_totalResultsAvailable;
  158. }
  159. /**
  160. * Implements SeekableIterator::current().
  161. *
  162. * @return void
  163. * @throws Zend_Service_Exception
  164. * @abstract
  165. */
  166. // abstract public function current();
  167. /**
  168. * Implements SeekableIterator::key().
  169. *
  170. * @return int
  171. */
  172. public function key()
  173. {
  174. return $this->_currentIndex;
  175. }
  176. /**
  177. * Implements SeekableIterator::next().
  178. *
  179. * @return void
  180. */
  181. public function next()
  182. {
  183. $this->_currentIndex += 1;
  184. }
  185. /**
  186. * Implements SeekableIterator::rewind().
  187. *
  188. * @return bool
  189. */
  190. public function rewind()
  191. {
  192. $this->_currentIndex = 0;
  193. return true;
  194. }
  195. /**
  196. * Implement SeekableIterator::seek().
  197. *
  198. * @param int $index
  199. * @return void
  200. * @throws OutOfBoundsException
  201. */
  202. public function seek($index)
  203. {
  204. $indexInt = (int) $index;
  205. if ($indexInt >= 0 && $indexInt < $this->_results->length) {
  206. $this->_currentIndex = $indexInt;
  207. } else {
  208. throw new OutOfBoundsException("Illegal index '$index'");
  209. }
  210. }
  211. /**
  212. * Implement SeekableIterator::valid().
  213. *
  214. * @return boolean
  215. */
  216. public function valid()
  217. {
  218. return null !== $this->_results && $this->_currentIndex < $this->_results->length;
  219. }
  220. /**
  221. * Returns the response document as XML string.
  222. *
  223. * @return string the response document converted into XML format
  224. */
  225. public function getXml()
  226. {
  227. return $this->_dom->saveXML();
  228. }
  229. /**
  230. * Overwrites standard __sleep method to make this object serializable.
  231. *
  232. * DomDocument and DOMXpath objects cannot be serialized.
  233. * This method converts them back to an XML string.
  234. *
  235. * @return void
  236. */
  237. public function __sleep() {
  238. $this->_xml = $this->getXml();
  239. $vars = array_keys(get_object_vars($this));
  240. return array_diff($vars, array('_dom', '_xpath'));
  241. }
  242. /**
  243. * Overwrites standard __wakeup method to make this object unserializable.
  244. *
  245. * Restores object status before serialization.
  246. * Converts XML string into a DomDocument object and creates a valid
  247. * DOMXpath instance for given DocDocument.
  248. *
  249. * @return void
  250. */
  251. public function __wakeup() {
  252. $dom = new DOMDocument();
  253. $dom->loadXml($this->_xml);
  254. $this->_init($dom);
  255. $this->_xml = null; // reset XML content
  256. }
  257. }