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