EntryAbstract.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Reader
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Feed_Reader
  24. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. abstract class Zend_Feed_Reader_Extension_EntryAbstract
  28. {
  29. /**
  30. * Feed entry data
  31. *
  32. * @var array
  33. */
  34. protected $_data = array();
  35. /**
  36. * DOM document object
  37. *
  38. * @var DOMDocument
  39. */
  40. protected $_domDocument = null;
  41. /**
  42. * Entry instance
  43. *
  44. * @var Zend_Feed_Entry_Abstract
  45. */
  46. protected $_entry = null;
  47. /**
  48. * Pointer to the current entry
  49. *
  50. * @var int
  51. */
  52. protected $_entryKey = 0;
  53. /**
  54. * XPath object
  55. *
  56. * @var DOMXPath
  57. */
  58. protected $_xpath = null;
  59. /**
  60. * XPath query
  61. *
  62. * @var string
  63. */
  64. protected $_xpathPrefix = '';
  65. /**
  66. * Constructor
  67. *
  68. * @param Zend_Feed_Entry_Abstract $entry
  69. * @param int $entryKey
  70. * @param string $type
  71. * @return void
  72. */
  73. public function __construct(DOMElement $entry, $entryKey, $type = null)
  74. {
  75. $this->_entry = $entry;
  76. $this->_entryKey = $entryKey;
  77. $this->_domDocument = $entry->ownerDocument;
  78. if ($type !== null) {
  79. $this->_data['type'] = $type;
  80. } else {
  81. $this->_data['type'] = Zend_Feed_Reader::detectType($entry->ownerDocument, true);
  82. }
  83. // set the XPath query prefix for the entry being queried
  84. if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_10
  85. || $this->getType() == Zend_Feed_Reader::TYPE_RSS_090
  86. ) {
  87. $this->setXpathPrefix('//rss:item[' . ($this->_entryKey+1) . ']');
  88. } elseif ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10
  89. || $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03
  90. ) {
  91. $this->setXpathPrefix('//atom:entry[' . ($this->_entryKey+1) . ']');
  92. } else {
  93. $this->setXpathPrefix('//item[' . ($this->_entryKey+1) . ']');
  94. }
  95. }
  96. /**
  97. * Get the DOM
  98. *
  99. * @return DOMDocument
  100. */
  101. public function getDomDocument()
  102. {
  103. return $this->_domDocument;
  104. }
  105. /**
  106. * Get the Entry's encoding
  107. *
  108. * @return string
  109. */
  110. public function getEncoding()
  111. {
  112. $assumed = $this->getDomDocument()->encoding;
  113. return $assumed;
  114. }
  115. /**
  116. * Get the entry type
  117. *
  118. * @return string
  119. */
  120. public function getType()
  121. {
  122. return $this->_data['type'];
  123. }
  124. /**
  125. * Set the XPath query
  126. *
  127. * @param DOMXPath $xpath
  128. * @return Zend_Feed_Reader_Extension_EntryAbstract
  129. */
  130. public function setXpath(DOMXPath $xpath)
  131. {
  132. $this->_xpath = $xpath;
  133. $this->_registerNamespaces();
  134. return $this;
  135. }
  136. /**
  137. * Get the XPath query object
  138. *
  139. * @return DOMXPath
  140. */
  141. public function getXpath()
  142. {
  143. if (!$this->_xpath) {
  144. $this->setXpath(new DOMXPath($this->getDomDocument()));
  145. }
  146. return $this->_xpath;
  147. }
  148. /**
  149. * Serialize the entry to an array
  150. *
  151. * @return array
  152. */
  153. public function toArray()
  154. {
  155. return $this->_data;
  156. }
  157. /**
  158. * Get the XPath prefix
  159. *
  160. * @return string
  161. */
  162. public function getXpathPrefix()
  163. {
  164. return $this->_xpathPrefix;
  165. }
  166. /**
  167. * Set the XPath prefix
  168. *
  169. * @param string $prefix
  170. * @return Zend_Feed_Reader_Extension_EntryAbstract
  171. */
  172. public function setXpathPrefix($prefix)
  173. {
  174. $this->_xpathPrefix = $prefix;
  175. return $this;
  176. }
  177. /**
  178. * Register XML namespaces
  179. *
  180. * @return void
  181. */
  182. protected abstract function _registerNamespaces();
  183. }