From 655b3d578dff983bb16338c227fec7b5a9cbbe53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Mon, 12 Sep 2016 10:38:50 +0200 Subject: [PATCH] [FeedExpander] simplify feed type detection and store it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pierre Mazière --- lib/FeedExpander.php | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/FeedExpander.php b/lib/FeedExpander.php index f0ec4c6..7e9c50d 100644 --- a/lib/FeedExpander.php +++ b/lib/FeedExpander.php @@ -5,6 +5,7 @@ abstract class FeedExpander extends BridgeAbstract { private $name; private $uri; private $description; + private $feedType; public function collectExpandableDatas($url, $maxItems = -1){ if(empty($url)){ @@ -21,22 +22,26 @@ abstract class FeedExpander extends BridgeAbstract { $rssContent = simplexml_load_string($content); $this->debugMessage('Detecting feed format/version'); - if(isset($rssContent->channel[0])){ - $this->debugMessage('Detected RSS format'); - if(isset($rssContent->item[0])){ - $this->debugMessage('Detected RSS 1.0 format'); - $this->collect_RSS_1_0_data($rssContent, $maxItems); - } else { - $this->debugMessage('Detected RSS 0.9x or 2.0 format'); - $this->collect_RSS_2_0_data($rssContent, $maxItems); - } - } elseif(isset($rssContent->entry[0])){ + switch(true){ + case isset($rssContent->item[0]): + $this->debugMessage('Detected RSS 1.0 format'); + $this->feedType="RSS_1_0"; + break; + case isset($rssContent->channel[0]): + $this->debugMessage('Detected RSS 0.9x or 2.0 format'); + $this->feedType="RSS_2_0"; + break; + case isset($rssContent->entry[0]): $this->debugMessage('Detected ATOM format'); - $this->collect_ATOM_data($rssContent, $maxItems); - } else { + $this->feedType="ATOM_1_0"; + break; + default: $this->debugMessage('Unknown feed format/version'); $this->returnServerError('The feed format is unknown!'); + break; } + + $this->{'collect_'.$this->feedType.'_data'}($rssContent,$maxItems); } protected function collect_RSS_1_0_data($rssContent, $maxItems){ @@ -62,7 +67,7 @@ abstract class FeedExpander extends BridgeAbstract { } } - protected function collect_ATOM_data($content, $maxItems){ + protected function collect_ATOM_1_0_data($content, $maxItems){ $this->load_ATOM_feed_data($content); foreach($content->entry as $item){ $this->debugMessage('parsing item ' . var_export($item, true));