PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/rsilveira1987/Expresso
PHP | 290 lines | 71 code | 32 blank | 187 comment | 4 complexity | 41d677d04bc52dae1f68ac977aa0b8bf MD5 | raw file
  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-2009 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2009 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. * @todo
  62. */
  63. // public $firstResultPosition;
  64. /**
  65. * A DomNodeList of results
  66. *
  67. * @var DomNodeList
  68. * @access protected
  69. */
  70. protected $_results;
  71. /**
  72. * Technorati API response document
  73. *
  74. * @var DomDocument
  75. * @access protected
  76. */
  77. protected $_dom;
  78. /**
  79. * Object for $this->_dom
  80. *
  81. * @var DOMXpath
  82. * @access protected
  83. */
  84. protected $_xpath;
  85. /**
  86. * XML string representation for $this->_dom
  87. *
  88. * @var string
  89. * @access protected
  90. */
  91. protected $_xml;
  92. /**
  93. * Current Item
  94. *
  95. * @var int
  96. * @access protected
  97. */
  98. protected $_currentIndex = 0;
  99. /**
  100. * Parses the search response and retrieves the results for iteration.
  101. *
  102. * @param DomDocument $dom the ReST fragment for this object
  103. * @param array $options query options as associative array
  104. */
  105. public function __construct(DomDocument $dom, $options = array())
  106. {
  107. $this->_init($dom, $options);
  108. // Technorati loves to make developer's life really hard
  109. // I must read query options in order to normalize a single way
  110. // to display start and limit.
  111. // The value is printed out in XML using many different tag names,
  112. // too hard to get it from XML
  113. // Additionally, the following tags should be always available
  114. // according to API documentation but... this is not the truth!
  115. // - querytime
  116. // - limit
  117. // - start (sometimes rankingstart)
  118. // query tag is only available for some requests, the same for url.
  119. // For now ignore them.
  120. //$start = isset($options['start']) ? $options['start'] : 1;
  121. //$limit = isset($options['limit']) ? $options['limit'] : 20;
  122. //$this->_firstResultPosition = $start;
  123. }
  124. /**
  125. * Initializes this object from a DomDocument response.
  126. *
  127. * Because __construct and __wakeup shares some common executions,
  128. * it's useful to group them in a single initialization method.
  129. * This method is called once each time a new instance is created
  130. * or a serialized object is unserialized.
  131. *
  132. * @param DomDocument $dom the ReST fragment for this object
  133. * @param array $options query options as associative array
  134. * * @return void
  135. */
  136. protected function _init(DomDocument $dom, $options = array())
  137. {
  138. $this->_dom = $dom;
  139. $this->_xpath = new DOMXPath($dom);
  140. $this->_results = $this->_xpath->query("//item");
  141. }
  142. /**
  143. * Number of results returned.
  144. *
  145. * @return int total number of results returned
  146. */
  147. public function totalResults()
  148. {
  149. return (int) $this->_totalResultsReturned;
  150. }
  151. /**
  152. * Number of available results.
  153. *
  154. * @return int total number of available results
  155. */
  156. public function totalResultsAvailable()
  157. {
  158. return (int) $this->_totalResultsAvailable;
  159. }
  160. /**
  161. * Implements SeekableIterator::current().
  162. *
  163. * @return void
  164. * @throws Zend_Service_Exception
  165. * @abstract
  166. */
  167. // abstract public function current();
  168. /**
  169. * Implements SeekableIterator::key().
  170. *
  171. * @return int
  172. */
  173. public function key()
  174. {
  175. return $this->_currentIndex;
  176. }
  177. /**
  178. * Implements SeekableIterator::next().
  179. *
  180. * @return void
  181. */
  182. public function next()
  183. {
  184. $this->_currentIndex += 1;
  185. }
  186. /**
  187. * Implements SeekableIterator::rewind().
  188. *
  189. * @return bool
  190. */
  191. public function rewind()
  192. {
  193. $this->_currentIndex = 0;
  194. return true;
  195. }
  196. /**
  197. * Implement SeekableIterator::seek().
  198. *
  199. * @param int $index
  200. * @return void
  201. * @throws OutOfBoundsException
  202. */
  203. public function seek($index)
  204. {
  205. $indexInt = (int) $index;
  206. if ($indexInt >= 0 && $indexInt < $this->_results->length) {
  207. $this->_currentIndex = $indexInt;
  208. } else {
  209. throw new OutOfBoundsException("Illegal index '$index'");
  210. }
  211. }
  212. /**
  213. * Implement SeekableIterator::valid().
  214. *
  215. * @return boolean
  216. */
  217. public function valid()
  218. {
  219. return null !== $this->_results && $this->_currentIndex < $this->_results->length;
  220. }
  221. /**
  222. * Returns the response document as XML string.
  223. *
  224. * @return string the response document converted into XML format
  225. */
  226. public function getXml()
  227. {
  228. return $this->_dom->saveXML();
  229. }
  230. /**
  231. * Overwrites standard __sleep method to make this object serializable.
  232. *
  233. * DomDocument and DOMXpath objects cannot be serialized.
  234. * This method converts them back to an XML string.
  235. *
  236. * @return void
  237. */
  238. public function __sleep() {
  239. $this->_xml = $this->getXml();
  240. $vars = array_keys(get_object_vars($this));
  241. return array_diff($vars, array('_dom', '_xpath'));
  242. }
  243. /**
  244. * Overwrites standard __wakeup method to make this object unserializable.
  245. *
  246. * Restores object status before serialization.
  247. * Converts XML string into a DomDocument object and creates a valid
  248. * DOMXpath instance for given DocDocument.
  249. *
  250. * @return void
  251. */
  252. public function __wakeup() {
  253. $dom = new DOMDocument();
  254. $dom->loadXml($this->_xml);
  255. $this->_init($dom);
  256. $this->_xml = null; // reset XML content
  257. }
  258. }