annotate vendor/zendframework/zend-feed/src/Writer/Renderer/AbstractRenderer.php @ 3:e11175134f4e

Attempt to introduce editable version of theme
author Chris Cannam
date Tue, 05 Dec 2017 11:25:38 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
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\Writer\Renderer;
Chris@0 11
Chris@0 12 use DOMDocument;
Chris@0 13 use DOMElement;
Chris@0 14 use Zend\Feed\Writer;
Chris@0 15
Chris@0 16 /**
Chris@0 17 */
Chris@0 18 class AbstractRenderer
Chris@0 19 {
Chris@0 20 /**
Chris@0 21 * Extensions
Chris@0 22 * @var array
Chris@0 23 */
Chris@0 24 protected $extensions = [];
Chris@0 25
Chris@0 26 /**
Chris@0 27 * @var Writer\AbstractFeed
Chris@0 28 */
Chris@0 29 protected $container = null;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * @var DOMDocument
Chris@0 33 */
Chris@0 34 protected $dom = null;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * @var bool
Chris@0 38 */
Chris@0 39 protected $ignoreExceptions = false;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * @var array
Chris@0 43 */
Chris@0 44 protected $exceptions = [];
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Encoding of all text values
Chris@0 48 *
Chris@0 49 * @var string
Chris@0 50 */
Chris@0 51 protected $encoding = 'UTF-8';
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Holds the value "atom" or "rss" depending on the feed type set when
Chris@0 55 * when last exported.
Chris@0 56 *
Chris@0 57 * @var string
Chris@0 58 */
Chris@0 59 protected $type = null;
Chris@0 60
Chris@0 61 /**
Chris@0 62 * @var DOMElement
Chris@0 63 */
Chris@0 64 protected $rootElement = null;
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Constructor
Chris@0 68 *
Chris@0 69 * @param Writer\AbstractFeed $container
Chris@0 70 */
Chris@0 71 public function __construct($container)
Chris@0 72 {
Chris@0 73 $this->container = $container;
Chris@0 74 $this->setType($container->getType());
Chris@0 75 $this->_loadExtensions();
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Save XML to string
Chris@0 80 *
Chris@0 81 * @return string
Chris@0 82 */
Chris@0 83 public function saveXml()
Chris@0 84 {
Chris@0 85 return $this->getDomDocument()->saveXml();
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Get DOM document
Chris@0 90 *
Chris@0 91 * @return DOMDocument
Chris@0 92 */
Chris@0 93 public function getDomDocument()
Chris@0 94 {
Chris@0 95 return $this->dom;
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Get document element from DOM
Chris@0 100 *
Chris@0 101 * @return DOMElement
Chris@0 102 */
Chris@0 103 public function getElement()
Chris@0 104 {
Chris@0 105 return $this->getDomDocument()->documentElement;
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * Get data container of items being rendered
Chris@0 110 *
Chris@0 111 * @return Writer\AbstractFeed
Chris@0 112 */
Chris@0 113 public function getDataContainer()
Chris@0 114 {
Chris@0 115 return $this->container;
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Set feed encoding
Chris@0 120 *
Chris@0 121 * @param string $enc
Chris@0 122 * @return AbstractRenderer
Chris@0 123 */
Chris@0 124 public function setEncoding($enc)
Chris@0 125 {
Chris@0 126 $this->encoding = $enc;
Chris@0 127 return $this;
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * Get feed encoding
Chris@0 132 *
Chris@0 133 * @return string
Chris@0 134 */
Chris@0 135 public function getEncoding()
Chris@0 136 {
Chris@0 137 return $this->encoding;
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Indicate whether or not to ignore exceptions
Chris@0 142 *
Chris@0 143 * @param bool $bool
Chris@0 144 * @return AbstractRenderer
Chris@0 145 * @throws Writer\Exception\InvalidArgumentException
Chris@0 146 */
Chris@0 147 public function ignoreExceptions($bool = true)
Chris@0 148 {
Chris@0 149 if (!is_bool($bool)) {
Chris@0 150 throw new Writer\Exception\InvalidArgumentException('Invalid parameter: $bool. Should be TRUE or FALSE (defaults to TRUE if null)');
Chris@0 151 }
Chris@0 152 $this->ignoreExceptions = $bool;
Chris@0 153 return $this;
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Get exception list
Chris@0 158 *
Chris@0 159 * @return array
Chris@0 160 */
Chris@0 161 public function getExceptions()
Chris@0 162 {
Chris@0 163 return $this->exceptions;
Chris@0 164 }
Chris@0 165
Chris@0 166 /**
Chris@0 167 * Set the current feed type being exported to "rss" or "atom". This allows
Chris@0 168 * other objects to gracefully choose whether to execute or not, depending
Chris@0 169 * on their appropriateness for the current type, e.g. renderers.
Chris@0 170 *
Chris@0 171 * @param string $type
Chris@0 172 */
Chris@0 173 public function setType($type)
Chris@0 174 {
Chris@0 175 $this->type = $type;
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Retrieve the current or last feed type exported.
Chris@0 180 *
Chris@0 181 * @return string Value will be "rss" or "atom"
Chris@0 182 */
Chris@0 183 public function getType()
Chris@0 184 {
Chris@0 185 return $this->type;
Chris@0 186 }
Chris@0 187
Chris@0 188 /**
Chris@0 189 * Sets the absolute root element for the XML feed being generated. This
Chris@0 190 * helps simplify the appending of namespace declarations, but also ensures
Chris@0 191 * namespaces are added to the root element - not scattered across the entire
Chris@0 192 * XML file - may assist namespace unsafe parsers and looks pretty ;).
Chris@0 193 *
Chris@0 194 * @param DOMElement $root
Chris@0 195 */
Chris@0 196 public function setRootElement(DOMElement $root)
Chris@0 197 {
Chris@0 198 $this->rootElement = $root;
Chris@0 199 }
Chris@0 200
Chris@0 201 /**
Chris@0 202 * Retrieve the absolute root element for the XML feed being generated.
Chris@0 203 *
Chris@0 204 * @return DOMElement
Chris@0 205 */
Chris@0 206 public function getRootElement()
Chris@0 207 {
Chris@0 208 return $this->rootElement;
Chris@0 209 }
Chris@0 210
Chris@0 211 /**
Chris@0 212 * Load extensions from Zend\Feed\Writer\Writer
Chris@0 213 *
Chris@0 214 * @return void
Chris@0 215 */
Chris@0 216 protected function _loadExtensions()
Chris@0 217 {
Chris@0 218 Writer\Writer::registerCoreExtensions();
Chris@0 219 $manager = Writer\Writer::getExtensionManager();
Chris@0 220 $all = Writer\Writer::getExtensions();
Chris@0 221 if (stripos(get_class($this), 'entry')) {
Chris@0 222 $exts = $all['entryRenderer'];
Chris@0 223 } else {
Chris@0 224 $exts = $all['feedRenderer'];
Chris@0 225 }
Chris@0 226 foreach ($exts as $extension) {
Chris@0 227 $plugin = $manager->get($extension);
Chris@0 228 $plugin->setDataContainer($this->getDataContainer());
Chris@0 229 $plugin->setEncoding($this->getEncoding());
Chris@0 230 $this->extensions[$extension] = $plugin;
Chris@0 231 }
Chris@0 232 }
Chris@0 233 }