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\Extension;
|
Chris@0
|
11
|
Chris@0
|
12 use DOMDocument;
|
Chris@0
|
13 use DOMXPath;
|
Chris@0
|
14 use Zend\Feed\Reader;
|
Chris@0
|
15
|
Chris@0
|
16 abstract class AbstractFeed
|
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 * The base XPath query used to retrieve feed data
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var DOMXPath
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $xpath = null;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The XPath prefix
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $xpathPrefix = '';
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Set the DOM document
|
Chris@0
|
48 *
|
Chris@0
|
49 * @param DOMDocument $dom
|
Chris@0
|
50 * @return AbstractFeed
|
Chris@0
|
51 */
|
Chris@0
|
52 public function setDomDocument(DOMDocument $dom)
|
Chris@0
|
53 {
|
Chris@0
|
54 $this->domDocument = $dom;
|
Chris@0
|
55 return $this;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Get the DOM
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return DOMDocument
|
Chris@0
|
62 */
|
Chris@0
|
63 public function getDomDocument()
|
Chris@0
|
64 {
|
Chris@0
|
65 return $this->domDocument;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Get the Feed's encoding
|
Chris@0
|
70 *
|
Chris@0
|
71 * @return string
|
Chris@0
|
72 */
|
Chris@0
|
73 public function getEncoding()
|
Chris@0
|
74 {
|
Chris@0
|
75 $assumed = $this->getDomDocument()->encoding;
|
Chris@0
|
76 return $assumed;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Set the feed type
|
Chris@0
|
81 *
|
Chris@0
|
82 * @param string $type
|
Chris@0
|
83 * @return AbstractFeed
|
Chris@0
|
84 */
|
Chris@0
|
85 public function setType($type)
|
Chris@0
|
86 {
|
Chris@0
|
87 $this->data['type'] = $type;
|
Chris@0
|
88 return $this;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Get the feed type
|
Chris@0
|
93 *
|
Chris@0
|
94 * If null, it will attempt to autodetect the type.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return string
|
Chris@0
|
97 */
|
Chris@0
|
98 public function getType()
|
Chris@0
|
99 {
|
Chris@0
|
100 $type = $this->data['type'];
|
Chris@0
|
101 if (null === $type) {
|
Chris@0
|
102 $type = Reader\Reader::detectType($this->getDomDocument());
|
Chris@0
|
103 $this->setType($type);
|
Chris@0
|
104 }
|
Chris@0
|
105 return $type;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Return the feed as an array
|
Chris@0
|
110 *
|
Chris@0
|
111 * @return array
|
Chris@0
|
112 */
|
Chris@0
|
113 public function toArray() // untested
|
Chris@0
|
114 {
|
Chris@0
|
115 return $this->data;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Set the XPath query
|
Chris@0
|
120 *
|
Chris@0
|
121 * @param DOMXPath $xpath
|
Chris@17
|
122 * @return AbstractFeed
|
Chris@0
|
123 */
|
Chris@0
|
124 public function setXpath(DOMXPath $xpath = null)
|
Chris@0
|
125 {
|
Chris@0
|
126 if (null === $xpath) {
|
Chris@0
|
127 $this->xpath = null;
|
Chris@0
|
128 return $this;
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 $this->xpath = $xpath;
|
Chris@0
|
132 $this->registerNamespaces();
|
Chris@0
|
133 return $this;
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Get the DOMXPath object
|
Chris@0
|
138 *
|
Chris@0
|
139 * @return string
|
Chris@0
|
140 */
|
Chris@0
|
141 public function getXpath()
|
Chris@0
|
142 {
|
Chris@0
|
143 if (null === $this->xpath) {
|
Chris@0
|
144 $this->setXpath(new DOMXPath($this->getDomDocument()));
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 return $this->xpath;
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * Get the XPath prefix
|
Chris@0
|
152 *
|
Chris@0
|
153 * @return string
|
Chris@0
|
154 */
|
Chris@0
|
155 public function getXpathPrefix()
|
Chris@0
|
156 {
|
Chris@0
|
157 return $this->xpathPrefix;
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * Set the XPath prefix
|
Chris@0
|
162 *
|
Chris@0
|
163 * @param string $prefix
|
Chris@0
|
164 * @return void
|
Chris@0
|
165 */
|
Chris@0
|
166 public function setXpathPrefix($prefix)
|
Chris@0
|
167 {
|
Chris@0
|
168 $this->xpathPrefix = $prefix;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * Register the default namespaces for the current feed format
|
Chris@0
|
173 */
|
Chris@0
|
174 abstract protected function registerNamespaces();
|
Chris@0
|
175 }
|