~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Service/Technorati/ResultSet.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2008 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 8064 2008-02-16 10:58:39Z thomas $
 
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-2008 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
     * @todo
 
68
     */
 
69
    // public $firstResultPosition;
 
70
 
 
71
 
 
72
    /**
 
73
     * A DomNodeList of results
 
74
     *
 
75
     * @var     DomNodeList
 
76
     * @access  protected
 
77
     */
 
78
    protected $_results;
 
79
 
 
80
    /**
 
81
     * Technorati API response document
 
82
     *
 
83
     * @var     DomDocument
 
84
     * @access  protected
 
85
     */
 
86
    protected $_dom;
 
87
 
 
88
    /**
 
89
     * Object for $this->_dom
 
90
     *
 
91
     * @var     DOMXpath
 
92
     * @access  protected
 
93
     */
 
94
    protected $_xpath;
 
95
 
 
96
    /**
 
97
     * XML string representation for $this->_dom
 
98
     *
 
99
     * @var     string
 
100
     * @access  protected
 
101
     */
 
102
    protected $_xml;
 
103
 
 
104
    /**
 
105
     * Current Item
 
106
     *
 
107
     * @var     int
 
108
     * @access  protected
 
109
     */
 
110
    protected $_currentIndex = 0;
 
111
 
 
112
 
 
113
    /**
 
114
     * Parses the search response and retrieves the results for iteration.
 
115
     *
 
116
     * @param   DomDocument $dom    the ReST fragment for this object
 
117
     * @param   array $options      query options as associative array
 
118
     */
 
119
    public function __construct(DomDocument $dom, $options = array())
 
120
    {
 
121
        $this->_init($dom, $options);
 
122
 
 
123
        // Technorati loves to make developer's life really hard
 
124
        // I must read query options in order to normalize a single way
 
125
        // to display start and limit.
 
126
        // The value is printed out in XML using many different tag names,
 
127
        // too hard to get it from XML
 
128
 
 
129
        // Additionally, the following tags should be always available
 
130
        // according to API documentation but... this is not the truth!
 
131
        // - querytime
 
132
        // - limit
 
133
        // - start (sometimes rankingstart)
 
134
 
 
135
        // query tag is only available for some requests, the same for url.
 
136
        // For now ignore them.
 
137
 
 
138
        //$start = isset($options['start']) ? $options['start'] : 1;
 
139
        //$limit = isset($options['limit']) ? $options['limit'] : 20;
 
140
        //$this->_firstResultPosition = $start;
 
141
    }
 
142
 
 
143
    /**
 
144
     * Initializes this object from a DomDocument response.
 
145
     *
 
146
     * Because __construct and __wakeup shares some common executions,
 
147
     * it's useful to group them in a single initialization method.
 
148
     * This method is called once each time a new instance is created
 
149
     * or a serialized object is unserialized.
 
150
     *
 
151
     * @param   DomDocument $dom    the ReST fragment for this object
 
152
     * @param   array $options      query options as associative array
 
153
     *      * @return  void
 
154
     */
 
155
    protected function _init(DomDocument $dom, $options = array())
 
156
    {
 
157
        $this->_dom     = $dom;
 
158
        $this->_xpath   = new DOMXPath($dom);
 
159
 
 
160
        $this->_results = $this->_xpath->query("//item");
 
161
    }
 
162
 
 
163
    /**
 
164
     * Number of results returned.
 
165
     *
 
166
     * @return  int     total number of results returned
 
167
     */
 
168
    public function totalResults()
 
169
    {
 
170
        return (int) $this->_totalResultsReturned;
 
171
    }
 
172
 
 
173
 
 
174
    /**
 
175
     * Number of available results.
 
176
     *
 
177
     * @return  int     total number of available results
 
178
     */
 
179
    public function totalResultsAvailable()
 
180
    {
 
181
        return (int) $this->_totalResultsAvailable;
 
182
    }
 
183
 
 
184
    /**
 
185
     * Implements SeekableIterator::current().
 
186
     *
 
187
     * @return  void
 
188
     * @throws  Zend_Service_Exception
 
189
     * @abstract
 
190
     */
 
191
    // abstract public function current();
 
192
 
 
193
    /**
 
194
     * Implements SeekableIterator::key().
 
195
     *
 
196
     * @return  int
 
197
     */
 
198
    public function key()
 
199
    {
 
200
        return $this->_currentIndex;
 
201
    }
 
202
 
 
203
    /**
 
204
     * Implements SeekableIterator::next().
 
205
     *
 
206
     * @return  void
 
207
     */
 
208
    public function next()
 
209
    {
 
210
        $this->_currentIndex += 1;
 
211
    }
 
212
 
 
213
    /**
 
214
     * Implements SeekableIterator::rewind().
 
215
     *
 
216
     * @return  bool
 
217
     */
 
218
    public function rewind()
 
219
    {
 
220
        $this->_currentIndex = 0;
 
221
        return true;
 
222
    }
 
223
 
 
224
    /**
 
225
     * Implement SeekableIterator::seek().
 
226
     *
 
227
     * @param   int $index
 
228
     * @return  void
 
229
     * @throws  OutOfBoundsException
 
230
     */
 
231
    public function seek($index)
 
232
    {
 
233
        $indexInt = (int) $index;
 
234
        if ($indexInt >= 0 && $indexInt < $this->_results->length) {
 
235
            $this->_currentIndex = $indexInt;
 
236
        } else {
 
237
            throw new OutOfBoundsException("Illegal index '$index'");
 
238
        }
 
239
    }
 
240
 
 
241
    /**
 
242
     * Implement SeekableIterator::valid().
 
243
     *
 
244
     * @return boolean
 
245
     */
 
246
    public function valid()
 
247
    {
 
248
        return null !== $this->_results && $this->_currentIndex < $this->_results->length;
 
249
    }
 
250
 
 
251
    /**
 
252
     * Returns the response document as XML string.
 
253
     *
 
254
     * @return string   the response document converted into XML format
 
255
     */
 
256
    public function getXml()
 
257
    {
 
258
        return $this->_dom->saveXML();
 
259
    }
 
260
 
 
261
    /**
 
262
     * Overwrites standard __sleep method to make this object serializable.
 
263
     *
 
264
     * DomDocument and DOMXpath objects cannot be serialized.
 
265
     * This method converts them back to an XML string.
 
266
     *
 
267
     * @return void
 
268
     */
 
269
    public function __sleep() {
 
270
        $this->_xml     = $this->getXml();
 
271
        $vars = array_keys(get_object_vars($this));
 
272
        return array_diff($vars, array('_dom', '_xpath'));
 
273
    }
 
274
 
 
275
    /**
 
276
     * Overwrites standard __wakeup method to make this object unserializable.
 
277
     *
 
278
     * Restores object status before serialization.
 
279
     * Converts XML string into a DomDocument object and creates a valid
 
280
     * DOMXpath instance for given DocDocument.
 
281
     *
 
282
     * @return void
 
283
     */
 
284
    public function __wakeup() {
 
285
        $dom = new DOMDocument();
 
286
        $dom->loadXml($this->_xml);
 
287
        $this->_init($dom);
 
288
        $this->_xml = null; // reset XML content
 
289
    }
 
290
}