Show
Ignore:
Timestamp:
07/01/2008 01:38:34 PM (5 months ago)
Author:
Shadowhand
Message:

Added initial feed::create($info, $items, $format) method. Only supports RSS 2.0, for now.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/feed.php

    r2373 r2954  
    5454        } 
    5555 
    56 } // End rss 
     56        /** 
     57         * Creates a feed from the given parameters. 
     58         * 
     59         * @param   array   feed information 
     60         * @param   array   items to add to the feed 
     61         * @return  string 
     62         */ 
     63        public static function create($info, $items, $format = 'rss2') 
     64        { 
     65                $info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP'); 
     66 
     67                $feed = '<?xml version="1.0"?><rss version="2.0"><channel></channel></rss>'; 
     68                $feed = simplexml_load_string($feed); 
     69 
     70                foreach ($info as $name => $value) 
     71                { 
     72                        if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value))) 
     73                        { 
     74                                // Convert timestamps to RFC 822 formatted dates 
     75                                $value = date(DATE_RFC822, $value); 
     76                        } 
     77                        elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE) 
     78                        { 
     79                                // Convert URIs to URLs 
     80                                $value = url::site($value, 'http'); 
     81                        } 
     82 
     83                        // Add the info to the channel 
     84                        $feed->channel->addChild($name, $value); 
     85                } 
     86 
     87                foreach ($items as $item) 
     88                { 
     89                        // Add the item to the channel 
     90                        $row = $feed->channel->addChild('item'); 
     91 
     92                        foreach ($item as $name => $value) 
     93                        { 
     94                                if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value))) 
     95                                { 
     96                                        // Convert timestamps to RFC 822 formatted dates 
     97                                        $value = date(DATE_RFC822, $value); 
     98                                } 
     99                                elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE) 
     100                                { 
     101                                        // Convert URIs to URLs 
     102                                        $value = url::site($value, 'http'); 
     103                                } 
     104 
     105                                // Add the info to the row 
     106                                $row->addChild($name, $value); 
     107                        } 
     108                } 
     109 
     110                return $feed->asXML(); 
     111        } 
     112 
     113} // End feed