web/enmi/Zend/Service/Technorati/ResultSet.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service
       
    17  * @subpackage Technorati
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: ResultSet.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * @see Zend_Service_Technorati_Result
       
    26  */
       
    27 require_once 'Zend/Service/Technorati/Result.php';
       
    28 
       
    29 
       
    30 /**
       
    31  * This is the most essential result set.
       
    32  * The scope of this class is to be extended by a query-specific child result set class,
       
    33  * and it should never be used to initialize a standalone object.
       
    34  *
       
    35  * Each of the specific result sets represents a collection of query-specific
       
    36  * Zend_Service_Technorati_Result objects.
       
    37  *
       
    38  * @category   Zend
       
    39  * @package    Zend_Service
       
    40  * @subpackage Technorati
       
    41  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    43  * @abstract
       
    44  */
       
    45 abstract class Zend_Service_Technorati_ResultSet implements SeekableIterator
       
    46 {
       
    47     /**
       
    48      * The total number of results available
       
    49      *
       
    50      * @var     int
       
    51      * @access  protected
       
    52      */
       
    53     protected $_totalResultsAvailable;
       
    54 
       
    55     /**
       
    56      * The number of results in this result set
       
    57      *
       
    58      * @var     int
       
    59      * @access  protected
       
    60      */
       
    61     protected $_totalResultsReturned;
       
    62 
       
    63     /**
       
    64      * The offset in the total result set of this search set
       
    65      *
       
    66      * @var     int
       
    67      */
       
    68     //TODO public $firstResultPosition;
       
    69 
       
    70 
       
    71     /**
       
    72      * A DomNodeList of results
       
    73      *
       
    74      * @var     DomNodeList
       
    75      * @access  protected
       
    76      */
       
    77     protected $_results;
       
    78 
       
    79     /**
       
    80      * Technorati API response document
       
    81      *
       
    82      * @var     DomDocument
       
    83      * @access  protected
       
    84      */
       
    85     protected $_dom;
       
    86 
       
    87     /**
       
    88      * Object for $this->_dom
       
    89      *
       
    90      * @var     DOMXpath
       
    91      * @access  protected
       
    92      */
       
    93     protected $_xpath;
       
    94 
       
    95     /**
       
    96      * XML string representation for $this->_dom
       
    97      *
       
    98      * @var     string
       
    99      * @access  protected
       
   100      */
       
   101     protected $_xml;
       
   102 
       
   103     /**
       
   104      * Current Item
       
   105      *
       
   106      * @var     int
       
   107      * @access  protected
       
   108      */
       
   109     protected $_currentIndex = 0;
       
   110 
       
   111 
       
   112     /**
       
   113      * Parses the search response and retrieves the results for iteration.
       
   114      *
       
   115      * @param   DomDocument $dom    the ReST fragment for this object
       
   116      * @param   array $options      query options as associative array
       
   117      */
       
   118     public function __construct(DomDocument $dom, $options = array())
       
   119     {
       
   120         $this->_init($dom, $options);
       
   121 
       
   122         // Technorati loves to make developer's life really hard
       
   123         // I must read query options in order to normalize a single way
       
   124         // to display start and limit.
       
   125         // The value is printed out in XML using many different tag names,
       
   126         // too hard to get it from XML
       
   127 
       
   128         // Additionally, the following tags should be always available
       
   129         // according to API documentation but... this is not the truth!
       
   130         // - querytime
       
   131         // - limit
       
   132         // - start (sometimes rankingstart)
       
   133 
       
   134         // query tag is only available for some requests, the same for url.
       
   135         // For now ignore them.
       
   136 
       
   137         //$start = isset($options['start']) ? $options['start'] : 1;
       
   138         //$limit = isset($options['limit']) ? $options['limit'] : 20;
       
   139         //$this->_firstResultPosition = $start;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Initializes this object from a DomDocument response.
       
   144      *
       
   145      * Because __construct and __wakeup shares some common executions,
       
   146      * it's useful to group them in a single initialization method.
       
   147      * This method is called once each time a new instance is created
       
   148      * or a serialized object is unserialized.
       
   149      *
       
   150      * @param   DomDocument $dom the ReST fragment for this object
       
   151      * @param   array $options   query options as associative array
       
   152      *      * @return  void
       
   153      */
       
   154     protected function _init(DomDocument $dom, $options = array())
       
   155     {
       
   156         $this->_dom     = $dom;
       
   157         $this->_xpath   = new DOMXPath($dom);
       
   158 
       
   159         $this->_results = $this->_xpath->query("//item");
       
   160     }
       
   161 
       
   162     /**
       
   163      * Number of results returned.
       
   164      *
       
   165      * @return  int     total number of results returned
       
   166      */
       
   167     public function totalResults()
       
   168     {
       
   169         return (int) $this->_totalResultsReturned;
       
   170     }
       
   171 
       
   172 
       
   173     /**
       
   174      * Number of available results.
       
   175      *
       
   176      * @return  int     total number of available results
       
   177      */
       
   178     public function totalResultsAvailable()
       
   179     {
       
   180         return (int) $this->_totalResultsAvailable;
       
   181     }
       
   182 
       
   183     /**
       
   184      * Implements SeekableIterator::current().
       
   185      *
       
   186      * @return  void
       
   187      * @throws  Zend_Service_Exception
       
   188      * @abstract
       
   189      */
       
   190     // abstract public function current();
       
   191 
       
   192     /**
       
   193      * Implements SeekableIterator::key().
       
   194      *
       
   195      * @return  int
       
   196      */
       
   197     public function key()
       
   198     {
       
   199         return $this->_currentIndex;
       
   200     }
       
   201 
       
   202     /**
       
   203      * Implements SeekableIterator::next().
       
   204      *
       
   205      * @return  void
       
   206      */
       
   207     public function next()
       
   208     {
       
   209         $this->_currentIndex += 1;
       
   210     }
       
   211 
       
   212     /**
       
   213      * Implements SeekableIterator::rewind().
       
   214      *
       
   215      * @return  bool
       
   216      */
       
   217     public function rewind()
       
   218     {
       
   219         $this->_currentIndex = 0;
       
   220         return true;
       
   221     }
       
   222 
       
   223     /**
       
   224      * Implement SeekableIterator::seek().
       
   225      *
       
   226      * @param   int $index
       
   227      * @return  void
       
   228      * @throws  OutOfBoundsException
       
   229      */
       
   230     public function seek($index)
       
   231     {
       
   232         $indexInt = (int) $index;
       
   233         if ($indexInt >= 0 && $indexInt < $this->_results->length) {
       
   234             $this->_currentIndex = $indexInt;
       
   235         } else {
       
   236             throw new OutOfBoundsException("Illegal index '$index'");
       
   237         }
       
   238     }
       
   239 
       
   240     /**
       
   241      * Implement SeekableIterator::valid().
       
   242      *
       
   243      * @return boolean
       
   244      */
       
   245     public function valid()
       
   246     {
       
   247         return null !== $this->_results && $this->_currentIndex < $this->_results->length;
       
   248     }
       
   249 
       
   250     /**
       
   251      * Returns the response document as XML string.
       
   252      *
       
   253      * @return string   the response document converted into XML format
       
   254      */
       
   255     public function getXml()
       
   256     {
       
   257         return $this->_dom->saveXML();
       
   258     }
       
   259 
       
   260     /**
       
   261      * Overwrites standard __sleep method to make this object serializable.
       
   262      *
       
   263      * DomDocument and DOMXpath objects cannot be serialized.
       
   264      * This method converts them back to an XML string.
       
   265      *
       
   266      * @return void
       
   267      */
       
   268     public function __sleep() {
       
   269         $this->_xml     = $this->getXml();
       
   270         $vars = array_keys(get_object_vars($this));
       
   271         return array_diff($vars, array('_dom', '_xpath'));
       
   272     }
       
   273 
       
   274     /**
       
   275      * Overwrites standard __wakeup method to make this object unserializable.
       
   276      *
       
   277      * Restores object status before serialization.
       
   278      * Converts XML string into a DomDocument object and creates a valid
       
   279      * DOMXpath instance for given DocDocument.
       
   280      *
       
   281      * @return void
       
   282      */
       
   283     public function __wakeup() {
       
   284         $dom = new DOMDocument();
       
   285         $dom->loadXml($this->_xml);
       
   286         $this->_init($dom);
       
   287         $this->_xml = null; // reset XML content
       
   288     }
       
   289 }