|
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_Feed_Reader |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: FeedAbstract.php 22092 2010-05-04 12:50:51Z padraic $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Feed_Reader |
|
24 */ |
|
25 require_once 'Zend/Feed/Reader.php'; |
|
26 |
|
27 /** |
|
28 * @see Zend_feed_Reader_FeedInterface |
|
29 */ |
|
30 require_once 'Zend/Feed/Reader/FeedInterface.php'; |
|
31 |
|
32 /** |
|
33 * @category Zend |
|
34 * @package Zend_Feed_Reader |
|
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 abstract class Zend_Feed_Reader_FeedAbstract implements Zend_Feed_Reader_FeedInterface |
|
39 { |
|
40 /** |
|
41 * Parsed feed data |
|
42 * |
|
43 * @var array |
|
44 */ |
|
45 protected $_data = array(); |
|
46 |
|
47 /** |
|
48 * Parsed feed data in the shape of a DOMDocument |
|
49 * |
|
50 * @var DOMDocument |
|
51 */ |
|
52 protected $_domDocument = null; |
|
53 |
|
54 /** |
|
55 * An array of parsed feed entries |
|
56 * |
|
57 * @var array |
|
58 */ |
|
59 protected $_entries = array(); |
|
60 |
|
61 /** |
|
62 * A pointer for the iterator to keep track of the entries array |
|
63 * |
|
64 * @var int |
|
65 */ |
|
66 protected $_entriesKey = 0; |
|
67 |
|
68 /** |
|
69 * The base XPath query used to retrieve feed data |
|
70 * |
|
71 * @var DOMXPath |
|
72 */ |
|
73 protected $_xpath = null; |
|
74 |
|
75 /** |
|
76 * Array of loaded extensions |
|
77 * |
|
78 * @var array |
|
79 */ |
|
80 protected $_extensions = array(); |
|
81 |
|
82 /** |
|
83 * Original Source URI (set if imported from a URI) |
|
84 * |
|
85 * @var string |
|
86 */ |
|
87 protected $_originalSourceUri = null; |
|
88 |
|
89 /** |
|
90 * Constructor |
|
91 * |
|
92 * @param DomDocument The DOM object for the feed's XML |
|
93 * @param string $type Feed type |
|
94 */ |
|
95 public function __construct(DomDocument $domDocument, $type = null) |
|
96 { |
|
97 $this->_domDocument = $domDocument; |
|
98 $this->_xpath = new DOMXPath($this->_domDocument); |
|
99 |
|
100 if ($type !== null) { |
|
101 $this->_data['type'] = $type; |
|
102 } else { |
|
103 $this->_data['type'] = Zend_Feed_Reader::detectType($this->_domDocument); |
|
104 } |
|
105 $this->_registerNamespaces(); |
|
106 $this->_indexEntries(); |
|
107 $this->_loadExtensions(); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Set an original source URI for the feed being parsed. This value |
|
112 * is returned from getFeedLink() method if the feed does not carry |
|
113 * a self-referencing URI. |
|
114 * |
|
115 * @param string $uri |
|
116 */ |
|
117 public function setOriginalSourceUri($uri) |
|
118 { |
|
119 $this->_originalSourceUri = $uri; |
|
120 } |
|
121 |
|
122 /** |
|
123 * Get an original source URI for the feed being parsed. Returns null if |
|
124 * unset or the feed was not imported from a URI. |
|
125 * |
|
126 * @return string|null |
|
127 */ |
|
128 public function getOriginalSourceUri() |
|
129 { |
|
130 return $this->_originalSourceUri; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Get the number of feed entries. |
|
135 * Required by the Iterator interface. |
|
136 * |
|
137 * @return int |
|
138 */ |
|
139 public function count() |
|
140 { |
|
141 return count($this->_entries); |
|
142 } |
|
143 |
|
144 /** |
|
145 * Return the current entry |
|
146 * |
|
147 * @return Zend_Feed_Reader_EntryInterface |
|
148 */ |
|
149 public function current() |
|
150 { |
|
151 if (substr($this->getType(), 0, 3) == 'rss') { |
|
152 $reader = new Zend_Feed_Reader_Entry_Rss($this->_entries[$this->key()], $this->key(), $this->getType()); |
|
153 } else { |
|
154 $reader = new Zend_Feed_Reader_Entry_Atom($this->_entries[$this->key()], $this->key(), $this->getType()); |
|
155 } |
|
156 |
|
157 $reader->setXpath($this->_xpath); |
|
158 |
|
159 return $reader; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Get the DOM |
|
164 * |
|
165 * @return DOMDocument |
|
166 */ |
|
167 public function getDomDocument() |
|
168 { |
|
169 return $this->_domDocument; |
|
170 } |
|
171 |
|
172 /** |
|
173 * Get the Feed's encoding |
|
174 * |
|
175 * @return string |
|
176 */ |
|
177 public function getEncoding() |
|
178 { |
|
179 $assumed = $this->getDomDocument()->encoding; |
|
180 if (empty($assumed)) { |
|
181 $assumed = 'UTF-8'; |
|
182 } |
|
183 return $assumed; |
|
184 } |
|
185 |
|
186 /** |
|
187 * Get feed as xml |
|
188 * |
|
189 * @return string |
|
190 */ |
|
191 public function saveXml() |
|
192 { |
|
193 return $this->getDomDocument()->saveXml(); |
|
194 } |
|
195 |
|
196 /** |
|
197 * Get the DOMElement representing the items/feed element |
|
198 * |
|
199 * @return DOMElement |
|
200 */ |
|
201 public function getElement() |
|
202 { |
|
203 return $this->getDomDocument()->documentElement; |
|
204 } |
|
205 |
|
206 /** |
|
207 * Get the DOMXPath object for this feed |
|
208 * |
|
209 * @return DOMXPath |
|
210 */ |
|
211 public function getXpath() |
|
212 { |
|
213 return $this->_xpath; |
|
214 } |
|
215 |
|
216 /** |
|
217 * Get the feed type |
|
218 * |
|
219 * @return string |
|
220 */ |
|
221 public function getType() |
|
222 { |
|
223 return $this->_data['type']; |
|
224 } |
|
225 |
|
226 /** |
|
227 * Return the current feed key |
|
228 * |
|
229 * @return unknown |
|
230 */ |
|
231 public function key() |
|
232 { |
|
233 return $this->_entriesKey; |
|
234 } |
|
235 |
|
236 /** |
|
237 * Move the feed pointer forward |
|
238 * |
|
239 */ |
|
240 public function next() |
|
241 { |
|
242 ++$this->_entriesKey; |
|
243 } |
|
244 |
|
245 /** |
|
246 * Reset the pointer in the feed object |
|
247 * |
|
248 */ |
|
249 public function rewind() |
|
250 { |
|
251 $this->_entriesKey = 0; |
|
252 } |
|
253 |
|
254 /** |
|
255 * Check to see if the iterator is still valid |
|
256 * |
|
257 * @return boolean |
|
258 */ |
|
259 public function valid() |
|
260 { |
|
261 return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count(); |
|
262 } |
|
263 |
|
264 public function getExtensions() |
|
265 { |
|
266 return $this->_extensions; |
|
267 } |
|
268 |
|
269 public function __call($method, $args) |
|
270 { |
|
271 foreach ($this->_extensions as $extension) { |
|
272 if (method_exists($extension, $method)) { |
|
273 return call_user_func_array(array($extension, $method), $args); |
|
274 } |
|
275 } |
|
276 require_once 'Zend/Feed/Exception.php'; |
|
277 throw new Zend_Feed_Exception('Method: ' . $method |
|
278 . 'does not exist and could not be located on a registered Extension'); |
|
279 } |
|
280 |
|
281 /** |
|
282 * Return an Extension object with the matching name (postfixed with _Feed) |
|
283 * |
|
284 * @param string $name |
|
285 * @return Zend_Feed_Reader_Extension_FeedAbstract |
|
286 */ |
|
287 public function getExtension($name) |
|
288 { |
|
289 if (array_key_exists($name . '_Feed', $this->_extensions)) { |
|
290 return $this->_extensions[$name . '_Feed']; |
|
291 } |
|
292 return null; |
|
293 } |
|
294 |
|
295 protected function _loadExtensions() |
|
296 { |
|
297 $all = Zend_Feed_Reader::getExtensions(); |
|
298 $feed = $all['feed']; |
|
299 foreach ($feed as $extension) { |
|
300 if (in_array($extension, $all['core'])) { |
|
301 continue; |
|
302 } |
|
303 $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension); |
|
304 $this->_extensions[$extension] = new $className( |
|
305 $this->getDomDocument(), $this->_data['type'], $this->_xpath |
|
306 ); |
|
307 } |
|
308 } |
|
309 |
|
310 /** |
|
311 * Read all entries to the internal entries array |
|
312 * |
|
313 */ |
|
314 abstract protected function _indexEntries(); |
|
315 |
|
316 /** |
|
317 * Register the default namespaces for the current feed format |
|
318 * |
|
319 */ |
|
320 abstract protected function _registerNamespaces(); |
|
321 } |