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 DOMElement;
|
Chris@0
|
14 use DOMXPath;
|
Chris@0
|
15 use Zend\Feed\Reader;
|
Chris@0
|
16
|
Chris@0
|
17 abstract class AbstractEntry
|
Chris@0
|
18 {
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Feed entry data
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $data = [];
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * DOM document object
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var DOMDocument
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $domDocument = null;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Entry instance
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var DOMElement
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $entry = null;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Pointer to the current entry
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var int
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $entryKey = 0;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * XPath object
|
Chris@0
|
49 *
|
Chris@0
|
50 * @var DOMXPath
|
Chris@0
|
51 */
|
Chris@0
|
52 protected $xpath = null;
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * XPath query
|
Chris@0
|
56 *
|
Chris@0
|
57 * @var string
|
Chris@0
|
58 */
|
Chris@0
|
59 protected $xpathPrefix = '';
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Set the entry DOMElement
|
Chris@0
|
63 *
|
Chris@0
|
64 * Has side effect of setting the DOMDocument for the entry.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @param DOMElement $entry
|
Chris@0
|
67 * @return AbstractEntry
|
Chris@0
|
68 */
|
Chris@0
|
69 public function setEntryElement(DOMElement $entry)
|
Chris@0
|
70 {
|
Chris@0
|
71 $this->entry = $entry;
|
Chris@0
|
72 $this->domDocument = $entry->ownerDocument;
|
Chris@0
|
73 return $this;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Get the entry DOMElement
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return DOMElement
|
Chris@0
|
80 */
|
Chris@0
|
81 public function getEntryElement()
|
Chris@0
|
82 {
|
Chris@0
|
83 return $this->entry;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Set the entry key
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param string $entryKey
|
Chris@0
|
90 * @return AbstractEntry
|
Chris@0
|
91 */
|
Chris@0
|
92 public function setEntryKey($entryKey)
|
Chris@0
|
93 {
|
Chris@0
|
94 $this->entryKey = $entryKey;
|
Chris@0
|
95 return $this;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Get the DOM
|
Chris@0
|
100 *
|
Chris@0
|
101 * @return DOMDocument
|
Chris@0
|
102 */
|
Chris@0
|
103 public function getDomDocument()
|
Chris@0
|
104 {
|
Chris@0
|
105 return $this->domDocument;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Get the Entry's encoding
|
Chris@0
|
110 *
|
Chris@0
|
111 * @return string
|
Chris@0
|
112 */
|
Chris@0
|
113 public function getEncoding()
|
Chris@0
|
114 {
|
Chris@0
|
115 $assumed = $this->getDomDocument()->encoding;
|
Chris@0
|
116 return $assumed;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Set the entry type
|
Chris@0
|
121 *
|
Chris@0
|
122 * Has side effect of setting xpath prefix
|
Chris@0
|
123 *
|
Chris@0
|
124 * @param string $type
|
Chris@0
|
125 * @return AbstractEntry
|
Chris@0
|
126 */
|
Chris@0
|
127 public function setType($type)
|
Chris@0
|
128 {
|
Chris@0
|
129 if (null === $type) {
|
Chris@0
|
130 $this->data['type'] = null;
|
Chris@0
|
131 return $this;
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 $this->data['type'] = $type;
|
Chris@0
|
135 if ($type === Reader\Reader::TYPE_RSS_10
|
Chris@0
|
136 || $type === Reader\Reader::TYPE_RSS_090
|
Chris@0
|
137 ) {
|
Chris@12
|
138 $this->setXpathPrefix('//rss:item[' . ((int)$this->entryKey + 1) . ']');
|
Chris@0
|
139 return $this;
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 if ($type === Reader\Reader::TYPE_ATOM_10
|
Chris@0
|
143 || $type === Reader\Reader::TYPE_ATOM_03
|
Chris@0
|
144 ) {
|
Chris@12
|
145 $this->setXpathPrefix('//atom:entry[' . ((int)$this->entryKey + 1) . ']');
|
Chris@0
|
146 return $this;
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@12
|
149 $this->setXpathPrefix('//item[' . ((int)$this->entryKey + 1) . ']');
|
Chris@0
|
150 return $this;
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * Get the entry type
|
Chris@0
|
155 *
|
Chris@0
|
156 * @return string
|
Chris@0
|
157 */
|
Chris@0
|
158 public function getType()
|
Chris@0
|
159 {
|
Chris@0
|
160 $type = $this->data['type'];
|
Chris@0
|
161 if ($type === null) {
|
Chris@0
|
162 $type = Reader\Reader::detectType($this->getEntryElement(), true);
|
Chris@0
|
163 $this->setType($type);
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 return $type;
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * Set the XPath query
|
Chris@0
|
171 *
|
Chris@0
|
172 * @param DOMXPath $xpath
|
Chris@0
|
173 * @return AbstractEntry
|
Chris@0
|
174 */
|
Chris@0
|
175 public function setXpath(DOMXPath $xpath)
|
Chris@0
|
176 {
|
Chris@0
|
177 $this->xpath = $xpath;
|
Chris@0
|
178 $this->registerNamespaces();
|
Chris@0
|
179 return $this;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 /**
|
Chris@0
|
183 * Get the XPath query object
|
Chris@0
|
184 *
|
Chris@0
|
185 * @return DOMXPath
|
Chris@0
|
186 */
|
Chris@0
|
187 public function getXpath()
|
Chris@0
|
188 {
|
Chris@12
|
189 if (! $this->xpath) {
|
Chris@0
|
190 $this->setXpath(new DOMXPath($this->getDomDocument()));
|
Chris@0
|
191 }
|
Chris@0
|
192 return $this->xpath;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Serialize the entry to an array
|
Chris@0
|
197 *
|
Chris@0
|
198 * @return array
|
Chris@0
|
199 */
|
Chris@0
|
200 public function toArray()
|
Chris@0
|
201 {
|
Chris@0
|
202 return $this->data;
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * Get the XPath prefix
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return string
|
Chris@0
|
209 */
|
Chris@0
|
210 public function getXpathPrefix()
|
Chris@0
|
211 {
|
Chris@0
|
212 return $this->xpathPrefix;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * Set the XPath prefix
|
Chris@0
|
217 *
|
Chris@0
|
218 * @param string $prefix
|
Chris@0
|
219 * @return AbstractEntry
|
Chris@0
|
220 */
|
Chris@0
|
221 public function setXpathPrefix($prefix)
|
Chris@0
|
222 {
|
Chris@0
|
223 $this->xpathPrefix = $prefix;
|
Chris@0
|
224 return $this;
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Register XML namespaces
|
Chris@0
|
229 *
|
Chris@0
|
230 * @return void
|
Chris@0
|
231 */
|
Chris@0
|
232 abstract protected function registerNamespaces();
|
Chris@0
|
233 }
|