1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | <?php /* * Copyright (C) 2008 Anis uddin Ahmad <anisniit@gmail.com> * Copyright (C) 2010-2012 Michael Bemmerl <mail@mx-server.de> * * This file is part of the "Universal Feed Writer" project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * Universal Feed Writer * * FeedItem class - Used as feed element in FeedWriter class * * @package UniversalFeedWriter * @author Anis uddin Ahmad <anisniit@gmail.com> * @link http://www.ajaxray.com/projects/rss */ class FeedItem { private $elements = array(); //Collection of feed elements private $version; /** * Constructor * * @param contant (RSS1/RSS2/ATOM) RSS2 is default. */ function __construct($version = RSS2) { $this->version = $version; } /** * Add an element to elements array * * @access public * @param string The tag name of an element * @param string The content of tag * @param array Attributes(if any) in 'attrName' => 'attrValue' format * @param boolean Specifies, if an already existing element is overwritten. * @return void */ public function addElement($elementName, $content, $attributes = null, $overwrite = FALSE) { // return if element already exists & if overwriting is disabled. if (isset($this->elements[$elementName]) && !$overwrite) return; $this->elements[$elementName]['name'] = $elementName; $this->elements[$elementName]['content'] = $content; $this->elements[$elementName]['attributes'] = $attributes; } /** * Set multiple feed elements from an array. * Elements which have attributes cannot be added by this method * * @access public * @param array array of elements in 'tagName' => 'tagContent' format. * @return void */ public function addElementArray($elementArray) { if (!is_array($elementArray)) return; foreach ($elementArray as $elementName => $content) { $this->addElement($elementName, $content); } } /** * Return the collection of elements in this feed item * * @access public * @return array */ public function getElements() { return $this->elements; } /** * Return the type of this feed item * * @access public * @return string The feed type, as defined in FeedWriter.php */ public function getVersion() { return $this->version; } // Wrapper functions ------------------------------------------------------ /** * Set the 'dscription' element of feed item * * @access public * @param string The content of 'description' or 'summary' element * @return void */ public function setDescription($description) { $tag = ($this->version == ATOM) ? 'summary' : 'description'; $this->addElement($tag, $description); } /** * @desc Set the 'title' element of feed item * @access public * @param string The content of 'title' element * @return void */ public function setTitle($title) { $this->addElement('title', $title); } /** * Set the 'date' element of feed item * * @access public * @param string The content of 'date' element * @return void */ public function setDate($date) { if(!is_numeric($date)) { if ($date instanceof DateTime) { if (version_compare(PHP_VERSION, '5.3.0', '>=')) $date = $date->getTimestamp(); else $date = strtotime($date->format('r')); } else $date = strtotime($date); } if($this->version == ATOM) { $tag = 'updated'; $value = date(DATE_ATOM, $date); } elseif($this->version == RSS2) { $tag = 'pubDate'; $value = date(DATE_RSS, $date); } else { $tag = 'dc:date'; $value = date("Y-m-d", $date); } $this->addElement($tag, $value); } /** * Set the 'link' element of feed item * * @access public * @param string The content of 'link' element * @return void */ public function setLink($link) { if($this->version == RSS2 || $this->version == RSS1) { $this->addElement('link', $link); } else { $this->addElement('link','',array('href'=>$link)); $this->addElement('id', FeedWriter::uuid($link,'urn:uuid:')); } } /** * Set the 'encloser' element of feed item * For RSS 2.0 only * * @access public * @param string The url attribute of encloser tag * @param string The length attribute of encloser tag * @param string The type attribute of encloser tag * @return void */ public function setEncloser($url, $length, $type) { if ($this->version != RSS2) return; $attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type); $this->addElement('enclosure','',$attributes); } /** * Set the 'author' element of feed item * For ATOM only * * @access public * @param string The author of this item * @return void */ public function setAuthor($author) { if ($this->version != ATOM) return; $this->addElement('author', '<name>' . $author . '</name>'); } /** * Set the unique identifier of the feed item * * @access public * @param string The unique identifier of this item * @return void */ public function setId($id) { if ($this->version == RSS2) { $this->addElement('guid', $id, array('isPermaLink' => 'false')); } else if ($this->version == ATOM) { $this->addElement('id', FeedWriter::uuid($id,'urn:uuid:'), NULL, TRUE); } } } // end of class FeedItem |