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