Skip to content

Commit

Permalink
yup
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Oct 19, 2023
1 parent 689cfa8 commit f597f0e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 58 deletions.
115 changes: 66 additions & 49 deletions formats/MrssFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ class MrssFormat extends FormatAbstract
public function stringify()
{
$document = new \DomDocument('1.0', $this->getCharset());

$feedUrl = get_current_url();
$feedArray = $this->getFeed();

$uri = $feedArray['uri'];

$document->formatOutput = true;

$feed = $document->createElement('rss');
$document->appendChild($feed);
$feed->setAttribute('version', '2.0');
Expand All @@ -51,51 +46,73 @@ public function stringify()
$channel = $document->createElement('channel');
$feed->appendChild($channel);

$feedArray = $this->getFeed();
$uri = $feedArray['uri'];
$title = $feedArray['name'];
$channelTitle = $document->createElement('title');
$channel->appendChild($channelTitle);
$channelTitle->appendChild($document->createTextNode($title));

$link = $document->createElement('link');
$channel->appendChild($link);
$link->appendChild($document->createTextNode($uri));

$description = $document->createElement('description');
$channel->appendChild($description);
$description->appendChild($document->createTextNode($title));

$allowedIconExtensions = [
'.gif',
'.jpg',
'.png',
'.ico',
];
$icon = $feedArray['icon'];
if ($icon && in_array(substr($icon, -4), $allowedIconExtensions)) {
$feedImage = $document->createElement('image');
$channel->appendChild($feedImage);
$iconUrl = $document->createElement('url');
$iconUrl->appendChild($document->createTextNode($icon));
$feedImage->appendChild($iconUrl);
$iconTitle = $document->createElement('title');
$iconTitle->appendChild($document->createTextNode($title));
$feedImage->appendChild($iconTitle);
$iconLink = $document->createElement('link');
$iconLink->appendChild($document->createTextNode($uri));
$feedImage->appendChild($iconLink);
}

$linkAlternate = $document->createElementNS(self::ATOM_NS, 'link');
$channel->appendChild($linkAlternate);
$linkAlternate->setAttribute('rel', 'alternate');
$linkAlternate->setAttribute('type', 'text/html');
$linkAlternate->setAttribute('href', $uri);

$linkSelf = $document->createElementNS(self::ATOM_NS, 'link');
$channel->appendChild($linkSelf);
$linkSelf->setAttribute('rel', 'self');
$linkSelf->setAttribute('type', 'application/atom+xml');
$linkSelf->setAttribute('href', $feedUrl);
foreach ($feedArray as $feedKey => $feedValue) {
if (in_array($feedKey, ['atom', 'donationUri'])) {
continue;
}
if ($feedKey === 'name') {
$channelTitle = $document->createElement('title');
$channel->appendChild($channelTitle);
$channelTitle->appendChild($document->createTextNode($title));

$description = $document->createElement('description');
$channel->appendChild($description);
$description->appendChild($document->createTextNode($title));
} elseif ($feedKey === 'uri') {
$link = $document->createElement('link');
$channel->appendChild($link);
$link->appendChild($document->createTextNode($uri));

$linkAlternate = $document->createElementNS(self::ATOM_NS, 'link');
$channel->appendChild($linkAlternate);
$linkAlternate->setAttribute('rel', 'alternate');
$linkAlternate->setAttribute('type', 'text/html');
$linkAlternate->setAttribute('href', $uri);

$linkSelf = $document->createElementNS(self::ATOM_NS, 'link');
$channel->appendChild($linkSelf);
$linkSelf->setAttribute('rel', 'self');
$linkSelf->setAttribute('type', 'application/atom+xml');
$feedUrl = get_current_url();
$linkSelf->setAttribute('href', $feedUrl);
} elseif ($feedKey === 'icon') {
$allowedIconExtensions = [
'.gif',
'.jpg',
'.png',
'.ico',
];
$icon = $feedValue;
if ($icon && in_array(substr($icon, -4), $allowedIconExtensions)) {
$feedImage = $document->createElement('image');
$channel->appendChild($feedImage);
$iconUrl = $document->createElement('url');
$iconUrl->appendChild($document->createTextNode($icon));
$feedImage->appendChild($iconUrl);
$iconTitle = $document->createElement('title');
$iconTitle->appendChild($document->createTextNode($title));
$feedImage->appendChild($iconTitle);
$iconLink = $document->createElement('link');
$iconLink->appendChild($document->createTextNode($uri));
$feedImage->appendChild($iconLink);
}
} elseif ($feedKey === 'itunes') {
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:itunes', self::ITUNES_NS);
foreach ($feedValue as $itunesKey => $itunesValue) {
$itunesProperty = $document->createElementNS(self::ITUNES_NS, $itunesKey);
$channel->appendChild($itunesProperty);
$itunesProperty->appendChild($document->createTextNode($itunesValue));
}
} else {
$channelTitle = $document->createElement($feedKey);
$channel->appendChild($channelTitle);
$channelTitle->appendChild($document->createTextNode($feedValue));
}
}

foreach ($this->getItems() as $item) {
$itemArray = $item->toArray();
Expand Down
2 changes: 1 addition & 1 deletion lib/BridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
abstract class BridgeAbstract
{
const NAME = 'Unnamed bridge';
const URI = null;
const URI = '';
const DONATION_URI = '';
const DESCRIPTION = 'No description provided';
const MAINTAINER = 'No maintainer';
Expand Down
6 changes: 3 additions & 3 deletions tests/Formats/samples/expectedMrssFormat/feed.common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Sample feed with common data</title>
<link>https://example.com/blog/</link>
<description>Sample feed with common data</description>
<link>https://example.com/blog/</link>
<atom:link href="https://example.com/blog/" rel="alternate" type="text/html"/>
<atom:link href="https://example.com/feed?type=common&amp;items=4" rel="self" type="application/atom+xml"/>
<image>
<url>https://example.com/logo.png</url>
<title>Sample feed with common data</title>
<link>https://example.com/blog/</link>
</image>
<atom:link href="https://example.com/blog/" rel="alternate" type="text/html"/>
<atom:link href="https://example.com/feed?type=common&amp;items=4" rel="self" type="application/atom+xml"/>

<item>
<title>Test Entry</title>
Expand Down
2 changes: 1 addition & 1 deletion tests/Formats/samples/expectedMrssFormat/feed.empty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Sample feed with minimum data</title>
<link>https://github.com/RSS-Bridge/rss-bridge/</link>
<description>Sample feed with minimum data</description>
<link>https://github.com/RSS-Bridge/rss-bridge/</link>
<atom:link href="https://github.com/RSS-Bridge/rss-bridge/" rel="alternate" type="text/html"/>
<atom:link href="https://example.com/feed" rel="self" type="application/atom+xml"/>
</channel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Sample feed with minimum data</title>
<link>https://github.com/RSS-Bridge/rss-bridge/</link>
<description>Sample feed with minimum data</description>
<link>https://github.com/RSS-Bridge/rss-bridge/</link>
<atom:link href="https://github.com/RSS-Bridge/rss-bridge/" rel="alternate" type="text/html"/>
<atom:link href="https://example.com/feed" rel="self" type="application/atom+xml"/>

Expand Down
6 changes: 3 additions & 3 deletions tests/Formats/samples/expectedMrssFormat/feed.microblog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Sample microblog feed</title>
<link>https://example.com/blog/</link>
<description>Sample microblog feed</description>
<link>https://example.com/blog/</link>
<atom:link href="https://example.com/blog/" rel="alternate" type="text/html"/>
<atom:link href="https://example.com/feed" rel="self" type="application/atom+xml"/>
<image>
<url>https://example.com/logo.png</url>
<title>Sample microblog feed</title>
<link>https://example.com/blog/</link>
</image>
<atom:link href="https://example.com/blog/" rel="alternate" type="text/html"/>
<atom:link href="https://example.com/feed" rel="self" type="application/atom+xml"/>

<item>
<guid isPermaLink="false">1918f084648b82057c1dd3faa3d091da82a6fac2</guid>
Expand Down

0 comments on commit f597f0e

Please sign in to comment.