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