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 abstract class FeedFormat extends Singleton 00016 { 00017 abstract public function getChannelWorker(); 00018 abstract public function getItemWorker(); 00019 abstract public function isAcceptable(SimpleXMLElement $xmlFeed); 00020 00021 public function parse(SimpleXMLElement $xmlFeed) 00022 { 00023 $this->checkWorkers(); 00024 00025 return 00026 $this->getChannelWorker()-> 00027 makeChannel($xmlFeed)-> 00028 setFeedItems( 00029 $this->getItemWorker()-> 00030 makeItems($xmlFeed) 00031 ); 00032 } 00033 00034 public function toXml(FeedChannel $channel) 00035 { 00036 $this->checkWorkers(); 00037 00038 $itemsXml = null; 00039 $itemWorker = $this->getItemWorker(); 00040 00041 foreach ($channel->getFeedItems() as $feedItem) 00042 $itemsXml .= $itemWorker->toXml($feedItem); 00043 00044 return $this->getChannelWorker()->toXml($channel, $itemsXml); 00045 } 00046 00047 private function checkWorkers() 00048 { 00049 if (!$this->getChannelWorker()) 00050 throw new WrongStateException( 00051 'Setup channelWorker must be assigned' 00052 ); 00053 00054 if (!$this->getItemWorker()) 00055 throw new WrongStateException( 00056 'Setup itemWorker must be assigned' 00057 ); 00058 00059 return $this; 00060 } 00061 } 00062 ?>