YandexRssItemWorker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2010 by Alexandr S. Krotov                              *
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 YandexRssItemWorker 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             $xmlFeed->registerXPathNamespace(
00028                 YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX,
00029                 YandexRssFeedFormat::YANDEX_NAMESPACE_URI
00030             );
00031             
00032             $fullTextList = 
00033                 $xmlFeed->xpath(
00034                     '//'.YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX
00035                     .':full-text'
00036                 );
00037             
00038             $result = array();
00039             
00040             $i = 0;
00041             
00042             if (isset($xmlFeed->channel->item)) {
00043                 foreach ($xmlFeed->channel->item as $item) {
00044                     $feedItem =
00045                         YandexRssFeedItem::create((string) $item->title)->
00046                         setContent(
00047                             FeedItemContent::create()->
00048                             setBody((string) $item->description)
00049                         )->
00050                         setPublished(
00051                             Timestamp::create(
00052                                 strtotime((string) $item->pubDate)
00053                             )
00054                         )->
00055                         setFullText((string) $fullTextList[$i++])->
00056                         setLink((string) $item->link);
00057                     
00058                     if (isset($item->guid))
00059                         $feedItem->setId($item->guid);
00060                     
00061                     if (isset($item->category))
00062                         $feedItem->setCategory((string) $item->category);
00063                     
00064                     $result[] = $feedItem;
00065                 }
00066             }
00067             
00068             return $result;
00069         }
00070         
00071         public function toXml(FeedItem $item)
00072         {
00073             return
00074                 '<item>'
00075                     .(
00076                         $item->getPublished()
00077                             ?
00078                                 '<pubDate>'
00079                                     .date('r', $item->getPublished()->toStamp())
00080                                 .'</pubDate>'
00081                             : null
00082                     )
00083                     .(
00084                         $item->getId()
00085                             ?
00086                                 '<guid isPermaLink="false">'
00087                                     .$item->getId()
00088                                 .'</guid>'
00089                             : null
00090                     )
00091                     .'<title>'.$item->getTitle().'</title>'
00092                     .(
00093                         $item->getLink()
00094                             ?
00095                                 '<link>'
00096                                 .str_replace("&", "&amp;", $item->getLink())
00097                                 .'</link>'
00098                             : null
00099                     )
00100                     .(
00101                         $item->getSummary()
00102                             ? '<description>'.$item->getSummary().'</description>'
00103                             : null
00104                     )
00105                     .(
00106                         $item->getFullText()
00107                             ? (
00108                                 '<yandex:full-text>'
00109                                 .$item->getFullText()
00110                                 .'</yandex:full-text>'
00111                             )
00112                             : null
00113                     )
00114                     .(
00115                         $item->getCategory()
00116                             ? '<category>'.$item->getCategory().'</category>'
00117                             : null
00118                     )
00119                 .'</item>';
00120         }
00121     }
00122 ?>