RssItemWorker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Dmitry A. Lomash, Dmitry E. Demidov             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class RssItemWorker extends Singleton implements FeedItemWorker
00016     {
00020         public static function me()
00021         {
00022             return Singleton::getInstance(__CLASS__);
00023         }
00024         
00025         public function makeItems(SimpleXMLElement $xmlFeed)
00026         {
00027             $result = array();
00028             
00029             if (isset($xmlFeed->channel->item)) {
00030                 foreach ($xmlFeed->channel->item as $item) {
00031                     $feedItem =
00032                         FeedItem::create((string) $item->title)->
00033                         setContent(
00034                             FeedItemContent::create()->
00035                             setBody((string) $item->description)
00036                         )->
00037                         setPublished(
00038                             Timestamp::create(
00039                                 strtotime((string) $item->pubDate)
00040                             )
00041                         )->
00042                         setLink((string) $item->link);
00043                     
00044                     if (isset($item->guid))
00045                         $feedItem->setId($item->guid);
00046                     
00047                     if (isset($item->category))
00048                         $feedItem->setCategory((string) $item->category);
00049                     
00050                     $result[] = $feedItem;
00051                 }
00052             }
00053             
00054             return $result;
00055         }
00056         
00057         public function toXml(FeedItem $item)
00058         {
00059             return
00060                 '<item>'
00061                     .(
00062                         $item->getPublished()
00063                             ?
00064                                 '<pubDate>'
00065                                     .date('r', $item->getPublished()->toStamp())
00066                                 .'</pubDate>'
00067                             : null
00068                     )
00069                     .(
00070                         $item->getId()
00071                             ?
00072                                 '<guid isPermaLink="false">'
00073                                     .$item->getId()
00074                                 .'</guid>'
00075                             : null
00076                     )
00077                     .'<title>'.$item->getTitle().'</title>'
00078                     .(
00079                         $item->getLink()
00080                             ?
00081                                 '<link>'
00082                                 .str_replace("&", "&amp;", $item->getLink())
00083                                 .'</link>'
00084                             : null
00085                     )
00086                     .(
00087                         $item->getSummary()
00088                             ? '<description>'.$item->getSummary().'</description>'
00089                             : null
00090                     )
00091                     .(
00092                         $item->getCategory()
00093                             ? '<category>'.$item->getCategory().'</category>'
00094                             : null
00095                     )
00096                 .'</item>';
00097         }
00098     }
00099 ?>