web/enmi/Zend/Service/Flickr/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 Flickr
       
    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_Flickr_Result
       
    27  */
       
    28 require_once 'Zend/Service/Flickr/Result.php';
       
    29 
       
    30 
       
    31 /**
       
    32  * @category   Zend
       
    33  * @package    Zend_Service
       
    34  * @subpackage Flickr
       
    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_Flickr_ResultSet implements SeekableIterator
       
    39 {
       
    40     /**
       
    41      * Total number of available results
       
    42      *
       
    43      * @var int
       
    44      */
       
    45     public $totalResultsAvailable;
       
    46 
       
    47     /**
       
    48      * Number of results in this result set
       
    49      *
       
    50      * @var int
       
    51      */
       
    52     public $totalResultsReturned;
       
    53 
       
    54     /**
       
    55      * The offset of this result set in the total set of available results
       
    56      *
       
    57      * @var int
       
    58      */
       
    59     public $firstResultPosition;
       
    60 
       
    61     /**
       
    62      * Results storage
       
    63      *
       
    64      * @var DOMNodeList
       
    65      */
       
    66     protected $_results = null;
       
    67 
       
    68     /**
       
    69      * Reference to Zend_Service_Flickr object with which the request was made
       
    70      *
       
    71      * @var Zend_Service_Flickr
       
    72      */
       
    73     private $_flickr;
       
    74 
       
    75     /**
       
    76      * Current index for the Iterator
       
    77      *
       
    78      * @var int
       
    79      */
       
    80     private $_currentIndex = 0;
       
    81 
       
    82     /**
       
    83      * Parse the Flickr Result Set
       
    84      *
       
    85      * @param  DOMDocument         $dom
       
    86      * @param  Zend_Service_Flickr $flickr
       
    87      * @return void
       
    88      */
       
    89     public function __construct(DOMDocument $dom, Zend_Service_Flickr $flickr)
       
    90     {
       
    91         $this->_flickr = $flickr;
       
    92 
       
    93         $xpath = new DOMXPath($dom);
       
    94 
       
    95         $photos = $xpath->query('//photos')->item(0);
       
    96 
       
    97         $page    = $photos->getAttribute('page');
       
    98         $pages   = $photos->getAttribute('pages');
       
    99         $perPage = $photos->getAttribute('perpage');
       
   100         $total   = $photos->getAttribute('total');
       
   101 
       
   102         $this->totalResultsReturned  = ($page == $pages || $pages == 0) ? ($total - ($page - 1) * $perPage) : (int) $perPage;
       
   103         $this->firstResultPosition   = ($page - 1) * $perPage + 1;
       
   104         $this->totalResultsAvailable = (int) $total;
       
   105 
       
   106         if ($total > 0) {
       
   107             $this->_results = $xpath->query('//photo');
       
   108         }
       
   109     }
       
   110 
       
   111     /**
       
   112      * Total Number of results returned
       
   113      *
       
   114      * @return int Total number of results returned
       
   115      */
       
   116     public function totalResults()
       
   117     {
       
   118         return $this->totalResultsReturned;
       
   119     }
       
   120 
       
   121     /**
       
   122      * Implements SeekableIterator::current()
       
   123      *
       
   124      * @return Zend_Service_Flickr_Result
       
   125      */
       
   126     public function current()
       
   127     {
       
   128         return new Zend_Service_Flickr_Result($this->_results->item($this->_currentIndex), $this->_flickr);
       
   129     }
       
   130 
       
   131     /**
       
   132      * Implements SeekableIterator::key()
       
   133      *
       
   134      * @return int
       
   135      */
       
   136     public function key()
       
   137     {
       
   138         return $this->_currentIndex;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Implements SeekableIterator::next()
       
   143      *
       
   144      * @return void
       
   145      */
       
   146     public function next()
       
   147     {
       
   148         $this->_currentIndex += 1;
       
   149     }
       
   150 
       
   151     /**
       
   152      * Implements SeekableIterator::rewind()
       
   153      *
       
   154      * @return void
       
   155      */
       
   156     public function rewind()
       
   157     {
       
   158         $this->_currentIndex = 0;
       
   159     }
       
   160 
       
   161     /**
       
   162      * Implements SeekableIterator::seek()
       
   163      *
       
   164      * @param  int $index
       
   165      * @throws OutOfBoundsException
       
   166      * @return void
       
   167      */
       
   168     public function seek($index)
       
   169     {
       
   170         $indexInt = (int) $index;
       
   171         if ($indexInt >= 0 && (null === $this->_results || $indexInt < $this->_results->length)) {
       
   172             $this->_currentIndex = $indexInt;
       
   173         } else {
       
   174             throw new OutOfBoundsException("Illegal index '$index'");
       
   175         }
       
   176     }
       
   177 
       
   178     /**
       
   179      * Implements SeekableIterator::valid()
       
   180      *
       
   181      * @return boolean
       
   182      */
       
   183     public function valid()
       
   184     {
       
   185         return null !== $this->_results && $this->_currentIndex < $this->_results->length;
       
   186     }
       
   187 }
       
   188