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