3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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 protected function _setTitle(DOMDocument
$dom, DOMElement
$root)
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
) {
85 $this->exceptions
[] = $exception;
89 $title = $dom->createElement('title');
90 $root->appendChild($title);
91 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
92 $title->appendChild($text);
96 * Set entry description
98 * @param DOMDocument $dom
99 * @param DOMElement $root
101 * @throws Writer\Exception\InvalidArgumentException
103 protected function _setDescription(DOMDocument
$dom, DOMElement
$root)
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
) {
115 $this->exceptions
[] = $exception;
119 if (!$this->getDataContainer()->getDescription()) {
122 $subtitle = $dom->createElement('description');
123 $root->appendChild($subtitle);
124 $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
125 $subtitle->appendChild($text);
129 * Set date entry was last modified
131 * @param DOMDocument $dom
132 * @param DOMElement $root
135 protected function _setDateModified(DOMDocument
$dom, DOMElement
$root)
137 if (!$this->getDataContainer()->getDateModified()) {
141 $updated = $dom->createElement('pubDate');
142 $root->appendChild($updated);
143 $text = $dom->createTextNode(
144 $this->getDataContainer()->getDateModified()->format(DateTime
::RSS
)
146 $updated->appendChild($text);
150 * Set date entry was created
152 * @param DOMDocument $dom
153 * @param DOMElement $root
156 protected function _setDateCreated(DOMDocument
$dom, DOMElement
$root)
158 if (!$this->getDataContainer()->getDateCreated()) {
161 if (!$this->getDataContainer()->getDateModified()) {
162 $this->getDataContainer()->setDateModified(
163 $this->getDataContainer()->getDateCreated()
171 * @param DOMDocument $dom
172 * @param DOMElement $root
175 protected function _setAuthors(DOMDocument
$dom, DOMElement
$root)
177 $authors = $this->container
->getAuthors();
178 if ((!$authors ||
empty($authors))) {
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'] . ')';
187 $text = $dom->createTextNode($name);
188 $author->appendChild($text);
189 $root->appendChild($author);
194 * Set entry enclosure
196 * @param DOMDocument $dom
197 * @param DOMElement $root
199 * @throws Writer\Exception\InvalidArgumentException
201 protected function _setEnclosure(DOMDocument
$dom, DOMElement
$root)
203 $data = $this->container
->getEnclosure();
204 if ((!$data ||
empty($data))) {
207 if (!isset($data['type'])) {
208 $exception = new Writer\Exception\
InvalidArgumentException('Enclosure "type" is not set');
209 if (!$this->ignoreExceptions
) {
212 $this->exceptions
[] = $exception;
216 if (!isset($data['length'])) {
217 $exception = new Writer\Exception\
InvalidArgumentException('Enclosure "length" is not set');
218 if (!$this->ignoreExceptions
) {
221 $this->exceptions
[] = $exception;
225 if (isset($data['length']) && (int) $data['length'] <= 0) {
226 $exception = new Writer\Exception\
InvalidArgumentException('Enclosure "length" must be an integer'
227 . ' indicating the content\'s length in bytes');
228 if (!$this->ignoreExceptions
) {
231 $this->exceptions
[] = $exception;
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);
245 * @param DOMDocument $dom
246 * @param DOMElement $root
249 protected function _setLink(DOMDocument
$dom, DOMElement
$root)
251 if (!$this->getDataContainer()->getLink()) {
254 $link = $dom->createElement('link');
255 $root->appendChild($link);
256 $text = $dom->createTextNode($this->getDataContainer()->getLink());
257 $link->appendChild($text);
261 * Set entry identifier
263 * @param DOMDocument $dom
264 * @param DOMElement $root
267 protected function _setId(DOMDocument
$dom, DOMElement
$root)
269 if (!$this->getDataContainer()->getId()
270 && !$this->getDataContainer()->getLink()) {
274 $id = $dom->createElement('guid');
275 $root->appendChild($id);
276 if (!$this->getDataContainer()->getId()) {
277 $this->getDataContainer()->setId(
278 $this->getDataContainer()->getLink());
280 $text = $dom->createTextNode($this->getDataContainer()->getId());
281 $id->appendChild($text);
282 if (!Uri
::factory($this->getDataContainer()->getId())->isValid()) {
283 $id->setAttribute('isPermaLink', 'false');
288 * Set link to entry comments
290 * @param DOMDocument $dom
291 * @param DOMElement $root
294 protected function _setCommentLink(DOMDocument
$dom, DOMElement
$root)
296 $link = $this->getDataContainer()->getCommentLink();
300 $clink = $this->dom
->createElement('comments');
301 $text = $dom->createTextNode($link);
302 $clink->appendChild($text);
303 $root->appendChild($clink);
307 * Set entry categories
309 * @param DOMDocument $dom
310 * @param DOMElement $root
313 protected function _setCategories(DOMDocument
$dom, DOMElement
$root)
315 $categories = $this->getDataContainer()->getCategories();
319 foreach ($categories as $cat) {
320 $category = $dom->createElement('category');
321 if (isset($cat['scheme'])) {
322 $category->setAttribute('domain', $cat['scheme']);
324 $text = $dom->createCDATASection($cat['term']);
325 $category->appendChild($text);
326 $root->appendChild($category);