ResultSet.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 Amazon
  18. * @copyright Copyright (c) 2005-2012 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_Amazon_Item
  24. */
  25. require_once 'Zend/Service/Amazon/Item.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Amazon
  30. * @copyright Copyright (c) 2005-2012 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_Amazon_ResultSet implements SeekableIterator
  34. {
  35. /**
  36. * A DOMNodeList of <Item> elements
  37. *
  38. * @var DOMNodeList
  39. */
  40. protected $_results = null;
  41. /**
  42. * Amazon Web Service Return Document
  43. *
  44. * @var DOMDocument
  45. */
  46. protected $_dom;
  47. /**
  48. * XPath Object for $this->_dom
  49. *
  50. * @var DOMXPath
  51. */
  52. protected $_xpath;
  53. /**
  54. * Current index for SeekableIterator
  55. *
  56. * @var int
  57. */
  58. protected $_currentIndex = 0;
  59. /**
  60. * Create an instance of Zend_Service_Amazon_ResultSet and create the necessary data objects
  61. *
  62. * @param DOMDocument $dom
  63. * @return void
  64. */
  65. public function __construct(DOMDocument $dom)
  66. {
  67. $this->_dom = $dom;
  68. $this->_xpath = new DOMXPath($dom);
  69. $this->_xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2011-08-01');
  70. $this->_results = $this->_xpath->query('//az:Item');
  71. }
  72. /**
  73. * Total Number of results returned
  74. *
  75. * @return int Total number of results returned
  76. */
  77. public function totalResults()
  78. {
  79. $result = $this->_xpath->query('//az:TotalResults/text()');
  80. return (int) $result->item(0)->data;
  81. }
  82. /**
  83. * Total Number of pages returned
  84. *
  85. * @return int Total number of pages returned
  86. */
  87. public function totalPages()
  88. {
  89. $result = $this->_xpath->query('//az:TotalPages/text()');
  90. return (int) $result->item(0)->data;
  91. }
  92. /**
  93. * Implement SeekableIterator::current()
  94. *
  95. * @return Zend_Service_Amazon_Item
  96. */
  97. public function current()
  98. {
  99. return new Zend_Service_Amazon_Item($this->_results->item($this->_currentIndex));
  100. }
  101. /**
  102. * Implement SeekableIterator::key()
  103. *
  104. * @return int
  105. */
  106. public function key()
  107. {
  108. return $this->_currentIndex;
  109. }
  110. /**
  111. * Implement SeekableIterator::next()
  112. *
  113. * @return void
  114. */
  115. public function next()
  116. {
  117. $this->_currentIndex += 1;
  118. }
  119. /**
  120. * Implement SeekableIterator::rewind()
  121. *
  122. * @return void
  123. */
  124. public function rewind()
  125. {
  126. $this->_currentIndex = 0;
  127. }
  128. /**
  129. * Implement SeekableIterator::seek()
  130. *
  131. * @param int $index
  132. * @throws OutOfBoundsException
  133. * @return void
  134. */
  135. public function seek($index)
  136. {
  137. $indexInt = (int) $index;
  138. if ($indexInt >= 0 && (null === $this->_results || $indexInt < $this->_results->length)) {
  139. $this->_currentIndex = $indexInt;
  140. } else {
  141. throw new OutOfBoundsException("Illegal index '$index'");
  142. }
  143. }
  144. /**
  145. * Implement SeekableIterator::valid()
  146. *
  147. * @return boolean
  148. */
  149. public function valid()
  150. {
  151. return null !== $this->_results && $this->_currentIndex < $this->_results->length;
  152. }
  153. }