Source of file Rss.php
Size: 10,423 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Feed/Writer/Renderer/Entry/Rss.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Feed\Writer\Renderer\Entry; use DateTime; use DOMDocument; use DOMElement; use Zend\Feed\Uri; use Zend\Feed\Writer; use Zend\Feed\Writer\Renderer; /** */ class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface { /** * Constructor * * @param Writer\Entry $container */ public function __construct(Writer\Entry $container) { parent::__construct($container); } /** * Render RSS entry * * @return Rss */ public function render() { $this->dom = new DOMDocument('1.0', $this->container->getEncoding()); $this->dom->formatOutput = true; $this->dom->substituteEntities = false; $entry = $this->dom->createElement('item'); $this->dom->appendChild($entry); $this->_setTitle($this->dom, $entry); $this->_setDescription($this->dom, $entry); $this->_setDateCreated($this->dom, $entry); $this->_setDateModified($this->dom, $entry); $this->_setLink($this->dom, $entry); $this->_setId($this->dom, $entry); $this->_setAuthors($this->dom, $entry); $this->_setEnclosure($this->dom, $entry); $this->_setCommentLink($this->dom, $entry); $this->_setCategories($this->dom, $entry); foreach ($this->extensions as $ext) { $ext->setType($this->getType()); $ext->setRootElement($this->getRootElement()); $ext->setDOMDocument($this->getDOMDocument(), $entry); $ext->render(); } return $this; } /** * Set entry title * * @param DOMDocument $dom * @param DOMElement $root * @return void * @throws Writer\Exception\InvalidArgumentException */ protected function _setTitle(DOMDocument $dom, DOMElement $root) { if (!$this->getDataContainer()->getDescription() && !$this->getDataContainer()->getTitle()) { $message = 'RSS 2.0 entry elements SHOULD contain exactly one' . ' title element but a title has not been set. In addition, there' . ' is no description as required in the absence of a title.'; $exception = new Writer\Exception\InvalidArgumentException($message); if (!$this->ignoreExceptions) { throw $exception; } else { $this->exceptions[] = $exception; return; } } $title = $dom->createElement('title'); $root->appendChild($title); $text = $dom->createTextNode($this->getDataContainer()->getTitle()); $title->appendChild($text); } /** * Set entry description * * @param DOMDocument $dom * @param DOMElement $root * @return void * @throws Writer\Exception\InvalidArgumentException */ protected function _setDescription(DOMDocument $dom, DOMElement $root) { if (!$this->getDataContainer()->getDescription() && !$this->getDataContainer()->getTitle()) { $message = 'RSS 2.0 entry elements SHOULD contain exactly one' . ' description element but a description has not been set. In' . ' addition, there is no title element as required in the absence' . ' of a description.'; $exception = new Writer\Exception\InvalidArgumentException($message); if (!$this->ignoreExceptions) { throw $exception; } else { $this->exceptions[] = $exception; return; } } if (!$this->getDataContainer()->getDescription()) { return; } $subtitle = $dom->createElement('description'); $root->appendChild($subtitle); $text = $dom->createCDATASection($this->getDataContainer()->getDescription()); $subtitle->appendChild($text); } /** * Set date entry was last modified * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setDateModified(DOMDocument $dom, DOMElement $root) { if (!$this->getDataContainer()->getDateModified()) { return; } $updated = $dom->createElement('pubDate'); $root->appendChild($updated); $text = $dom->createTextNode( $this->getDataContainer()->getDateModified()->format(DateTime::RSS) ); $updated->appendChild($text); } /** * Set date entry was created * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setDateCreated(DOMDocument $dom, DOMElement $root) { if (!$this->getDataContainer()->getDateCreated()) { return; } if (!$this->getDataContainer()->getDateModified()) { $this->getDataContainer()->setDateModified( $this->getDataContainer()->getDateCreated() ); } } /** * Set entry authors * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setAuthors(DOMDocument $dom, DOMElement $root) { $authors = $this->container->getAuthors(); if ((!$authors || empty($authors))) { return; } foreach ($authors as $data) { $author = $this->dom->createElement('author'); $name = $data['name']; if (array_key_exists('email', $data)) { $name = $data['email'] . ' (' . $data['name'] . ')'; } $text = $dom->createTextNode($name); $author->appendChild($text); $root->appendChild($author); } } /** * Set entry enclosure * * @param DOMDocument $dom * @param DOMElement $root * @return void * @throws Writer\Exception\InvalidArgumentException */ protected function _setEnclosure(DOMDocument $dom, DOMElement $root) { $data = $this->container->getEnclosure(); if ((!$data || empty($data))) { return; } if (!isset($data['type'])) { $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set'); if (!$this->ignoreExceptions) { throw $exception; } else { $this->exceptions[] = $exception; return; } } if (!isset($data['length'])) { $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set'); if (!$this->ignoreExceptions) { throw $exception; } else { $this->exceptions[] = $exception; return; } } if (isset($data['length']) && (int) $data['length'] <= 0) { $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer' . ' indicating the content\'s length in bytes'); if (!$this->ignoreExceptions) { throw $exception; } else { $this->exceptions[] = $exception; return; } } $enclosure = $this->dom->createElement('enclosure'); $enclosure->setAttribute('type', $data['type']); $enclosure->setAttribute('length', $data['length']); $enclosure->setAttribute('url', $data['uri']); $root->appendChild($enclosure); } /** * Set link to entry * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setLink(DOMDocument $dom, DOMElement $root) { if (!$this->getDataContainer()->getLink()) { return; } $link = $dom->createElement('link'); $root->appendChild($link); $text = $dom->createTextNode($this->getDataContainer()->getLink()); $link->appendChild($text); } /** * Set entry identifier * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setId(DOMDocument $dom, DOMElement $root) { if (!$this->getDataContainer()->getId() && !$this->getDataContainer()->getLink()) { return; } $id = $dom->createElement('guid'); $root->appendChild($id); if (!$this->getDataContainer()->getId()) { $this->getDataContainer()->setId( $this->getDataContainer()->getLink()); } $text = $dom->createTextNode($this->getDataContainer()->getId()); $id->appendChild($text); if (!Uri::factory($this->getDataContainer()->getId())->isValid()) { $id->setAttribute('isPermaLink', 'false'); } } /** * Set link to entry comments * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setCommentLink(DOMDocument $dom, DOMElement $root) { $link = $this->getDataContainer()->getCommentLink(); if (!$link) { return; } $clink = $this->dom->createElement('comments'); $text = $dom->createTextNode($link); $clink->appendChild($text); $root->appendChild($clink); } /** * Set entry categories * * @param DOMDocument $dom * @param DOMElement $root * @return void */ protected function _setCategories(DOMDocument $dom, DOMElement $root) { $categories = $this->getDataContainer()->getCategories(); if (!$categories) { return; } foreach ($categories as $cat) { $category = $dom->createElement('category'); if (isset($cat['scheme'])) { $category->setAttribute('domain', $cat['scheme']); } $text = $dom->createCDATASection($cat['term']); $category->appendChild($text); $root->appendChild($category); } } } |