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;
20 class Atom extends Renderer\AbstractRenderer implements Renderer\RendererInterface
25 * @param Writer\Entry $container
27 public function __construct(Writer\Entry $container)
29 parent::__construct($container);
37 public function render()
39 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
40 $this->dom->formatOutput = true;
41 $entry = $this->dom->createElementNS(Writer\Writer::NAMESPACE_ATOM_10, 'entry');
42 $this->dom->appendChild($entry);
44 $this->_setSource($this->dom, $entry);
45 $this->_setTitle($this->dom, $entry);
46 $this->_setDescription($this->dom, $entry);
47 $this->_setDateCreated($this->dom, $entry);
48 $this->_setDateModified($this->dom, $entry);
49 $this->_setLink($this->dom, $entry);
50 $this->_setId($this->dom, $entry);
51 $this->_setAuthors($this->dom, $entry);
52 $this->_setEnclosure($this->dom, $entry);
53 $this->_setContent($this->dom, $entry);
54 $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()->getTitle()) {
79 $message = 'Atom 1.0 entry elements MUST contain exactly one'
80 . ' atom:title element but a title has not been set';
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 $title->setAttribute('type', 'html');
92 $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
93 $title->appendChild($cdata);
97 * Set entry description
99 * @param DOMDocument $dom
100 * @param DOMElement $root
103 // @codingStandardsIgnoreStart
104 protected function _setDescription(DOMDocument $dom, DOMElement $root)
106 // @codingStandardsIgnoreEnd
107 if (! $this->getDataContainer()->getDescription()) {
108 return; // unless src content or base64
110 $subtitle = $dom->createElement('summary');
111 $root->appendChild($subtitle);
112 $subtitle->setAttribute('type', 'html');
113 $cdata = $dom->createCDATASection(
114 $this->getDataContainer()->getDescription()
116 $subtitle->appendChild($cdata);
120 * Set date entry was modified
122 * @param DOMDocument $dom
123 * @param DOMElement $root
125 * @throws Writer\Exception\InvalidArgumentException
127 // @codingStandardsIgnoreStart
128 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
130 // @codingStandardsIgnoreEnd
131 if (! $this->getDataContainer()->getDateModified()) {
132 $message = 'Atom 1.0 entry elements MUST contain exactly one'
133 . ' atom:updated element but a modification date has not been set';
134 $exception = new Writer\Exception\InvalidArgumentException($message);
135 if (! $this->ignoreExceptions) {
138 $this->exceptions[] = $exception;
143 $updated = $dom->createElement('updated');
144 $root->appendChild($updated);
145 $text = $dom->createTextNode(
146 $this->getDataContainer()->getDateModified()->format(DateTime::ATOM)
148 $updated->appendChild($text);
152 * Set date entry was created
154 * @param DOMDocument $dom
155 * @param DOMElement $root
158 // @codingStandardsIgnoreStart
159 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
161 // @codingStandardsIgnoreEnd
162 if (! $this->getDataContainer()->getDateCreated()) {
165 $el = $dom->createElement('published');
166 $root->appendChild($el);
167 $text = $dom->createTextNode(
168 $this->getDataContainer()->getDateCreated()->format(DateTime::ATOM)
170 $el->appendChild($text);
176 * @param DOMDocument $dom
177 * @param DOMElement $root
180 // @codingStandardsIgnoreStart
181 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
183 // @codingStandardsIgnoreEnd
184 $authors = $this->container->getAuthors();
185 if ((! $authors || empty($authors))) {
187 * This will actually trigger an Exception at the feed level if
188 * a feed level author is not set.
192 foreach ($authors as $data) {
193 $author = $this->dom->createElement('author');
194 $name = $this->dom->createElement('name');
195 $author->appendChild($name);
196 $root->appendChild($author);
197 $text = $dom->createTextNode($data['name']);
198 $name->appendChild($text);
199 if (array_key_exists('email', $data)) {
200 $email = $this->dom->createElement('email');
201 $author->appendChild($email);
202 $text = $dom->createTextNode($data['email']);
203 $email->appendChild($text);
205 if (array_key_exists('uri', $data)) {
206 $uri = $this->dom->createElement('uri');
207 $author->appendChild($uri);
208 $text = $dom->createTextNode($data['uri']);
209 $uri->appendChild($text);
215 * Set entry enclosure
217 * @param DOMDocument $dom
218 * @param DOMElement $root
221 // @codingStandardsIgnoreStart
222 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
224 // @codingStandardsIgnoreEnd
225 $data = $this->container->getEnclosure();
226 if ((! $data || empty($data))) {
229 $enclosure = $this->dom->createElement('link');
230 $enclosure->setAttribute('rel', 'enclosure');
231 if (isset($data['type'])) {
232 $enclosure->setAttribute('type', $data['type']);
234 if (isset($data['length'])) {
235 $enclosure->setAttribute('length', $data['length']);
237 $enclosure->setAttribute('href', $data['uri']);
238 $root->appendChild($enclosure);
241 // @codingStandardsIgnoreStart
242 protected function _setLink(DOMDocument $dom, DOMElement $root)
244 // @codingStandardsIgnoreEnd
245 if (! $this->getDataContainer()->getLink()) {
248 $link = $dom->createElement('link');
249 $root->appendChild($link);
250 $link->setAttribute('rel', 'alternate');
251 $link->setAttribute('type', 'text/html');
252 $link->setAttribute('href', $this->getDataContainer()->getLink());
256 * Set entry identifier
258 * @param DOMDocument $dom
259 * @param DOMElement $root
261 * @throws Writer\Exception\InvalidArgumentException
263 // @codingStandardsIgnoreStart
264 protected function _setId(DOMDocument $dom, DOMElement $root)
266 // @codingStandardsIgnoreEnd
267 if (! $this->getDataContainer()->getId()
268 && ! $this->getDataContainer()->getLink()) {
269 $message = 'Atom 1.0 entry elements MUST contain exactly one '
270 . 'atom:id element, or as an alternative, we can use the same '
271 . 'value as atom:link however neither a suitable link nor an '
272 . 'id have been set';
273 $exception = new Writer\Exception\InvalidArgumentException($message);
274 if (! $this->ignoreExceptions) {
277 $this->exceptions[] = $exception;
282 if (! $this->getDataContainer()->getId()) {
283 $this->getDataContainer()->setId(
284 $this->getDataContainer()->getLink()
287 if (! Uri::factory($this->getDataContainer()->getId())->isValid()
289 "#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#",
290 $this->getDataContainer()->getId()
292 && ! $this->_validateTagUri($this->getDataContainer()->getId())
294 throw new Writer\Exception\InvalidArgumentException('Atom 1.0 IDs must be a valid URI/IRI');
296 $id = $dom->createElement('id');
297 $root->appendChild($id);
298 $text = $dom->createTextNode($this->getDataContainer()->getId());
299 $id->appendChild($text);
303 * Validate a URI using the tag scheme (RFC 4151)
308 // @codingStandardsIgnoreStart
309 protected function _validateTagUri($id)
311 // @codingStandardsIgnoreEnd
313 '/^tag:(?P<name>.*),(?P<date>\d{4}-?\d{0,2}-?\d{0,2}):(?P<specific>.*)(.*:)*$/',
318 $date = $matches['date'];
319 $d6 = strtotime($date);
320 if ((strlen($date) == 4) && $date <= date('Y')) {
322 } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
324 } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
327 $validator = new Validator\EmailAddress;
328 if ($validator->isValid($matches['name'])) {
331 $nvalid = $validator->isValid('info@' . $matches['name']);
333 return $dvalid && $nvalid;
341 * @param DOMDocument $dom
342 * @param DOMElement $root
344 * @throws Writer\Exception\InvalidArgumentException
346 // @codingStandardsIgnoreStart
347 protected function _setContent(DOMDocument $dom, DOMElement $root)
349 // @codingStandardsIgnoreEnd
350 $content = $this->getDataContainer()->getContent();
351 if (! $content && ! $this->getDataContainer()->getLink()) {
352 $message = 'Atom 1.0 entry elements MUST contain exactly one '
353 . 'atom:content element, or as an alternative, at least one link '
354 . 'with a rel attribute of "alternate" to indicate an alternate '
355 . 'method to consume the content.';
356 $exception = new Writer\Exception\InvalidArgumentException($message);
357 if (! $this->ignoreExceptions) {
360 $this->exceptions[] = $exception;
367 $element = $dom->createElement('content');
368 $element->setAttribute('type', 'xhtml');
369 $xhtmlElement = $this->_loadXhtml($content);
370 $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
371 $xhtml = $dom->importNode($xhtmlElement, $deep);
372 $element->appendChild($xhtml);
373 $root->appendChild($element);
377 * Load a HTML string and attempt to normalise to XML
379 // @codingStandardsIgnoreStart
380 protected function _loadXhtml($content)
382 // @codingStandardsIgnoreEnd
383 if (class_exists('tidy', false)) {
386 'output-xhtml' => true,
387 'show-body-only' => true,
388 'quote-nbsp' => false
390 $encoding = str_replace('-', '', $this->getEncoding());
391 $tidy->parseString($content, $config, $encoding);
392 $tidy->cleanRepair();
393 $xhtml = (string) $tidy;
397 $xhtml = preg_replace([
398 "/(<[\/]?)([a-zA-Z]+)/"
399 ], '$1xhtml:$2', $xhtml);
400 $dom = new DOMDocument('1.0', $this->getEncoding());
402 '<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
406 return $dom->documentElement;
410 * Set entry categories
412 * @param DOMDocument $dom
413 * @param DOMElement $root
416 // @codingStandardsIgnoreStart
417 protected function _setCategories(DOMDocument $dom, DOMElement $root)
419 // @codingStandardsIgnoreEnd
420 $categories = $this->getDataContainer()->getCategories();
424 foreach ($categories as $cat) {
425 $category = $dom->createElement('category');
426 $category->setAttribute('term', $cat['term']);
427 if (isset($cat['label'])) {
428 $category->setAttribute('label', $cat['label']);
430 $category->setAttribute('label', $cat['term']);
432 if (isset($cat['scheme'])) {
433 $category->setAttribute('scheme', $cat['scheme']);
435 $root->appendChild($category);
440 * Append Source element (Atom 1.0 Feed Metadata)
442 * @param DOMDocument $dom
443 * @param DOMElement $root
446 // @codingStandardsIgnoreStart
447 protected function _setSource(DOMDocument $dom, DOMElement $root)
449 // @codingStandardsIgnoreEnd
450 $source = $this->getDataContainer()->getSource();
454 $renderer = new Renderer\Feed\AtomSource($source);
455 $renderer->setType($this->getType());
456 $element = $renderer->render()->getElement();
457 $imported = $dom->importNode($element, true);
458 $root->appendChild($imported);