misuzu/src/Feeds/RssFeedSerializer.php

86 lines
3.2 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu\Feeds;
use DOMDocument;
use DOMElement;
use DOMNode;
2022-09-13 13:14:49 +00:00
class RssFeedSerializer extends XmlFeedSerializer {
protected function formatTime(int $time): string {
return date('r', $time);
}
protected function createRoot(DOMDocument $document, Feed $feed): DOMNode {
2022-09-13 13:14:49 +00:00
$rss = $document->appendChild($document->createElement('rss'));
if($rss instanceof DOMElement) {
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
}
2022-09-13 13:14:49 +00:00
$channel = $rss->appendChild($document->createElement('channel'));
$channel->appendChild($document->createElement('ttl', '900'));
return $channel;
}
protected function createTitle(DOMDocument $document, string $title): DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('title', $this->cleanString($title));
}
protected function createDescription(DOMDocument $document, string $description): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('description', $this->cleanString($description));
}
protected function createLastUpdate(DOMDocument $document, int $lastUpdate): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('pubDate', $this->formatTime($lastUpdate));
}
protected function createContentUrl(DOMDocument $document, string $contentUrl): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('link', $this->cleanString($contentUrl));
}
protected function createFeedUrl(DOMDocument $document, string $feedUrl): ?DOMNode {
2022-09-13 13:14:49 +00:00
$link = $document->createElement('atom:link');
$link->setAttribute('href', $this->cleanString($feedUrl));
$link->setAttribute('ref', 'self');
return $link;
}
protected function createItem(DOMDocument $document, FeedItem $feedItem): DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('item');
}
protected function createItemTitle(DOMDocument $document, string $title): DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('title', $this->cleanString($title));
}
protected function createItemSummary(DOMDocument $document, string $summary): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('description', $this->cleanString($summary));
}
protected function createItemContent(DOMDocument $document, string $content): ?DOMNode {
2022-09-13 13:14:49 +00:00
return null;
}
protected function createItemCreationDate(DOMDocument $document, int $creationDate): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('pubDate', $this->formatTime($creationDate));
}
protected function createItemUniqueId(DOMDocument $document, string $uniqueId): ?DOMNode {
2022-09-13 13:14:49 +00:00
$elem = $document->createElement('guid', $uniqueId);
$elem->setAttribute('isPermaLink', 'true');
return $elem;
}
protected function createItemContentUrl(DOMDocument $document, string $contentUrl): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('link', $contentUrl);
}
protected function createItemCommentsUrl(DOMDocument $document, string $commentsUrl): ?DOMNode {
2022-09-13 13:14:49 +00:00
return $document->createElement('comments', $commentsUrl);
}
protected function createItemAuthor(DOMDocument $document, ?string $authorName, ?string $authorUrl): ?DOMNode {
2022-09-13 13:14:49 +00:00
return null;
}
}