web/lib/Zend/Service/Amazon/ResultSet.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Zend Framework
       
     5  *
       
     6  * LICENSE
       
     7  *
       
     8  * This source file is subject to the new BSD license that is bundled
       
     9  * with this package in the file LICENSE.txt.
       
    10  * It is also available through the world-wide-web at this URL:
       
    11  * http://framework.zend.com/license/new-bsd
       
    12  * If you did not receive a copy of the license and are unable to
       
    13  * obtain it through the world-wide-web, please send an email
       
    14  * to license@zend.com so we can send you a copy immediately.
       
    15  *
       
    16  * @category   Zend
       
    17  * @package    Zend_Service
       
    18  * @subpackage Amazon
       
    19  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  * @version    $Id: ResultSet.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    22  */
       
    23 
       
    24 
       
    25 /**
       
    26  * @see Zend_Service_Amazon_Item
       
    27  */
       
    28 require_once 'Zend/Service/Amazon/Item.php';
       
    29 
       
    30 
       
    31 /**
       
    32  * @category   Zend
       
    33  * @package    Zend_Service
       
    34  * @subpackage Amazon
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Service_Amazon_ResultSet implements SeekableIterator
       
    39 {
       
    40     /**
       
    41      * A DOMNodeList of <Item> elements
       
    42      *
       
    43      * @var DOMNodeList
       
    44      */
       
    45     protected $_results = null;
       
    46 
       
    47     /**
       
    48      * Amazon Web Service Return Document
       
    49      *
       
    50      * @var DOMDocument
       
    51      */
       
    52     protected $_dom;
       
    53 
       
    54     /**
       
    55      * XPath Object for $this->_dom
       
    56      *
       
    57      * @var DOMXPath
       
    58      */
       
    59     protected $_xpath;
       
    60 
       
    61     /**
       
    62      * Current index for SeekableIterator
       
    63      *
       
    64      * @var int
       
    65      */
       
    66     protected $_currentIndex = 0;
       
    67 
       
    68     /**
       
    69      * Create an instance of Zend_Service_Amazon_ResultSet and create the necessary data objects
       
    70      *
       
    71      * @param  DOMDocument $dom
       
    72      * @return void
       
    73      */
       
    74     public function __construct(DOMDocument $dom)
       
    75     {
       
    76         $this->_dom = $dom;
       
    77         $this->_xpath = new DOMXPath($dom);
       
    78         $this->_xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
       
    79         $this->_results = $this->_xpath->query('//az:Item');
       
    80     }
       
    81 
       
    82     /**
       
    83      * Total Number of results returned
       
    84      *
       
    85      * @return int Total number of results returned
       
    86      */
       
    87     public function totalResults()
       
    88     {
       
    89         $result = $this->_xpath->query('//az:TotalResults/text()');
       
    90         return (int) $result->item(0)->data;
       
    91     }
       
    92 
       
    93     /**
       
    94      * Total Number of pages returned
       
    95      *
       
    96      * @return int Total number of pages returned
       
    97      */
       
    98     public function totalPages()
       
    99     {
       
   100         $result = $this->_xpath->query('//az:TotalPages/text()');
       
   101         return (int) $result->item(0)->data;
       
   102     }
       
   103 
       
   104     /**
       
   105      * Implement SeekableIterator::current()
       
   106      *
       
   107      * @return Zend_Service_Amazon_Item
       
   108      */
       
   109     public function current()
       
   110     {
       
   111         return new Zend_Service_Amazon_Item($this->_results->item($this->_currentIndex));
       
   112     }
       
   113 
       
   114     /**
       
   115      * Implement SeekableIterator::key()
       
   116      *
       
   117      * @return int
       
   118      */
       
   119     public function key()
       
   120     {
       
   121         return $this->_currentIndex;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Implement SeekableIterator::next()
       
   126      *
       
   127      * @return void
       
   128      */
       
   129     public function next()
       
   130     {
       
   131         $this->_currentIndex += 1;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Implement SeekableIterator::rewind()
       
   136      *
       
   137      * @return void
       
   138      */
       
   139     public function rewind()
       
   140     {
       
   141         $this->_currentIndex = 0;
       
   142     }
       
   143 
       
   144     /**
       
   145      * Implement SeekableIterator::seek()
       
   146      *
       
   147      * @param  int $index
       
   148      * @throws OutOfBoundsException
       
   149      * @return void
       
   150      */
       
   151     public function seek($index)
       
   152     {
       
   153         $indexInt = (int) $index;
       
   154         if ($indexInt >= 0 && (null === $this->_results || $indexInt < $this->_results->length)) {
       
   155             $this->_currentIndex = $indexInt;
       
   156         } else {
       
   157             throw new OutOfBoundsException("Illegal index '$index'");
       
   158         }
       
   159     }
       
   160 
       
   161     /**
       
   162      * Implement SeekableIterator::valid()
       
   163      *
       
   164      * @return boolean
       
   165      */
       
   166     public function valid()
       
   167     {
       
   168         return null !== $this->_results && $this->_currentIndex < $this->_results->length;
       
   169     }
       
   170 }