File indexing completed on 2024-12-22 05:37:02
0001 <?php 0002 0003 /** 0004 * Zend Framework 0005 * 0006 * LICENSE 0007 * 0008 * This source file is subject to the new BSD license that is bundled 0009 * with this package in the file LICENSE.txt. 0010 * It is also available through the world-wide-web at this URL: 0011 * http://framework.zend.com/license/new-bsd 0012 * If you did not receive a copy of the license and are unable to 0013 * obtain it through the world-wide-web, please send an email 0014 * to license@zend.com so we can send you a copy immediately. 0015 * 0016 * @category Zend 0017 * @package Zend_Service 0018 * @subpackage Amazon 0019 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 * @version $Id$ 0022 */ 0023 0024 0025 /** 0026 * @see Zend_Service_Amazon_Item 0027 */ 0028 // require_once 'Zend/Service/Amazon/Item.php'; 0029 0030 0031 /** 0032 * @category Zend 0033 * @package Zend_Service 0034 * @subpackage Amazon 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 */ 0038 class Zend_Service_Amazon_ResultSet implements SeekableIterator 0039 { 0040 /** 0041 * A DOMNodeList of <Item> elements 0042 * 0043 * @var DOMNodeList 0044 */ 0045 protected $_results = null; 0046 0047 /** 0048 * Amazon Web Service Return Document 0049 * 0050 * @var DOMDocument 0051 */ 0052 protected $_dom; 0053 0054 /** 0055 * XPath Object for $this->_dom 0056 * 0057 * @var DOMXPath 0058 */ 0059 protected $_xpath; 0060 0061 /** 0062 * Current index for SeekableIterator 0063 * 0064 * @var int 0065 */ 0066 protected $_currentIndex = 0; 0067 0068 /** 0069 * Create an instance of Zend_Service_Amazon_ResultSet and create the necessary data objects 0070 * 0071 * @param DOMDocument $dom 0072 * @return void 0073 */ 0074 public function __construct(DOMDocument $dom) 0075 { 0076 $this->_dom = $dom; 0077 $this->_xpath = new DOMXPath($dom); 0078 $this->_xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2011-08-01'); 0079 $this->_results = $this->_xpath->query('//az:Item'); 0080 } 0081 0082 /** 0083 * Total Number of results returned 0084 * 0085 * @return int Total number of results returned 0086 */ 0087 public function totalResults() 0088 { 0089 $result = $this->_xpath->query('//az:TotalResults/text()'); 0090 return (int) $result->item(0)->data; 0091 } 0092 0093 /** 0094 * Total Number of pages returned 0095 * 0096 * @return int Total number of pages returned 0097 */ 0098 public function totalPages() 0099 { 0100 $result = $this->_xpath->query('//az:TotalPages/text()'); 0101 return (int) $result->item(0)->data; 0102 } 0103 0104 /** 0105 * Implement SeekableIterator::current() 0106 * 0107 * @return Zend_Service_Amazon_Item 0108 */ 0109 public function current() 0110 { 0111 return new Zend_Service_Amazon_Item($this->_results->item($this->_currentIndex)); 0112 } 0113 0114 /** 0115 * Implement SeekableIterator::key() 0116 * 0117 * @return int 0118 */ 0119 public function key() 0120 { 0121 return $this->_currentIndex; 0122 } 0123 0124 /** 0125 * Implement SeekableIterator::next() 0126 * 0127 * @return void 0128 */ 0129 public function next() 0130 { 0131 $this->_currentIndex += 1; 0132 } 0133 0134 /** 0135 * Implement SeekableIterator::rewind() 0136 * 0137 * @return void 0138 */ 0139 public function rewind() 0140 { 0141 $this->_currentIndex = 0; 0142 } 0143 0144 /** 0145 * Implement SeekableIterator::seek() 0146 * 0147 * @param int $index 0148 * @throws OutOfBoundsException 0149 * @return void 0150 */ 0151 public function seek($index) 0152 { 0153 $indexInt = (int) $index; 0154 if ($indexInt >= 0 && (null === $this->_results || $indexInt < $this->_results->length)) { 0155 $this->_currentIndex = $indexInt; 0156 } else { 0157 throw new OutOfBoundsException("Illegal index '$index'"); 0158 } 0159 } 0160 0161 /** 0162 * Implement SeekableIterator::valid() 0163 * 0164 * @return boolean 0165 */ 0166 public function valid() 0167 { 0168 return null !== $this->_results && $this->_currentIndex < $this->_results->length; 0169 } 0170 }