Skip to content

Commit 106bd43

Browse files
alek13dg
authored andcommitted
Unify link tag for RSS & Atom into $url property (#20)
In rss we have tag <link>https://example.com/articles/some-amazing-article</link>, but in Atom we have <link href="https://example.com/articles/some-amazing-article"/>. So, lets extract it from href attr & put into `url` property.
1 parent 18f00ab commit 106bd43

4 files changed

Lines changed: 8 additions & 6 deletions

File tree

example-atom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<h1><?php echo htmlspecialchars($atom->title) ?></h1>
1616

1717
<?php foreach ($atom->entry as $entry): ?>
18-
<h2><a href="<?php echo htmlspecialchars($entry->link['href']) ?>"><?php echo htmlspecialchars($entry->title) ?></a>
18+
<h2><a href="<?php echo htmlspecialchars($entry->url) ?>"><?php echo htmlspecialchars($entry->title) ?></a>
1919
<small><?php echo date('j.n.Y H:i', (int) $entry->timestamp) ?></small></h2>
2020

2121
<?php if ($entry->content['type'] == 'html'): ?>

example-rss.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<p><i><?php echo htmlspecialchars($rss->description) ?></i></p>
1717

1818
<?php foreach ($rss->item as $item): ?>
19-
<h2><a href="<?php echo htmlspecialchars($item->link) ?>"><?php echo htmlspecialchars($item->title) ?></a>
19+
<h2><a href="<?php echo htmlspecialchars($item->url) ?>"><?php echo htmlspecialchars($item->title) ?></a>
2020
<small><?php echo date('j.n.Y H:i', (int) $item->timestamp) ?></small></h2>
2121

2222
<?php if (isset($item->{'content:encoded'})): ?>

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ the information from the channel is easy:
3030
```php
3131
echo 'Title: ', $rss->title;
3232
echo 'Description: ', $rss->description;
33-
echo 'Link: ', $rss->link;
33+
echo 'Link: ', $rss->url;
3434

3535
foreach ($rss->item as $item) {
3636
echo 'Title: ', $item->title;
37-
echo 'Link: ', $item->link;
37+
echo 'Link: ', $item->url;
3838
echo 'Timestamp: ', $item->timestamp;
3939
echo 'Description ', $item->description;
4040
echo 'HTML encoded content: ', $item->{'content:encoded'};

src/Feed.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ private static function fromRss(SimpleXMLElement $xml)
8181
// converts namespaces to dotted tags
8282
self::adjustNamespaces($item);
8383

84-
// generate 'timestamp' tag
84+
// generate 'url' & 'timestamp' tags
85+
$item->url = (string) $item->link;
8586
if (isset($item->{'dc:date'})) {
8687
$item->timestamp = strtotime($item->{'dc:date'});
8788
} elseif (isset($item->pubDate)) {
@@ -102,8 +103,9 @@ private static function fromAtom(SimpleXMLElement $xml)
102103
throw new FeedException('Invalid feed.');
103104
}
104105

105-
// generate 'timestamp' tag
106+
// generate 'url' & 'timestamp' tags
106107
foreach ($xml->entry as $entry) {
108+
$entry->url = (string) $entry->link['href'];
107109
$entry->timestamp = strtotime($entry->updated);
108110
}
109111
$feed = new self;

0 commit comments

Comments
 (0)