|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00027 require_once 'Zend/Service/Technorati/Result.php'; 00028 00029 00045 abstract class Zend_Service_Technorati_ResultSet implements SeekableIterator 00046 { 00053 protected $_totalResultsAvailable; 00054 00061 protected $_totalResultsReturned; 00062 00068 //TODO public $firstResultPosition; 00069 00070 00077 protected $_results; 00078 00085 protected $_dom; 00086 00093 protected $_xpath; 00094 00101 protected $_xml; 00102 00109 protected $_currentIndex = 0; 00110 00111 00118 public function __construct(DomDocument $dom, $options = array()) 00119 { 00120 $this->_init($dom, $options); 00121 00122 // Technorati loves to make developer's life really hard 00123 // I must read query options in order to normalize a single way 00124 // to display start and limit. 00125 // The value is printed out in XML using many different tag names, 00126 // too hard to get it from XML 00127 00128 // Additionally, the following tags should be always available 00129 // according to API documentation but... this is not the truth! 00130 // - querytime 00131 // - limit 00132 // - start (sometimes rankingstart) 00133 00134 // query tag is only available for some requests, the same for url. 00135 // For now ignore them. 00136 00137 //$start = isset($options['start']) ? $options['start'] : 1; 00138 //$limit = isset($options['limit']) ? $options['limit'] : 20; 00139 //$this->_firstResultPosition = $start; 00140 } 00141 00154 protected function _init(DomDocument $dom, $options = array()) 00155 { 00156 $this->_dom = $dom; 00157 $this->_xpath = new DOMXPath($dom); 00158 00159 $this->_results = $this->_xpath->query("//item"); 00160 } 00161 00167 public function totalResults() 00168 { 00169 return (int) $this->_totalResultsReturned; 00170 } 00171 00172 00178 public function totalResultsAvailable() 00179 { 00180 return (int) $this->_totalResultsAvailable; 00181 } 00182 00190 // abstract public function current(); 00191 00197 public function key() 00198 { 00199 return $this->_currentIndex; 00200 } 00201 00207 public function next() 00208 { 00209 $this->_currentIndex += 1; 00210 } 00211 00217 public function rewind() 00218 { 00219 $this->_currentIndex = 0; 00220 return true; 00221 } 00222 00230 public function seek($index) 00231 { 00232 $indexInt = (int) $index; 00233 if ($indexInt >= 0 && $indexInt < $this->_results->length) { 00234 $this->_currentIndex = $indexInt; 00235 } else { 00236 throw new OutOfBoundsException("Illegal index '$index'"); 00237 } 00238 } 00239 00245 public function valid() 00246 { 00247 return null !== $this->_results && $this->_currentIndex < $this->_results->length; 00248 } 00249 00255 public function getXml() 00256 { 00257 return $this->_dom->saveXML(); 00258 } 00259 00268 public function __sleep() { 00269 $this->_xml = $this->getXml(); 00270 $vars = array_keys(get_object_vars($this)); 00271 return array_diff($vars, array('_dom', '_xpath')); 00272 } 00273 00283 public function __wakeup() { 00284 $dom = new DOMDocument(); 00285 $dom->loadXml($this->_xml); 00286 $this->_init($dom); 00287 $this->_xml = null; // reset XML content 00288 } 00289 }