1<?php 2/** 3 * UniversalFeedCreator lets you choose during runtime which 4 * format to build. 5 * For general usage of a feed class, see the FeedCreator class 6 * below or the example above. 7 * 8 * @since 1.3 9 * @author Kai Blankenhorn <kaib@bitfolge.de> 10 */ 11class UniversalFeedCreator extends FeedCreator 12{ 13 /** @var FeedCreator */ 14 protected $_feed; 15 16 /** 17 * @param string $format 18 */ 19 protected function _setFormat($format) 20 { 21 switch (strtoupper((string) $format)) { 22 23 case "BASE": 24 $this->format = $format; 25 case "2.0": 26 // fall through 27 case "RSS2.0": 28 $this->_feed = new RSSCreator20(); 29 break; 30 31 case "GEOPHOTORSS": 32 case "PHOTORSS": 33 case "GEORSS": 34 $this->format = $format; 35 case "1.0": 36 // fall through 37 case "RSS1.0": 38 $this->_feed = new RSSCreator10(); 39 break; 40 41 case "0.91": 42 // fall through 43 case "RSS0.91": 44 $this->_feed = new RSSCreator091(); 45 break; 46 47 case "PIE0.1": 48 $this->_feed = new PIECreator01(); 49 break; 50 51 case "MBOX": 52 $this->_feed = new MBOXCreator(); 53 break; 54 55 case "OPML": 56 $this->_feed = new OPMLCreator(); 57 break; 58 59 case "TOOLBAR": 60 $this->format = $format; 61 62 case "ATOM": 63 // fall through: always the latest ATOM version 64 case "ATOM1.0": 65 $this->_feed = new AtomCreator10(); 66 break; 67 68 case "ATOM0.3": 69 $this->_feed = new AtomCreator03(); 70 break; 71 72 case "HTML": 73 $this->_feed = new HTMLCreator(); 74 break; 75 76 case "PHP": 77 $this->_feed = new PHPCreator(); 78 break; 79 case "GPX": 80 $this->_feed = new GPXCreator(); 81 break; 82 case "KML": 83 $this->_feed = new KMLCreator(); 84 break; 85 case "JS": 86 // fall through 87 case "JAVASCRIPT": 88 $this->_feed = new JSCreator(); 89 break; 90 91 case "JSON": 92 $this->_feed = new JSONCreator(); 93 break; 94 95 default: 96 $this->_feed = new RSSCreator091(); 97 break; 98 } 99 100 $vars = get_object_vars($this); 101 foreach ($vars as $key => $value) { 102 // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself 103 if (!in_array($key, array("_feed", "contentType", "encoding"))) { 104 $this->_feed->{$key} = $this->{$key}; 105 } 106 } 107 } 108 109 /** 110 * Creates a syndication feed based on the items previously added. 111 * 112 * @see FeedCreator::addItem() 113 * @param string $format format the feed should comply to. Valid values are: 114 * "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS" 115 * @return string the contents of the feed. 116 */ 117 public function createFeed($format = "RSS0.91") 118 { 119 $this->_setFormat($format); 120 121 return $this->_feed->createFeed(); 122 } 123 124 /** 125 * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect 126 * header may be sent to redirect the use to the newly created file. 127 * 128 * @since 1.4 129 * @param string $format format the feed should comply to. Valid values are: 130 * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", 131 * "ATOM0.3", "HTML", "JS" 132 * @param string $filename optional the filename where a recent version of the feed is saved. If not 133 * specified, the filename is $_SERVER["SCRIPT_NAME"] with the extension changed to 134 * .xml (see _generateFilename()). 135 * @param boolean $displayContents optional send the content of the file or not. If true, the file will be sent 136 * in the body of the response. 137 */ 138 public function saveFeed($format = "RSS0.91", $filename = "", $displayContents = true) 139 { 140 $this->_setFormat($format); 141 $this->_feed->saveFeed($filename, $displayContents); 142 } 143 144 /** 145 * Turns on caching and checks if there is a recent version of this feed in the cache. 146 * If there is, an HTTP redirect header is sent. 147 * To effectively use caching, you should create the FeedCreator object and call this method 148 * before anything else, especially before you do the time consuming task to build the feed 149 * (web fetching, for example). 150 * 151 * @param string $format format the feed should comply to. Valid values are: 152 * "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3". 153 * @param string $filename optional the filename where a recent version of the feed is saved. If not specified, the 154 * filename is $_SERVER["SCRIPT_NAME"] with the extension changed to .xml (see 155 * _generateFilename()). 156 * @param int $timeout optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 157 * 1 hour) 158 */ 159 public function useCached($format = "RSS0.91", $filename = "", $timeout = 3600) 160 { 161 $this->_setFormat($format); 162 $this->_feed->useCached($filename, $timeout); 163 } 164} 165