File indexing completed on 2024-06-16 05:30:27

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 Yahoo
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  * @category   Zend
0027  * @package    Zend_Service
0028  * @subpackage Yahoo
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Service_Yahoo_ResultSet implements SeekableIterator
0033 {
0034     /**
0035      * Total number of results available
0036      *
0037      * @var int
0038      */
0039     public $totalResultsAvailable;
0040 
0041     /**
0042      * The number of results in this result set
0043      *
0044      * @var int
0045      */
0046     public $totalResultsReturned;
0047 
0048     /**
0049      * The offset in the total result set of this search set
0050      *
0051      * @var int
0052      */
0053     public $firstResultPosition;
0054 
0055     /**
0056      * A DOMNodeList of results
0057      *
0058      * @var DOMNodeList
0059      */
0060     protected $_results;
0061 
0062     /**
0063      * Yahoo Web Service Return Document
0064      *
0065      * @var DOMDocument
0066      */
0067     protected $_dom;
0068 
0069     /**
0070      * Xpath Object for $this->_dom
0071      *
0072      * @var DOMXPath
0073      */
0074     protected $_xpath;
0075 
0076     /**
0077      * Current Index for SeekableIterator
0078      *
0079      * @var int
0080      */
0081     protected $_currentIndex = 0;
0082 
0083 
0084     /**
0085      * Parse the search response and retrieve the results for iteration
0086      *
0087      * @param  DOMDocument $dom the REST fragment for this object
0088      * @return void
0089      */
0090     public function __construct(DOMDocument $dom)
0091     {
0092         $this->totalResultsAvailable = (int) $dom->documentElement->getAttribute('totalResultsAvailable');
0093         $this->totalResultsReturned = (int) $dom->documentElement->getAttribute('totalResultsReturned');
0094         $this->firstResultPosition = (int) $dom->documentElement->getAttribute('firstResultPosition');
0095 
0096         $this->_dom = $dom;
0097         $this->_xpath = new DOMXPath($dom);
0098 
0099         $this->_xpath->registerNamespace('yh', $this->_namespace);
0100 
0101         $this->_results = $this->_xpath->query('//yh:Result');
0102     }
0103 
0104 
0105     /**
0106      * Total Number of results returned
0107      *
0108      * @return int Total number of results returned
0109      */
0110     public function totalResults()
0111     {
0112         return $this->totalResultsReturned;
0113     }
0114 
0115 
0116     /**
0117      * Implement SeekableIterator::current()
0118      *
0119      * Must be implemented by child classes
0120      *
0121      * @throws Zend_Service_Exception
0122      * @return Zend_Service_Yahoo_Result
0123      */
0124     public function current()
0125     {
0126         /**
0127          * @see Zend_Service_Exception
0128          */
0129         // require_once 'Zend/Service/Exception.php';
0130         throw new Zend_Service_Exception('Zend_Service_Yahoo_ResultSet::current() must be implemented by child '
0131                                        . 'classes');
0132     }
0133 
0134 
0135     /**
0136      * Implement SeekableIterator::key()
0137      *
0138      * @return int
0139      */
0140     public function key()
0141     {
0142         return $this->_currentIndex;
0143     }
0144 
0145 
0146     /**
0147      * Implement SeekableIterator::next()
0148      *
0149      * @return void
0150      */
0151     public function next()
0152     {
0153         $this->_currentIndex += 1;
0154     }
0155 
0156 
0157     /**
0158      * Implement SeekableIterator::rewind()
0159      *
0160      * @return void
0161      */
0162     public function rewind()
0163     {
0164         $this->_currentIndex = 0;
0165     }
0166 
0167 
0168     /**
0169      * Implement SeekableIterator::seek()
0170      *
0171      * @param  int $index
0172      * @return void
0173      * @throws OutOfBoundsException
0174      */
0175     public function seek($index)
0176     {
0177         $indexInt = (int) $index;
0178         if ($indexInt >= 0 && $indexInt < $this->_results->length) {
0179             $this->_currentIndex = $indexInt;
0180         } else {
0181             throw new OutOfBoundsException("Illegal index '$index'");
0182         }
0183     }
0184 
0185 
0186     /**
0187      * Implement SeekableIterator::valid()
0188      *
0189      * @return boolean
0190      */
0191     public function valid()
0192     {
0193         return $this->_currentIndex < $this->_results->length;
0194     }
0195 }