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