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($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            default:
92                $this->_feed = new RSSCreator091();
93                break;
94        }
95
96        $vars = get_object_vars($this);
97        foreach ($vars as $key => $value) {
98            // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
99            if (!in_array($key, array("_feed", "contentType", "encoding"))) {
100                $this->_feed->{$key} = $this->{$key};
101            }
102        }
103    }
104
105    /**
106     * Creates a syndication feed based on the items previously added.
107     *
108     * @see FeedCreator::addItem()
109     * @param string $format format the feed should comply to. Valid values are:
110     *                       "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
111     * @return string the contents of the feed.
112     */
113    public function createFeed($format = "RSS0.91")
114    {
115        $this->_setFormat($format);
116
117        return $this->_feed->createFeed();
118    }
119
120    /**
121     * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
122     * header may be sent to redirect the use to the newly created file.
123     *
124     * @since 1.4
125     * @param string $format           format the feed should comply to. Valid values are:
126     *                                 "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM",
127     *                                 "ATOM0.3", "HTML", "JS"
128     * @param string $filename         optional    the filename where a recent version of the feed is saved. If not
129     *                                 specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to
130     *                                 .xml (see _generateFilename()).
131     * @param boolean $displayContents optional    send the content of the file or not. If true, the file will be sent
132     *                                 in the body of the response.
133     */
134    public function saveFeed($format = "RSS0.91", $filename = "", $displayContents = true)
135    {
136        $this->_setFormat($format);
137        $this->_feed->saveFeed($filename, $displayContents);
138    }
139
140    /**
141     * Turns on caching and checks if there is a recent version of this feed in the cache.
142     * If there is, an HTTP redirect header is sent.
143     * To effectively use caching, you should create the FeedCreator object and call this method
144     * before anything else, especially before you do the time consuming task to build the feed
145     * (web fetching, for example).
146     *
147     * @param string $format   format the feed should comply to. Valid values are:
148     *                         "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
149     * @param string $filename optional the filename where a recent version of the feed is saved. If not specified, the
150     *                         filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see
151     *                         _generateFilename()).
152     * @param int $timeout     optional the timeout in seconds before a cached version is refreshed (defaults to 3600 =
153     *                         1 hour)
154     */
155    public function useCached($format = "RSS0.91", $filename = "", $timeout = 3600)
156    {
157        $this->_setFormat($format);
158        $this->_feed->useCached($filename, $timeout);
159    }
160}
161