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