comparison vendor/zendframework/zend-feed/src/Writer/Renderer/AbstractRenderer.php @ 0:c75dbcec494b

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