comparison vendor/zendframework/zend-feed/src/Writer/Renderer/Entry/Rss.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\Entry;
11
12 use DateTime;
13 use DOMDocument;
14 use DOMElement;
15 use Zend\Feed\Uri;
16 use Zend\Feed\Writer;
17 use Zend\Feed\Writer\Renderer;
18
19 /**
20 */
21 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
22 {
23 /**
24 * Constructor
25 *
26 * @param Writer\Entry $container
27 */
28 public function __construct(Writer\Entry $container)
29 {
30 parent::__construct($container);
31 }
32
33 /**
34 * Render RSS entry
35 *
36 * @return Rss
37 */
38 public function render()
39 {
40 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
41 $this->dom->formatOutput = true;
42 $this->dom->substituteEntities = false;
43 $entry = $this->dom->createElement('item');
44 $this->dom->appendChild($entry);
45
46 $this->_setTitle($this->dom, $entry);
47 $this->_setDescription($this->dom, $entry);
48 $this->_setDateCreated($this->dom, $entry);
49 $this->_setDateModified($this->dom, $entry);
50 $this->_setLink($this->dom, $entry);
51 $this->_setId($this->dom, $entry);
52 $this->_setAuthors($this->dom, $entry);
53 $this->_setEnclosure($this->dom, $entry);
54 $this->_setCommentLink($this->dom, $entry);
55 $this->_setCategories($this->dom, $entry);
56 foreach ($this->extensions as $ext) {
57 $ext->setType($this->getType());
58 $ext->setRootElement($this->getRootElement());
59 $ext->setDOMDocument($this->getDOMDocument(), $entry);
60 $ext->render();
61 }
62
63 return $this;
64 }
65
66 /**
67 * Set entry title
68 *
69 * @param DOMDocument $dom
70 * @param DOMElement $root
71 * @return void
72 * @throws Writer\Exception\InvalidArgumentException
73 */
74 protected function _setTitle(DOMDocument $dom, DOMElement $root)
75 {
76 if (!$this->getDataContainer()->getDescription()
77 && !$this->getDataContainer()->getTitle()) {
78 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
79 . ' title element but a title has not been set. In addition, there'
80 . ' is no description as required in the absence of a title.';
81 $exception = new Writer\Exception\InvalidArgumentException($message);
82 if (!$this->ignoreExceptions) {
83 throw $exception;
84 } else {
85 $this->exceptions[] = $exception;
86 return;
87 }
88 }
89 $title = $dom->createElement('title');
90 $root->appendChild($title);
91 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
92 $title->appendChild($text);
93 }
94
95 /**
96 * Set entry description
97 *
98 * @param DOMDocument $dom
99 * @param DOMElement $root
100 * @return void
101 * @throws Writer\Exception\InvalidArgumentException
102 */
103 protected function _setDescription(DOMDocument $dom, DOMElement $root)
104 {
105 if (!$this->getDataContainer()->getDescription()
106 && !$this->getDataContainer()->getTitle()) {
107 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
108 . ' description element but a description has not been set. In'
109 . ' addition, there is no title element as required in the absence'
110 . ' of a description.';
111 $exception = new Writer\Exception\InvalidArgumentException($message);
112 if (!$this->ignoreExceptions) {
113 throw $exception;
114 } else {
115 $this->exceptions[] = $exception;
116 return;
117 }
118 }
119 if (!$this->getDataContainer()->getDescription()) {
120 return;
121 }
122 $subtitle = $dom->createElement('description');
123 $root->appendChild($subtitle);
124 $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
125 $subtitle->appendChild($text);
126 }
127
128 /**
129 * Set date entry was last modified
130 *
131 * @param DOMDocument $dom
132 * @param DOMElement $root
133 * @return void
134 */
135 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
136 {
137 if (!$this->getDataContainer()->getDateModified()) {
138 return;
139 }
140
141 $updated = $dom->createElement('pubDate');
142 $root->appendChild($updated);
143 $text = $dom->createTextNode(
144 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
145 );
146 $updated->appendChild($text);
147 }
148
149 /**
150 * Set date entry was created
151 *
152 * @param DOMDocument $dom
153 * @param DOMElement $root
154 * @return void
155 */
156 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
157 {
158 if (!$this->getDataContainer()->getDateCreated()) {
159 return;
160 }
161 if (!$this->getDataContainer()->getDateModified()) {
162 $this->getDataContainer()->setDateModified(
163 $this->getDataContainer()->getDateCreated()
164 );
165 }
166 }
167
168 /**
169 * Set entry authors
170 *
171 * @param DOMDocument $dom
172 * @param DOMElement $root
173 * @return void
174 */
175 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
176 {
177 $authors = $this->container->getAuthors();
178 if ((!$authors || empty($authors))) {
179 return;
180 }
181 foreach ($authors as $data) {
182 $author = $this->dom->createElement('author');
183 $name = $data['name'];
184 if (array_key_exists('email', $data)) {
185 $name = $data['email'] . ' (' . $data['name'] . ')';
186 }
187 $text = $dom->createTextNode($name);
188 $author->appendChild($text);
189 $root->appendChild($author);
190 }
191 }
192
193 /**
194 * Set entry enclosure
195 *
196 * @param DOMDocument $dom
197 * @param DOMElement $root
198 * @return void
199 * @throws Writer\Exception\InvalidArgumentException
200 */
201 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
202 {
203 $data = $this->container->getEnclosure();
204 if ((!$data || empty($data))) {
205 return;
206 }
207 if (!isset($data['type'])) {
208 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set');
209 if (!$this->ignoreExceptions) {
210 throw $exception;
211 } else {
212 $this->exceptions[] = $exception;
213 return;
214 }
215 }
216 if (!isset($data['length'])) {
217 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set');
218 if (!$this->ignoreExceptions) {
219 throw $exception;
220 } else {
221 $this->exceptions[] = $exception;
222 return;
223 }
224 }
225 if ((int) $data['length'] < 0 || !ctype_digit((string) $data['length'])) {
226 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer'
227 . ' indicating the content\'s length in bytes');
228 if (!$this->ignoreExceptions) {
229 throw $exception;
230 } else {
231 $this->exceptions[] = $exception;
232 return;
233 }
234 }
235 $enclosure = $this->dom->createElement('enclosure');
236 $enclosure->setAttribute('type', $data['type']);
237 $enclosure->setAttribute('length', $data['length']);
238 $enclosure->setAttribute('url', $data['uri']);
239 $root->appendChild($enclosure);
240 }
241
242 /**
243 * Set link to entry
244 *
245 * @param DOMDocument $dom
246 * @param DOMElement $root
247 * @return void
248 */
249 protected function _setLink(DOMDocument $dom, DOMElement $root)
250 {
251 if (!$this->getDataContainer()->getLink()) {
252 return;
253 }
254 $link = $dom->createElement('link');
255 $root->appendChild($link);
256 $text = $dom->createTextNode($this->getDataContainer()->getLink());
257 $link->appendChild($text);
258 }
259
260 /**
261 * Set entry identifier
262 *
263 * @param DOMDocument $dom
264 * @param DOMElement $root
265 * @return void
266 */
267 protected function _setId(DOMDocument $dom, DOMElement $root)
268 {
269 if (!$this->getDataContainer()->getId()
270 && !$this->getDataContainer()->getLink()) {
271 return;
272 }
273
274 $id = $dom->createElement('guid');
275 $root->appendChild($id);
276 if (!$this->getDataContainer()->getId()) {
277 $this->getDataContainer()->setId(
278 $this->getDataContainer()->getLink());
279 }
280 $text = $dom->createTextNode($this->getDataContainer()->getId());
281 $id->appendChild($text);
282 if (!Uri::factory($this->getDataContainer()->getId())->isValid()) {
283 $id->setAttribute('isPermaLink', 'false');
284 }
285 }
286
287 /**
288 * Set link to entry comments
289 *
290 * @param DOMDocument $dom
291 * @param DOMElement $root
292 * @return void
293 */
294 protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
295 {
296 $link = $this->getDataContainer()->getCommentLink();
297 if (!$link) {
298 return;
299 }
300 $clink = $this->dom->createElement('comments');
301 $text = $dom->createTextNode($link);
302 $clink->appendChild($text);
303 $root->appendChild($clink);
304 }
305
306 /**
307 * Set entry categories
308 *
309 * @param DOMDocument $dom
310 * @param DOMElement $root
311 * @return void
312 */
313 protected function _setCategories(DOMDocument $dom, DOMElement $root)
314 {
315 $categories = $this->getDataContainer()->getCategories();
316 if (!$categories) {
317 return;
318 }
319 foreach ($categories as $cat) {
320 $category = $dom->createElement('category');
321 if (isset($cat['scheme'])) {
322 $category->setAttribute('domain', $cat['scheme']);
323 }
324 $text = $dom->createCDATASection($cat['term']);
325 $category->appendChild($text);
326 $root->appendChild($category);
327 }
328 }
329 }