File indexing completed on 2024-12-22 05:37:03

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 Flickr
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_Flickr_Result
0027  */
0028 // require_once 'Zend/Service/Flickr/Result.php';
0029 
0030 
0031 /**
0032  * @category   Zend
0033  * @package    Zend_Service
0034  * @subpackage Flickr
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_Flickr_ResultSet implements SeekableIterator
0039 {
0040     /**
0041      * Total number of available results
0042      *
0043      * @var int
0044      */
0045     public $totalResultsAvailable;
0046 
0047     /**
0048      * Number of results in this result set
0049      *
0050      * @var int
0051      */
0052     public $totalResultsReturned;
0053 
0054     /**
0055      * The offset of this result set in the total set of available results
0056      *
0057      * @var int
0058      */
0059     public $firstResultPosition;
0060 
0061     /**
0062      * Results storage
0063      *
0064      * @var DOMNodeList
0065      */
0066     protected $_results = null;
0067 
0068     /**
0069      * Reference to Zend_Service_Flickr object with which the request was made
0070      *
0071      * @var Zend_Service_Flickr
0072      */
0073     private $_flickr;
0074 
0075     /**
0076      * Current index for the Iterator
0077      *
0078      * @var int
0079      */
0080     private $_currentIndex = 0;
0081 
0082     /**
0083      * Parse the Flickr Result Set
0084      *
0085      * @param  DOMDocument         $dom
0086      * @param  Zend_Service_Flickr $flickr
0087      * @return void
0088      */
0089     public function __construct(DOMDocument $dom, Zend_Service_Flickr $flickr)
0090     {
0091         $this->_flickr = $flickr;
0092 
0093         $xpath = new DOMXPath($dom);
0094 
0095         $photos = $xpath->query('//photos')->item(0);
0096 
0097         $page    = $photos->getAttribute('page');
0098         $pages   = $photos->getAttribute('pages');
0099         $perPage = $photos->getAttribute('perpage');
0100         $total   = $photos->getAttribute('total');
0101 
0102         $this->totalResultsReturned  = ($page == $pages || $pages == 0) ? ($total - ($page - 1) * $perPage) : (int) $perPage;
0103         $this->firstResultPosition   = ($page - 1) * $perPage + 1;
0104         $this->totalResultsAvailable = (int) $total;
0105 
0106         if ($total > 0) {
0107             $this->_results = $xpath->query('//photo');
0108         }
0109     }
0110 
0111     /**
0112      * Total Number of results returned
0113      *
0114      * @return int Total number of results returned
0115      */
0116     public function totalResults()
0117     {
0118         return $this->totalResultsReturned;
0119     }
0120 
0121     /**
0122      * Implements SeekableIterator::current()
0123      *
0124      * @return Zend_Service_Flickr_Result
0125      */
0126     public function current()
0127     {
0128         return new Zend_Service_Flickr_Result($this->_results->item($this->_currentIndex), $this->_flickr);
0129     }
0130 
0131     /**
0132      * Implements SeekableIterator::key()
0133      *
0134      * @return int
0135      */
0136     public function key()
0137     {
0138         return $this->_currentIndex;
0139     }
0140 
0141     /**
0142      * Implements SeekableIterator::next()
0143      *
0144      * @return void
0145      */
0146     public function next()
0147     {
0148         $this->_currentIndex += 1;
0149     }
0150 
0151     /**
0152      * Implements SeekableIterator::rewind()
0153      *
0154      * @return void
0155      */
0156     public function rewind()
0157     {
0158         $this->_currentIndex = 0;
0159     }
0160 
0161     /**
0162      * Implements SeekableIterator::seek()
0163      *
0164      * @param  int $index
0165      * @throws OutOfBoundsException
0166      * @return void
0167      */
0168     public function seek($index)
0169     {
0170         $indexInt = (int) $index;
0171         if ($indexInt >= 0 && (null === $this->_results || $indexInt < $this->_results->length)) {
0172             $this->_currentIndex = $indexInt;
0173         } else {
0174             throw new OutOfBoundsException("Illegal index '$index'");
0175         }
0176     }
0177 
0178     /**
0179      * Implements SeekableIterator::valid()
0180      *
0181      * @return boolean
0182      */
0183     public function valid()
0184     {
0185         return null !== $this->_results && $this->_currentIndex < $this->_results->length;
0186     }
0187 }
0188