misuzu/src/Feeds/AtomFeedSerializer.php

117 lines
4 KiB
PHP

<?php
namespace Misuzu\Feeds;
use DOMDocument;
use DOMElement;
use DOMNode;
class AtomFeedSerializer extends XmlFeedSerializer {
protected function formatTime(int $time): string {
return date('c', $time);
}
protected function createRoot(DOMDocument $document, Feed $feed): DOMNode {
$atom = $document->appendChild($document->createElement('feed'));
if($atom instanceof DOMElement)
$atom->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
$atom->appendChild(
$document->createElement(
'id',
$feed->hasContentUrl()
? $this->cleanString($feed->getContentUrl())
: time()
)
);
return $atom;
}
protected function createTitle(DOMDocument $document, string $title): DOMNode {
return $document->createElement('title', $this->cleanString($title));
}
protected function createDescription(DOMDocument $document, string $description): ?DOMNode {
return $document->createElement('subtitle', $this->cleanString($description));
}
protected function createLastUpdate(DOMDocument $document, int $lastUpdate): ?DOMNode {
return $document->createElement('updated', $this->formatTime($lastUpdate));
}
protected function createContentUrl(DOMDocument $document, string $contentUrl): ?DOMNode {
$link = $document->createElement('link');
$link->setAttribute('href', $this->cleanString($contentUrl));
return $link;
}
protected function createFeedUrl(DOMDocument $document, string $feedUrl): ?DOMNode {
$link = $document->createElement('link');
$link->setAttribute('href', $this->cleanString($feedUrl));
$link->setAttribute('ref', 'self');
return $link;
}
protected function createItem(DOMDocument $document, FeedItem $feedItem): DOMNode {
$elem = $document->createElement('entry');
$elem->appendChild(
$document->createElement(
'id',
$feedItem->hasContentUrl()
? $this->cleanString($feedItem->getContentUrl())
: time()
)
);
return $elem;
}
protected function createItemTitle(DOMDocument $document, string $title): DOMNode {
return $document->createElement('title', $this->cleanString($title));
}
protected function createItemSummary(DOMDocument $document, string $summary): ?DOMNode {
return $document->createElement('summary', $this->cleanString($summary));
}
protected function createItemContent(DOMDocument $document, string $content): ?DOMNode {
$elem = $document->createElement('content', $this->cleanString($content));
$elem->setAttribute('type', 'html');
return $elem;
}
protected function createItemCreationDate(DOMDocument $document, int $creationDate): ?DOMNode {
return $document->createElement('updated', $this->formatTime($creationDate));
}
protected function createItemUniqueId(DOMDocument $document, string $uniqueId): ?DOMNode {
return null;
}
protected function createItemContentUrl(DOMDocument $document, string $contentUrl): ?DOMNode {
$elem = $document->createElement('link');
$elem->setAttribute('href', $this->cleanString($contentUrl));
$elem->setAttribute('type', 'text/html');
return $elem;
}
protected function createItemCommentsUrl(DOMDocument $document, string $commentsUrl): ?DOMNode {
return null;
}
protected function createItemAuthor(DOMDocument $document, ?string $authorName, ?string $authorUrl): ?DOMNode {
if(empty($authorName) && empty($authorUrl))
return null;
$elem = $document->createElement('author');
if(!empty($authorName))
$elem->appendChild($document->createElement('name', $this->cleanString($authorName)));
if(!empty($authorUrl))
$elem->appendChild($document->createElement('uri', $this->cleanString($authorUrl)));
return $elem;
}
}