RendererAbstract.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_Writer
  17. * @copyright Copyright (c) 2005-2009 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. require_once 'Zend/Feed/Writer.php';
  22. require_once 'Zend/Version.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Feed_Writer
  26. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Feed_Writer_Renderer_RendererAbstract
  30. {
  31. protected $_extensions = array();
  32. protected $_container = null;
  33. protected $_dom = null;
  34. protected $_ignoreExceptions = false;
  35. protected $_exceptions = array();
  36. /**
  37. * Encoding of all text values
  38. *
  39. * @var string
  40. */
  41. protected $_encoding = 'UTF-8';
  42. /**
  43. * Holds the value "atom" or "rss" depending on the feed type set when
  44. * when last exported.
  45. *
  46. * @var string
  47. */
  48. protected $_type = null;
  49. protected $_rootElement = null;
  50. public function __construct($container)
  51. {
  52. $this->_container = $container;
  53. $this->setType($container->getType());
  54. $this->_loadExtensions();
  55. }
  56. public function saveXml()
  57. {
  58. return $this->getDomDocument()->saveXml();
  59. }
  60. public function getDomDocument()
  61. {
  62. return $this->_dom;
  63. }
  64. public function getElement()
  65. {
  66. return $this->getDomDocument()->documentElement;
  67. }
  68. public function getDataContainer()
  69. {
  70. return $this->_container;
  71. }
  72. public function setEncoding($enc)
  73. {
  74. $this->_encoding = $enc;
  75. }
  76. public function getEncoding()
  77. {
  78. return $this->_encoding;
  79. }
  80. public function ignoreExceptions($bool = true)
  81. {
  82. if (!is_bool($bool)) {
  83. require_once 'Zend/Feed/Exception.php';
  84. throw new Zend_Feed_Exception('Invalid parameter: $bool. Should be TRUE or FALSE (defaults to TRUE if null)');
  85. }
  86. $this->_ignoreExceptions = $bool;
  87. }
  88. public function getExceptions()
  89. {
  90. return $this->_exceptions;
  91. }
  92. /**
  93. * Set the current feed type being exported to "rss" or "atom". This allows
  94. * other objects to gracefully choose whether to execute or not, depending
  95. * on their appropriateness for the current type, e.g. renderers.
  96. *
  97. * @param string $type
  98. */
  99. public function setType($type)
  100. {
  101. $this->_type = $type;
  102. }
  103. /**
  104. * Retrieve the current or last feed type exported.
  105. *
  106. * @return string Value will be "rss" or "atom"
  107. */
  108. public function getType()
  109. {
  110. return $this->_type;
  111. }
  112. /**
  113. * Sets the absolute root element for the XML feed being generated. This
  114. * helps simplify the appending of namespace declarations, but also ensures
  115. * namespaces are added to the root element - not scattered across the entire
  116. * XML file - may assist namespace unsafe parsers and looks pretty ;).
  117. *
  118. * @param DOMElement $root
  119. */
  120. public function setRootElement(DOMElement $root)
  121. {
  122. $this->_rootElement = $root;
  123. }
  124. /**
  125. * Retrieve the absolute root element for the XML feed being generated.
  126. *
  127. * @return DOMElement
  128. */
  129. public function getRootElement()
  130. {
  131. return $this->_rootElement;
  132. }
  133. /**
  134. * Load extensions from Zend_Feed_Writer
  135. *
  136. * @return void
  137. */
  138. protected function _loadExtensions()
  139. {
  140. Zend_Feed_Writer::registerCoreExtensions();
  141. $all = Zend_Feed_Writer::getExtensions();
  142. if (stripos(get_class($this), 'entry')) {
  143. $exts = $all['entryRenderer'];
  144. } else {
  145. $exts = $all['feedRenderer'];
  146. }
  147. foreach ($exts as $extension) {
  148. $className = Zend_Feed_Writer::getPluginLoader()->getClassName($extension);
  149. $this->_extensions[$extension] = new $className(
  150. $this->getDataContainer()
  151. );
  152. $this->_extensions[$extension]->setEncoding($this->getEncoding());
  153. }
  154. }
  155. }