<?php
-/* Feed.class.php
- * Copyright (C) 2013 Jan Altensen (Stricted)
- * http://stricted.de/
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-class FeedClass {
+/**
+* @author Jan Altensen (Stricted)
+* @copyright 2013-2014 Jan Altensen (Stricted)
+* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+*/
+class Feed {
+ /**
+ * Constructs a new instance of Feed class.
+ *
+ * @param string $url
+ * @return array
+ */
public function __construct($url) {
- $file = file_get_contents($url);
- $xml = simplexml_load_string($file);
- $type = $this->getFeedType($xml);
- $data = null;
- switch($type) {
- case 'rss':
- $data = $this->parseRSSFeed($xml);
- break;
- case 'atom':
- $data = $this->parseAtomFeed($xml);
- break;
- default:
- return null;
- break;
- }
- return $data;
- }
-
- private function parseRSSFeed($xml) {
+ $file = file_get_contents($url);
+ $xml = simplexml_load_string($file);
+ $type = $this->getFeedType($xml);
+ $data = array();
+
+ switch($type) {
+ case 'rss':
+ $data = $this->parseRSSFeed($xml);
+ break;
+
+ case 'atom':
+ $data = $this->parseAtomFeed($xml);
+ break;
+ }
+
+ return $data;
+ }
+
+ /**
+ * parse rss feed
+ *
+ * @param object $xml
+ * @return array
+ */
+ private function parseRSSFeed($xml) {
foreach ($xml->channel->item as $item) {
$item->pubDate = $this->getTime($item->pubDate);
$item->description = html_entity_decode($item->description);
$data[] = $item;
}
return $data;
- }
-
- private function parseAtomFeed($xml) {
+ }
+
+ /**
+ * parse rss feed
+ *
+ * @param object $xml
+ * @return array
+ */
+ private function parseAtomFeed($xml) {
$entries = $xml->children()->entry;
foreach ($entries as $entry) {
$entry->children()->author = $entry->children()->author->name;
$data[] = $entry->children();
}
return $data;
- }
-
- private function getTime($time) {
- return date("d.m.Y H:i:s",intval(strtotime($time)));;
}
- private function getTimeStamp($time) {
- return intval(strtotime($time));;
+ /**
+ * get feed time
+ *
+ * @param string $time
+ * @return integer
+ */
+ private function getTime($time) {
+ return date("d.m.Y H:i:s", $this->getTimeStamp($time));
}
+
+ /**
+ * get feed timestamp
+ *
+ * @param string $time
+ * @return integer
+ */
+ private function getTimeStamp($time) {
+ return intval(strtotime($time));
+ }
+
+ /**
+ * get feed type
+ *
+ * @param object $xml
+ * @return array
+ */
+ public static function getFeedType($xml) {
+ $type = '';
+ if (isset($xml->channel->item)) {
+ $type = 'rss';
+ }
+ else if (isset($xml->entry)) {
+ $type = 'atom';
+ }
- public static function getFeedType($xml) {
- $type = false;
- if (isset($xml->channel->item)) $type = 'rss';
- if (isset($xml->entry)) $type = 'atom';
-
- return $type;
- }
+ return $type;
+ }
}
?>
\ No newline at end of file