diff options
Diffstat (limited to 'vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php')
-rw-r--r-- | vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php | 89 |
1 files changed, 62 insertions, 27 deletions
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php index 1217bc4b0..5bb930b22 100644 --- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php +++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php @@ -30,19 +30,31 @@ class Atom extends Parser * Find the feed url * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedUrl(SimpleXMLElement $xml, Feed $feed) { - $feed->url = $this->getLink($xml); + $feed->feed_url = $this->getUrl($xml, 'self'); + } + + /** + * Find the site url + * + * @access public + * @param SimpleXMLElement $xml Feed xml + * @param \PicoFeed\Parser\Feed $feed Feed object + */ + public function findSiteUrl(SimpleXMLElement $xml, Feed $feed) + { + $feed->site_url = $this->getUrl($xml, 'alternate', true); } /** * Find the feed description * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedDescription(SimpleXMLElement $xml, Feed $feed) @@ -54,7 +66,7 @@ class Atom extends Parser * Find the feed logo url * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedLogo(SimpleXMLElement $xml, Feed $feed) @@ -66,19 +78,19 @@ class Atom extends Parser * Find the feed title * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedTitle(SimpleXMLElement $xml, Feed $feed) { - $feed->title = Filter::stripWhiteSpace((string) $xml->title) ?: $feed->url; + $feed->title = Filter::stripWhiteSpace((string) $xml->title) ?: $feed->getSiteUrl(); } /** * Find the feed language * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed) @@ -90,7 +102,7 @@ class Atom extends Parser * Find the feed id * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedId(SimpleXMLElement $xml, Feed $feed) @@ -102,7 +114,7 @@ class Atom extends Parser * Find the feed date * * @access public - * @param SimpleXMLElement $xml Feed xml + * @param SimpleXMLElement $xml Feed xml * @param \PicoFeed\Parser\Feed $feed Feed object */ public function findFeedDate(SimpleXMLElement $xml, Feed $feed) @@ -115,7 +127,7 @@ class Atom extends Parser * * @access public * @param SimpleXMLElement $entry Feed item - * @param Item $item Item object + * @param Item $item Item object */ public function findItemDate(SimpleXMLElement $entry, Item $item) { @@ -145,8 +157,8 @@ class Atom extends Parser * Find the item author * * @access public - * @param SimpleXMLElement $xml Feed - * @param SimpleXMLElement $entry Feed item + * @param SimpleXMLElement $xml Feed + * @param SimpleXMLElement $entry Feed item * @param \PicoFeed\Parser\Item $item Item object */ public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item) @@ -180,7 +192,7 @@ class Atom extends Parser */ public function findItemUrl(SimpleXMLElement $entry, Item $item) { - $item->url = $this->getLink($entry); + $item->url = $this->getUrl($entry, 'alternate'); } /** @@ -215,13 +227,11 @@ class Atom extends Parser */ public function findItemEnclosure(SimpleXMLElement $entry, Item $item, Feed $feed) { - foreach ($entry->link as $link) { - if ((string) $link['rel'] === 'enclosure') { + $enclosure = $this->findLink($entry, 'enclosure'); - $item->enclosure_url = Url::resolve((string) $link['href'], $feed->url); - $item->enclosure_type = (string) $link['type']; - break; - } + if ($enclosure) { + $item->enclosure_url = Url::resolve((string) $enclosure['href'], $feed->getSiteUrl()); + $item->enclosure_type = (string) $enclosure['type']; } } @@ -241,29 +251,54 @@ class Atom extends Parser /** * Get the URL from a link tag * - * @access public - * @param SimpleXMLElement $xml XML tag + * @access private + * @param SimpleXMLElement $xml XML tag + * @param string $rel Link relationship: alternate, enclosure, related, self, via * @return string */ - public function getLink(SimpleXMLElement $xml) + private function getUrl(SimpleXMLElement $xml, $rel, $fallback = false) + { + $link = $this->findLink($xml, $rel); + + if ($link) { + return (string) $link['href']; + } + + if ($fallback) { + $link = $this->findLink($xml, ''); + return $link ? (string) $link['href'] : ''; + } + + return ''; + } + + /** + * Get a link tag that match a relationship + * + * @access private + * @param SimpleXMLElement $xml XML tag + * @param string $rel Link relationship: alternate, enclosure, related, self, via + * @return SimpleXMLElement|null + */ + private function findLink(SimpleXMLElement $xml, $rel) { foreach ($xml->link as $link) { - if ((string) $link['type'] === 'text/html' || (string) $link['type'] === 'application/xhtml+xml') { - return (string) $link['href']; + if (empty($rel) || $rel === (string) $link['rel']) { + return $link; } } - return (string) $xml->link['href']; + return null; } /** * Get the entry content * - * @access public + * @access private * @param SimpleXMLElement $entry XML Entry * @return string */ - public function getContent(SimpleXMLElement $entry) + private function getContent(SimpleXMLElement $entry) { if (isset($entry->content) && ! empty($entry->content)) { |