Chris@0: dom = new DOMDocument('1.0', $this->container->getEncoding()); Chris@0: $this->dom->formatOutput = true; Chris@0: $entry = $this->dom->createElementNS(Writer\Writer::NAMESPACE_ATOM_10, 'entry'); Chris@0: $this->dom->appendChild($entry); Chris@0: Chris@0: $this->_setSource($this->dom, $entry); 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->_setContent($this->dom, $entry); Chris@0: $this->_setCategories($this->dom, $entry); Chris@0: 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()->getTitle()) { Chris@0: $message = 'Atom 1.0 entry elements MUST contain exactly one' Chris@0: . ' atom:title element but a title has not been set'; 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: $title->setAttribute('type', 'html'); Chris@0: $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle()); Chris@0: $title->appendChild($cdata); 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: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _setDescription(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getDescription()) { Chris@0: return; // unless src content or base64 Chris@0: } Chris@0: $subtitle = $dom->createElement('summary'); Chris@0: $root->appendChild($subtitle); Chris@0: $subtitle->setAttribute('type', 'html'); Chris@0: $cdata = $dom->createCDATASection( Chris@0: $this->getDataContainer()->getDescription() Chris@0: ); Chris@0: $subtitle->appendChild($cdata); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set date entry was modified 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 _setDateModified(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@12: if (! $this->getDataContainer()->getDateModified()) { Chris@0: $message = 'Atom 1.0 entry elements MUST contain exactly one' Chris@0: . ' atom:updated element but a modification date has not been set'; 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: Chris@0: $updated = $dom->createElement('updated'); Chris@0: $root->appendChild($updated); Chris@0: $text = $dom->createTextNode( Chris@0: $this->getDataContainer()->getDateModified()->format(DateTime::ATOM) 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@0: $el = $dom->createElement('published'); Chris@0: $root->appendChild($el); Chris@0: $text = $dom->createTextNode( Chris@0: $this->getDataContainer()->getDateCreated()->format(DateTime::ATOM) Chris@0: ); Chris@0: $el->appendChild($text); 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: /** Chris@0: * This will actually trigger an Exception at the feed level if Chris@0: * a feed level author is not set. Chris@0: */ Chris@0: return; Chris@0: } Chris@0: foreach ($authors as $data) { Chris@0: $author = $this->dom->createElement('author'); Chris@0: $name = $this->dom->createElement('name'); Chris@0: $author->appendChild($name); Chris@0: $root->appendChild($author); Chris@0: $text = $dom->createTextNode($data['name']); Chris@0: $name->appendChild($text); Chris@0: if (array_key_exists('email', $data)) { Chris@0: $email = $this->dom->createElement('email'); Chris@0: $author->appendChild($email); Chris@0: $text = $dom->createTextNode($data['email']); Chris@0: $email->appendChild($text); Chris@0: } Chris@0: if (array_key_exists('uri', $data)) { Chris@0: $uri = $this->dom->createElement('uri'); Chris@0: $author->appendChild($uri); Chris@0: $text = $dom->createTextNode($data['uri']); Chris@0: $uri->appendChild($text); Chris@0: } 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: */ 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@0: $enclosure = $this->dom->createElement('link'); Chris@0: $enclosure->setAttribute('rel', 'enclosure'); Chris@0: if (isset($data['type'])) { Chris@0: $enclosure->setAttribute('type', $data['type']); Chris@0: } Chris@0: if (isset($data['length'])) { Chris@0: $enclosure->setAttribute('length', $data['length']); Chris@0: } Chris@0: $enclosure->setAttribute('href', $data['uri']); Chris@0: $root->appendChild($enclosure); Chris@0: } 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: $link->setAttribute('rel', 'alternate'); Chris@0: $link->setAttribute('type', 'text/html'); Chris@0: $link->setAttribute('href', $this->getDataContainer()->getLink()); 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: * @throws Writer\Exception\InvalidArgumentException 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: $message = 'Atom 1.0 entry elements MUST contain exactly one ' Chris@0: . 'atom:id element, or as an alternative, we can use the same ' Chris@0: . 'value as atom:link however neither a suitable link nor an ' Chris@0: . 'id have been set'; 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: Chris@12: if (! $this->getDataContainer()->getId()) { Chris@0: $this->getDataContainer()->setId( Chris@0: $this->getDataContainer()->getLink() Chris@0: ); Chris@0: } Chris@12: if (! Uri::factory($this->getDataContainer()->getId())->isValid() Chris@12: && ! preg_match( Chris@0: "#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", Chris@0: $this->getDataContainer()->getId() Chris@0: ) Chris@12: && ! $this->_validateTagUri($this->getDataContainer()->getId()) Chris@0: ) { Chris@0: throw new Writer\Exception\InvalidArgumentException('Atom 1.0 IDs must be a valid URI/IRI'); Chris@0: } Chris@0: $id = $dom->createElement('id'); Chris@0: $root->appendChild($id); Chris@0: $text = $dom->createTextNode($this->getDataContainer()->getId()); Chris@0: $id->appendChild($text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate a URI using the tag scheme (RFC 4151) Chris@0: * Chris@0: * @param string $id Chris@0: * @return bool Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _validateTagUri($id) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: if (preg_match( Chris@0: '/^tag:(?P.*),(?P\d{4}-?\d{0,2}-?\d{0,2}):(?P.*)(.*:)*$/', Chris@0: $id, Chris@0: $matches Chris@0: )) { Chris@0: $dvalid = false; Chris@0: $date = $matches['date']; Chris@0: $d6 = strtotime($date); Chris@0: if ((strlen($date) == 4) && $date <= date('Y')) { Chris@0: $dvalid = true; Chris@0: } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) { Chris@0: $dvalid = true; Chris@0: } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) { Chris@0: $dvalid = true; Chris@0: } Chris@0: $validator = new Validator\EmailAddress; Chris@0: if ($validator->isValid($matches['name'])) { Chris@0: $nvalid = true; Chris@0: } else { Chris@0: $nvalid = $validator->isValid('info@' . $matches['name']); Chris@0: } Chris@0: return $dvalid && $nvalid; Chris@0: } Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entry content 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 _setContent(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $content = $this->getDataContainer()->getContent(); Chris@12: if (! $content && ! $this->getDataContainer()->getLink()) { Chris@0: $message = 'Atom 1.0 entry elements MUST contain exactly one ' Chris@0: . 'atom:content element, or as an alternative, at least one link ' Chris@0: . 'with a rel attribute of "alternate" to indicate an alternate ' Chris@0: . 'method to consume the content.'; 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 (! $content) { Chris@0: return; Chris@0: } Chris@0: $element = $dom->createElement('content'); Chris@0: $element->setAttribute('type', 'xhtml'); Chris@0: $xhtmlElement = $this->_loadXhtml($content); Chris@0: $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true; Chris@0: $xhtml = $dom->importNode($xhtmlElement, $deep); Chris@0: $element->appendChild($xhtml); Chris@0: $root->appendChild($element); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Load a HTML string and attempt to normalise to XML Chris@17: * Chris@17: * @param string $content Chris@17: * @return \DOMElement Chris@0: */ Chris@12: // @codingStandardsIgnoreStart Chris@0: protected function _loadXhtml($content) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: if (class_exists('tidy', false)) { Chris@0: $tidy = new \tidy; Chris@0: $config = [ Chris@0: 'output-xhtml' => true, Chris@0: 'show-body-only' => true, Chris@0: 'quote-nbsp' => false Chris@0: ]; Chris@0: $encoding = str_replace('-', '', $this->getEncoding()); Chris@0: $tidy->parseString($content, $config, $encoding); Chris@0: $tidy->cleanRepair(); Chris@0: $xhtml = (string) $tidy; Chris@0: } else { Chris@0: $xhtml = $content; Chris@0: } Chris@0: $xhtml = preg_replace([ Chris@0: "/(<[\/]?)([a-zA-Z]+)/" Chris@0: ], '$1xhtml:$2', $xhtml); Chris@0: $dom = new DOMDocument('1.0', $this->getEncoding()); Chris@0: $dom->loadXML( Chris@0: '' Chris@0: . $xhtml Chris@0: . '' Chris@0: ); Chris@0: return $dom->documentElement; 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: $category->setAttribute('term', $cat['term']); Chris@0: if (isset($cat['label'])) { Chris@0: $category->setAttribute('label', $cat['label']); Chris@0: } else { Chris@0: $category->setAttribute('label', $cat['term']); Chris@0: } Chris@0: if (isset($cat['scheme'])) { Chris@0: $category->setAttribute('scheme', $cat['scheme']); Chris@0: } Chris@0: $root->appendChild($category); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Append Source element (Atom 1.0 Feed Metadata) 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 _setSource(DOMDocument $dom, DOMElement $root) Chris@0: { Chris@12: // @codingStandardsIgnoreEnd Chris@0: $source = $this->getDataContainer()->getSource(); Chris@12: if (! $source) { Chris@0: return; Chris@0: } Chris@0: $renderer = new Renderer\Feed\AtomSource($source); Chris@0: $renderer->setType($this->getType()); Chris@0: $element = $renderer->render()->getElement(); Chris@0: $imported = $dom->importNode($element, true); Chris@0: $root->appendChild($imported); Chris@0: } Chris@0: }