ResultSet.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Flickr
  18. * @copyright Copyright (c) 2005-2015 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. * @see Zend_Service_Flickr_Result
  24. */
  25. require_once 'Zend/Service/Flickr/Result.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Flickr
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_Flickr_ResultSet implements SeekableIterator
  34. {
  35. /**
  36. * Total number of available results
  37. *
  38. * @var int
  39. */
  40. public $totalResultsAvailable;
  41. /**
  42. * Number of results in this result set
  43. *
  44. * @var int
  45. */
  46. public $totalResultsReturned;
  47. /**
  48. * The offset of this result set in the total set of available results
  49. *
  50. * @var int
  51. */
  52. public $firstResultPosition;
  53. /**
  54. * Results storage
  55. *
  56. * @var DOMNodeList
  57. */
  58. protected $_results = null;
  59. /**
  60. * Reference to Zend_Service_Flickr object with which the request was made
  61. *
  62. * @var Zend_Service_Flickr
  63. */
  64. private $_flickr;
  65. /**
  66. * Current index for the Iterator
  67. *
  68. * @var int
  69. */
  70. private $_currentIndex = 0;
  71. /**
  72. * Parse the Flickr Result Set
  73. *
  74. * @param DOMDocument $dom
  75. * @param Zend_Service_Flickr $flickr
  76. * @return void
  77. */
  78. public function __construct(DOMDocument $dom, Zend_Service_Flickr $flickr)
  79. {
  80. $this->_flickr = $flickr;
  81. $xpath = new DOMXPath($dom);
  82. $photos = $xpath->query('//photos')->item(0);
  83. $page = $photos->getAttribute('page');
  84. $pages = $photos->getAttribute('pages');
  85. $perPage = $photos->getAttribute('perpage');
  86. $total = $photos->getAttribute('total');
  87. $this->totalResultsReturned = ($page == $pages || $pages == 0) ? ($total - ($page - 1) * $perPage) : (int) $perPage;
  88. $this->firstResultPosition = ($page - 1) * $perPage + 1;
  89. $this->totalResultsAvailable = (int) $total;
  90. if ($total > 0) {
  91. $this->_results = $xpath->query('//photo');
  92. }
  93. }
  94. /**
  95. * Total Number of results returned
  96. *
  97. * @return int Total number of results returned
  98. */
  99. public function totalResults()
  100. {
  101. return $this->totalResultsReturned;
  102. }
  103. /**
  104. * Implements SeekableIterator::current()
  105. *
  106. * @return Zend_Service_Flickr_Result
  107. */
  108. public function current()
  109. {
  110. return new Zend_Service_Flickr_Result($this->_results->item($this->_currentIndex), $this->_flickr);
  111. }
  112. /**
  113. * Implements SeekableIterator::key()
  114. *
  115. * @return int
  116. */
  117. public function key()
  118. {
  119. return $this->_currentIndex;
  120. }
  121. /**
  122. * Implements SeekableIterator::next()
  123. *
  124. * @return void
  125. */
  126. public function next()
  127. {
  128. $this->_currentIndex += 1;
  129. }
  130. /**
  131. * Implements SeekableIterator::rewind()
  132. *
  133. * @return void
  134. */
  135. public function rewind()
  136. {
  137. $this->_currentIndex = 0;
  138. }
  139. /**
  140. * Implements SeekableIterator::seek()
  141. *
  142. * @param int $index
  143. * @throws OutOfBoundsException
  144. * @return void
  145. */
  146. public function seek($index)
  147. {
  148. $indexInt = (int) $index;
  149. if ($indexInt >= 0 && (null === $this->_results || $indexInt < $this->_results->length)) {
  150. $this->_currentIndex = $indexInt;
  151. } else {
  152. throw new OutOfBoundsException("Illegal index '$index'");
  153. }
  154. }
  155. /**
  156. * Implements SeekableIterator::valid()
  157. *
  158. * @return boolean
  159. */
  160. public function valid()
  161. {
  162. return null !== $this->_results && $this->_currentIndex < $this->_results->length;
  163. }
  164. }