Chris@0: dom = new DOMDocument('1.0', $this->container->getEncoding()); Chris@0: $this->dom->formatOutput = true; Chris@0: $this->dom->substituteEntities = false; Chris@0: $entry = $this->dom->createElement('item'); Chris@0: $this->dom->appendChild($entry); Chris@0: Chris@0: $this->_setTitle($this->dom, $entry); Chris@0: $this->_setDescription($this->dom, $entry); Chris@0: $this->_setDateCreated($this->dom, $entry); Chris@0: $this->_setDateModified($this->dom, $entry); Chris@0: $this->_setLink($this->dom, $entry); Chris@0: $this->_setId($this->dom, $entry); Chris@0: $this->_setAuthors($this->dom, $entry); Chris@0: $this->_setEnclosure($this->dom, $entry); Chris@0: $this->_setCommentLink($this->dom, $entry); Chris@0: $this->_setCategories($this->dom, $entry); Chris@0: foreach ($this->extensions as $ext) { Chris@0: $ext->setType($this->getType()); Chris@0: $ext->setRootElement($this->getRootElement()); Chris@12: $ext->setDomDocument($this->getDomDocument(), $entry); Chris@0: $ext->render(); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry title Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: * @throws Writer\Exception\InvalidArgumentException Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setTitle(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getDescription() Chris@12: && ! $this->getDataContainer()->getTitle()) { Chris@0: $message = 'RSS 2.0 entry elements SHOULD contain exactly one' Chris@0: . ' title element but a title has not been set. In addition, there' Chris@0: . ' is no description as required in the absence of a title.'; Chris@0: $exception = new Writer\Exception\InvalidArgumentException($message); Chris@12: if (! $this->ignoreExceptions) { Chris@0: throw $exception; Chris@0: } else { Chris@0: $this->exceptions[] = $exception; Chris@0: return; Chris@0: } Chris@0: } Chris@0: $title = $dom->createElement('title'); Chris@0: $root->appendChild($title); Chris@0: $text = $dom->createTextNode($this->getDataContainer()->getTitle()); Chris@0: $title->appendChild($text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry description Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: * @throws Writer\Exception\InvalidArgumentException Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setDescription(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getDescription() Chris@12: && ! $this->getDataContainer()->getTitle()) { Chris@0: $message = 'RSS 2.0 entry elements SHOULD contain exactly one' Chris@0: . ' description element but a description has not been set. In' Chris@0: . ' addition, there is no title element as required in the absence' Chris@0: . ' of a description.'; Chris@0: $exception = new Writer\Exception\InvalidArgumentException($message); Chris@12: if (! $this->ignoreExceptions) { Chris@0: throw $exception; Chris@0: } else { Chris@0: $this->exceptions[] = $exception; Chris@0: return; Chris@0: } Chris@0: } Chris@12: if (! $this->getDataContainer()->getDescription()) { Chris@0: return; Chris@0: } Chris@0: $subtitle = $dom->createElement('description'); Chris@0: $root->appendChild($subtitle); Chris@0: $text = $dom->createCDATASection($this->getDataContainer()->getDescription()); Chris@0: $subtitle->appendChild($text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set date entry was last modified Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setDateModified(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getDateModified()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $updated = $dom->createElement('pubDate'); Chris@0: $root->appendChild($updated); Chris@0: $text = $dom->createTextNode( Chris@0: $this->getDataContainer()->getDateModified()->format(DateTime::RSS) Chris@0: ); Chris@0: $updated->appendChild($text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set date entry was created Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setDateCreated(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getDateCreated()) { Chris@0: return; Chris@0: } Chris@12: if (! $this->getDataContainer()->getDateModified()) { Chris@0: $this->getDataContainer()->setDateModified( Chris@0: $this->getDataContainer()->getDateCreated() Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry authors Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setAuthors(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $authors = $this->container->getAuthors(); Chris@12: if ((! $authors || empty($authors))) { Chris@0: return; Chris@0: } Chris@0: foreach ($authors as $data) { Chris@0: $author = $this->dom->createElement('author'); Chris@0: $name = $data['name']; Chris@0: if (array_key_exists('email', $data)) { Chris@0: $name = $data['email'] . ' (' . $data['name'] . ')'; Chris@0: } Chris@0: $text = $dom->createTextNode($name); Chris@0: $author->appendChild($text); Chris@0: $root->appendChild($author); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry enclosure Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: * @throws Writer\Exception\InvalidArgumentException Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setEnclosure(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $data = $this->container->getEnclosure(); Chris@12: if ((! $data || empty($data))) { Chris@0: return; Chris@0: } Chris@12: if (! isset($data['type'])) { Chris@0: $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set'); Chris@12: if (! $this->ignoreExceptions) { Chris@0: throw $exception; Chris@0: } else { Chris@0: $this->exceptions[] = $exception; Chris@0: return; Chris@0: } Chris@0: } Chris@12: if (! isset($data['length'])) { Chris@0: $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set'); Chris@12: if (! $this->ignoreExceptions) { Chris@0: throw $exception; Chris@0: } else { Chris@0: $this->exceptions[] = $exception; Chris@0: return; Chris@0: } Chris@0: } Chris@12: if ((int) $data['length'] < 0 || ! ctype_digit((string) $data['length'])) { Chris@0: $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer' Chris@0: . ' indicating the content\'s length in bytes'); Chris@12: if (! $this->ignoreExceptions) { Chris@0: throw $exception; Chris@0: } else { Chris@0: $this->exceptions[] = $exception; Chris@0: return; Chris@0: } Chris@0: } Chris@0: $enclosure = $this->dom->createElement('enclosure'); Chris@0: $enclosure->setAttribute('type', $data['type']); Chris@0: $enclosure->setAttribute('length', $data['length']); Chris@0: $enclosure->setAttribute('url', $data['uri']); Chris@0: $root->appendChild($enclosure); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set link to entry Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setLink(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getLink()) { Chris@0: return; Chris@0: } Chris@0: $link = $dom->createElement('link'); Chris@0: $root->appendChild($link); Chris@0: $text = $dom->createTextNode($this->getDataContainer()->getLink()); Chris@0: $link->appendChild($text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry identifier Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setId(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getId() Chris@12: && ! $this->getDataContainer()->getLink()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $id = $dom->createElement('guid'); Chris@0: $root->appendChild($id); Chris@12: if (! $this->getDataContainer()->getId()) { Chris@0: $this->getDataContainer()->setId( Chris@12: $this->getDataContainer()->getLink() Chris@12: ); Chris@0: } Chris@0: $text = $dom->createTextNode($this->getDataContainer()->getId()); Chris@0: $id->appendChild($text); Chris@18: Chris@18: $uri = Uri::factory($this->getDataContainer()->getId()); Chris@18: if (! $uri->isValid() || ! $uri->isAbsolute()) { Chris@18: /** @see http://www.rssboard.org/rss-profile#element-channel-item-guid */ Chris@0: $id->setAttribute('isPermaLink', 'false'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set link to entry comments Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setCommentLink(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $link = $this->getDataContainer()->getCommentLink(); Chris@12: if (! $link) { Chris@0: return; Chris@0: } Chris@0: $clink = $this->dom->createElement('comments'); Chris@0: $text = $dom->createTextNode($link); Chris@0: $clink->appendChild($text); Chris@0: $root->appendChild($clink); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry categories Chris@0: * Chris@0: * @param DOMDocument $dom Chris@0: * @param DOMElement $root Chris@0: * @return void Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setCategories(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $categories = $this->getDataContainer()->getCategories(); Chris@12: if (! $categories) { Chris@0: return; Chris@0: } Chris@0: foreach ($categories as $cat) { Chris@0: $category = $dom->createElement('category'); Chris@0: if (isset($cat['scheme'])) { Chris@0: $category->setAttribute('domain', $cat['scheme']); Chris@0: } Chris@0: $text = $dom->createCDATASection($cat['term']); Chris@0: $category->appendChild($text); Chris@0: $root->appendChild($category); Chris@0: } Chris@0: } Chris@0: }