miniflux-legacy/vendor/PicoFeed/Writers/Atom.php

149 lines
4.4 KiB
PHP
Raw Normal View History

2013-06-29 03:50:15 +02:00
<?php
namespace PicoFeed\Writers;
require_once __DIR__.'/../Writer.php';
class Atom extends \PicoFeed\Writer
{
private $required_feed_properties = array(
2013-06-29 03:50:15 +02:00
'title',
'site_url',
'feed_url',
);
private $required_item_properties = array(
'title',
'url',
2013-06-29 03:50:15 +02:00
);
public function execute($filename = '')
{
$this->checkRequiredProperties($this->required_feed_properties, $this);
2013-06-29 03:50:15 +02:00
$this->dom = new \DomDocument('1.0', 'UTF-8');
$this->dom->formatOutput = true;
2013-06-29 03:50:15 +02:00
// <feed/>
$feed = $this->dom->createElement('feed');
2013-06-29 03:50:15 +02:00
$feed->setAttributeNodeNS(new \DomAttr('xmlns', 'http://www.w3.org/2005/Atom'));
// <generator/>
$generator = $this->dom->createElement('generator', 'PicoFeed');
2013-06-29 03:50:15 +02:00
$generator->setAttribute('url', 'https://github.com/fguillot/picoFeed');
$feed->appendChild($generator);
// <title/>
$feed->appendChild($this->dom->createElement('title', $this->title));
// <id/>
$feed->appendChild($this->dom->createElement('id', $this->site_url));
2013-06-29 03:50:15 +02:00
// <updated/>
$this->addUpdated($feed, isset($this->updated) ? $this->updated : '');
2013-06-29 03:50:15 +02:00
// <link rel="alternate" type="text/html" href="http://example.org/"/>
$this->addLink($feed, $this->site_url);
2013-06-29 03:50:15 +02:00
// <link rel="self" type="application/atom+xml" href="http://example.org/feed.atom"/>
$this->addLink($feed, $this->feed_url, 'self', 'application/atom+xml');
2013-06-29 03:50:15 +02:00
// <author/>
if (isset($this->author)) $this->addAuthor($feed, $this->author);
2013-06-29 03:50:15 +02:00
// <entry/>
foreach ($this->items as $item) {
$this->checkRequiredProperties($this->required_item_properties, $item);
$entry = $this->dom->createElement('entry');
2013-06-29 03:50:15 +02:00
// <title/>
$entry->appendChild($this->dom->createElement('title', $item['title']));
// <id/>
$entry->appendChild($this->dom->createElement('id', $item['url']));
2013-06-29 03:50:15 +02:00
// <updated/>
$this->addUpdated($entry, isset($item['updated']) ? $item['updated'] : '');
2013-06-29 03:50:15 +02:00
// <published/>
if (isset($item['published'])) {
$entry->appendChild($this->dom->createElement('published', date(DATE_ATOM, $item['published'])));
2013-06-29 03:50:15 +02:00
}
// <link rel="alternate" type="text/html" href="http://example.org/"/>
$this->addLink($entry, $item['url']);
2013-06-29 03:50:15 +02:00
// <summary/>
if (isset($item['summary'])) {
$entry->appendChild($this->dom->createElement('summary', $item['summary']));
2013-06-29 03:50:15 +02:00
}
// <content/>
if (isset($item['content'])) {
$content = $this->dom->createElement('content');
2013-06-29 03:50:15 +02:00
$content->setAttribute('type', 'html');
$content->appendChild($this->dom->createCDATASection($item['content']));
2013-06-29 03:50:15 +02:00
$entry->appendChild($content);
}
// <author/>
if (isset($item['author'])) $this->addAuthor($entry, $item['author']);
2013-06-29 03:50:15 +02:00
$feed->appendChild($entry);
}
$this->dom->appendChild($feed);
2013-06-29 03:50:15 +02:00
if ($filename) {
$this->dom->save($filename);
2013-06-29 03:50:15 +02:00
}
else {
return $this->dom->saveXML();
}
}
public function addLink($xml, $url, $rel = 'alternate', $type = 'text/html')
{
$link = $this->dom->createElement('link');
$link->setAttribute('rel', $rel);
$link->setAttribute('type', $type);
$link->setAttribute('href', $url);
$xml->appendChild($link);
}
public function addUpdated($xml, $value = '')
{
$xml->appendChild($this->dom->createElement(
'updated',
date(DATE_ATOM, $value ?: time())
));
}
public function addAuthor($xml, array $values)
{
$author = $this->dom->createElement('author');
if (isset($values['name'])) {
$name = $this->dom->createElement('name', $values['name']);
$author->appendChild($name);
}
if (isset($values['email'])) {
$email = $this->dom->createElement('email', $values['email']);
$author->appendChild($email);
}
if (isset($values['url'])) {
$uri = $this->dom->createElement('uri', $values['url']);
$author->appendChild($uri);
2013-06-29 03:50:15 +02:00
}
$xml->appendChild($author);
2013-06-29 03:50:15 +02:00
}
}