3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Feed\Writer\Renderer\Entry;
17 use Zend\Feed\Writer\Renderer;
21 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
26 * @param Writer\Entry $container
28 public function __construct(Writer\Entry $container)
30 parent::__construct($container);
38 public function render()
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);
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);
69 * @param DOMDocument $dom
70 * @param DOMElement $root
72 * @throws Writer\Exception\InvalidArgumentException
74 // @codingStandardsIgnoreStart
75 protected function _setTitle(DOMDocument $dom, DOMElement $root)
77 // @codingStandardsIgnoreEnd
78 if (! $this->getDataContainer()->getDescription()
79 && ! $this->getDataContainer()->getTitle()) {
80 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
81 . ' title element but a title has not been set. In addition, there'
82 . ' is no description as required in the absence of a title.';
83 $exception = new Writer\Exception\InvalidArgumentException($message);
84 if (! $this->ignoreExceptions) {
87 $this->exceptions[] = $exception;
91 $title = $dom->createElement('title');
92 $root->appendChild($title);
93 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
94 $title->appendChild($text);
98 * Set entry description
100 * @param DOMDocument $dom
101 * @param DOMElement $root
103 * @throws Writer\Exception\InvalidArgumentException
105 // @codingStandardsIgnoreStart
106 protected function _setDescription(DOMDocument $dom, DOMElement $root)
108 // @codingStandardsIgnoreEnd
109 if (! $this->getDataContainer()->getDescription()
110 && ! $this->getDataContainer()->getTitle()) {
111 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
112 . ' description element but a description has not been set. In'
113 . ' addition, there is no title element as required in the absence'
114 . ' of a description.';
115 $exception = new Writer\Exception\InvalidArgumentException($message);
116 if (! $this->ignoreExceptions) {
119 $this->exceptions[] = $exception;
123 if (! $this->getDataContainer()->getDescription()) {
126 $subtitle = $dom->createElement('description');
127 $root->appendChild($subtitle);
128 $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
129 $subtitle->appendChild($text);
133 * Set date entry was last modified
135 * @param DOMDocument $dom
136 * @param DOMElement $root
139 // @codingStandardsIgnoreStart
140 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
142 // @codingStandardsIgnoreEnd
143 if (! $this->getDataContainer()->getDateModified()) {
147 $updated = $dom->createElement('pubDate');
148 $root->appendChild($updated);
149 $text = $dom->createTextNode(
150 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
152 $updated->appendChild($text);
156 * Set date entry was created
158 * @param DOMDocument $dom
159 * @param DOMElement $root
162 // @codingStandardsIgnoreStart
163 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
165 // @codingStandardsIgnoreEnd
166 if (! $this->getDataContainer()->getDateCreated()) {
169 if (! $this->getDataContainer()->getDateModified()) {
170 $this->getDataContainer()->setDateModified(
171 $this->getDataContainer()->getDateCreated()
179 * @param DOMDocument $dom
180 * @param DOMElement $root
183 // @codingStandardsIgnoreStart
184 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
186 // @codingStandardsIgnoreEnd
187 $authors = $this->container->getAuthors();
188 if ((! $authors || empty($authors))) {
191 foreach ($authors as $data) {
192 $author = $this->dom->createElement('author');
193 $name = $data['name'];
194 if (array_key_exists('email', $data)) {
195 $name = $data['email'] . ' (' . $data['name'] . ')';
197 $text = $dom->createTextNode($name);
198 $author->appendChild($text);
199 $root->appendChild($author);
204 * Set entry enclosure
206 * @param DOMDocument $dom
207 * @param DOMElement $root
209 * @throws Writer\Exception\InvalidArgumentException
211 // @codingStandardsIgnoreStart
212 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
214 // @codingStandardsIgnoreEnd
215 $data = $this->container->getEnclosure();
216 if ((! $data || empty($data))) {
219 if (! isset($data['type'])) {
220 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set');
221 if (! $this->ignoreExceptions) {
224 $this->exceptions[] = $exception;
228 if (! isset($data['length'])) {
229 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set');
230 if (! $this->ignoreExceptions) {
233 $this->exceptions[] = $exception;
237 if ((int) $data['length'] < 0 || ! ctype_digit((string) $data['length'])) {
238 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer'
239 . ' indicating the content\'s length in bytes');
240 if (! $this->ignoreExceptions) {
243 $this->exceptions[] = $exception;
247 $enclosure = $this->dom->createElement('enclosure');
248 $enclosure->setAttribute('type', $data['type']);
249 $enclosure->setAttribute('length', $data['length']);
250 $enclosure->setAttribute('url', $data['uri']);
251 $root->appendChild($enclosure);
257 * @param DOMDocument $dom
258 * @param DOMElement $root
261 // @codingStandardsIgnoreStart
262 protected function _setLink(DOMDocument $dom, DOMElement $root)
264 // @codingStandardsIgnoreEnd
265 if (! $this->getDataContainer()->getLink()) {
268 $link = $dom->createElement('link');
269 $root->appendChild($link);
270 $text = $dom->createTextNode($this->getDataContainer()->getLink());
271 $link->appendChild($text);
275 * Set entry identifier
277 * @param DOMDocument $dom
278 * @param DOMElement $root
281 // @codingStandardsIgnoreStart
282 protected function _setId(DOMDocument $dom, DOMElement $root)
284 // @codingStandardsIgnoreEnd
285 if (! $this->getDataContainer()->getId()
286 && ! $this->getDataContainer()->getLink()) {
290 $id = $dom->createElement('guid');
291 $root->appendChild($id);
292 if (! $this->getDataContainer()->getId()) {
293 $this->getDataContainer()->setId(
294 $this->getDataContainer()->getLink()
297 $text = $dom->createTextNode($this->getDataContainer()->getId());
298 $id->appendChild($text);
299 if (! Uri::factory($this->getDataContainer()->getId())->isValid()) {
300 $id->setAttribute('isPermaLink', 'false');
305 * Set link to entry comments
307 * @param DOMDocument $dom
308 * @param DOMElement $root
311 // @codingStandardsIgnoreStart
312 protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
314 // @codingStandardsIgnoreEnd
315 $link = $this->getDataContainer()->getCommentLink();
319 $clink = $this->dom->createElement('comments');
320 $text = $dom->createTextNode($link);
321 $clink->appendChild($text);
322 $root->appendChild($clink);
326 * Set entry categories
328 * @param DOMDocument $dom
329 * @param DOMElement $root
332 // @codingStandardsIgnoreStart
333 protected function _setCategories(DOMDocument $dom, DOMElement $root)
335 // @codingStandardsIgnoreEnd
336 $categories = $this->getDataContainer()->getCategories();
340 foreach ($categories as $cat) {
341 $category = $dom->createElement('category');
342 if (isset($cat['scheme'])) {
343 $category->setAttribute('domain', $cat['scheme']);
345 $text = $dom->createCDATASection($cat['term']);
346 $category->appendChild($text);
347 $root->appendChild($category);