ResultSet.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Yahoo
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage Yahoo
  26. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_Yahoo_ResultSet implements SeekableIterator
  30. {
  31. /**
  32. * Total number of results available
  33. *
  34. * @var int
  35. */
  36. public $totalResultsAvailable;
  37. /**
  38. * The number of results in this result set
  39. *
  40. * @var int
  41. */
  42. public $totalResultsReturned;
  43. /**
  44. * The offset in the total result set of this search set
  45. *
  46. * @var int
  47. */
  48. public $firstResultPosition;
  49. /**
  50. * A DOMNodeList of results
  51. *
  52. * @var DOMNodeList
  53. */
  54. protected $_results;
  55. /**
  56. * Yahoo Web Service Return Document
  57. *
  58. * @var DOMDocument
  59. */
  60. protected $_dom;
  61. /**
  62. * Xpath Object for $this->_dom
  63. *
  64. * @var DOMXPath
  65. */
  66. protected $_xpath;
  67. /**
  68. * Current Index for SeekableIterator
  69. *
  70. * @var int
  71. */
  72. protected $_currentIndex = 0;
  73. /**
  74. * Parse the search response and retrieve the results for iteration
  75. *
  76. * @param DOMDocument $dom the REST fragment for this object
  77. * @return void
  78. */
  79. public function __construct(DOMDocument $dom)
  80. {
  81. $this->totalResultsAvailable = (int) $dom->documentElement->getAttribute('totalResultsAvailable');
  82. $this->totalResultsReturned = (int) $dom->documentElement->getAttribute('totalResultsReturned');
  83. $this->firstResultPosition = (int) $dom->documentElement->getAttribute('firstResultPosition');
  84. $this->_dom = $dom;
  85. $this->_xpath = new DOMXPath($dom);
  86. $this->_xpath->registerNamespace('yh', $this->_namespace);
  87. $this->_results = $this->_xpath->query('//yh:Result');
  88. }
  89. /**
  90. * Total Number of results returned
  91. *
  92. * @return int Total number of results returned
  93. */
  94. public function totalResults()
  95. {
  96. return $this->totalResultsReturned;
  97. }
  98. /**
  99. * Implement SeekableIterator::current()
  100. *
  101. * Must be implemented by child classes
  102. *
  103. * @throws Zend_Service_Exception
  104. * @return Zend_Service_Yahoo_Result
  105. */
  106. public function current()
  107. {
  108. /**
  109. * @see Zend_Service_Exception
  110. */
  111. require_once 'Zend/Service/Exception.php';
  112. throw new Zend_Service_Exception('Zend_Service_Yahoo_ResultSet::current() must be implemented by child '
  113. . 'classes');
  114. }
  115. /**
  116. * Implement SeekableIterator::key()
  117. *
  118. * @return int
  119. */
  120. public function key()
  121. {
  122. return $this->_currentIndex;
  123. }
  124. /**
  125. * Implement SeekableIterator::next()
  126. *
  127. * @return void
  128. */
  129. public function next()
  130. {
  131. $this->_currentIndex += 1;
  132. }
  133. /**
  134. * Implement SeekableIterator::rewind()
  135. *
  136. * @return void
  137. */
  138. public function rewind()
  139. {
  140. $this->_currentIndex = 0;
  141. }
  142. /**
  143. * Implement SeekableIterator::seek()
  144. *
  145. * @param int $index
  146. * @return void
  147. * @throws OutOfBoundsException
  148. */
  149. public function seek($index)
  150. {
  151. $indexInt = (int) $index;
  152. if ($indexInt >= 0 && $indexInt < $this->_results->length) {
  153. $this->_currentIndex = $indexInt;
  154. } else {
  155. throw new OutOfBoundsException("Illegal index '$index'");
  156. }
  157. }
  158. /**
  159. * Implement SeekableIterator::valid()
  160. *
  161. * @return boolean
  162. */
  163. public function valid()
  164. {
  165. return $this->_currentIndex < $this->_results->length;
  166. }
  167. }