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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Builder.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_Feed_Builder_Interface
*/
require_once 'Zend/Feed/Builder/Interface.php';
/**
* @see Zend_Feed_Builder_Header
*/
require_once 'Zend/Feed/Builder/Header.php';
/**
* @see Zend_Feed_Builder_Entry
*/
require_once 'Zend/Feed/Builder/Entry.php';
/**
* A simple implementation of Zend_Feed_Builder_Interface.
*
* Users are encouraged to make their own classes to implement Zend_Feed_Builder_Interface
*
* @category Zend
* @package Zend_Feed
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Builder implements Zend_Feed_Builder_Interface
{
/**
* The data of the feed
*
* @var $_data array
*/
private $_data;
/**
* Header of the feed
*
* @var $_header Zend_Feed_Builder_Header
*/
private $_header;
/**
* List of the entries of the feed
*
* @var $_entries array
*/
private $_entries = array();
/**
* Constructor. The $data array must conform to the following format:
* <code>
* array(
* 'title' => 'title of the feed', //required
* 'link' => 'canonical url to the feed', //required
* 'lastUpdate' => 'timestamp of the update date', // optional
* 'published' => 'timestamp of the publication date', //optional
* 'charset' => 'charset', // required
* 'description' => 'short description of the feed', //optional
* 'author' => 'author/publisher of the feed', //optional
* 'email' => 'email of the author', //optional
* 'webmaster' => 'email address for person responsible for technical issues' // optional, ignored if atom is used
* 'copyright' => 'copyright notice', //optional
* 'image' => 'url to image', //optional
* 'generator' => 'generator', // optional
* 'language' => 'language the feed is written in', // optional
* 'ttl' => 'how long in minutes a feed can be cached before refreshing', // optional, ignored if atom is used
* 'rating' => 'The PICS rating for the channel.', // optional, ignored if atom is used
* 'cloud' => array(
* 'domain' => 'domain of the cloud, e.g. rpc.sys.com' // required
* 'port' => 'port to connect to' // optional, default to 80
* 'path' => 'path of the cloud, e.g. /RPC2 //required
* 'registerProcedure' => 'procedure to call, e.g. myCloud.rssPleaseNotify' // required
* 'protocol' => 'protocol to use, e.g. soap or xml-rpc' // required
* ), a cloud to be notified of updates // optional, ignored if atom is used
* 'textInput' => array(
* 'title' => 'the label of the Submit button in the text input area' // required,
* 'description' => 'explains the text input area' // required
* 'name' => 'the name of the text object in the text input area' // required
* 'link' => 'the URL of the CGI script that processes text input requests' // required
* ) // a text input box that can be displayed with the feed // optional, ignored if atom is used
* 'skipHours' => array(
* 'hour in 24 format', // e.g 13 (1pm)
* // up to 24 rows whose value is a number between 0 and 23
* ) // Hint telling aggregators which hours they can skip // optional, ignored if atom is used
* 'skipDays ' => array(
* 'a day to skip', // e.g Monday
* // up to 7 rows whose value is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday
* ) // Hint telling aggregators which days they can skip // optional, ignored if atom is used
* 'itunes' => array(
* 'author' => 'Artist column' // optional, default to the main author value
* 'owner' => array(
* 'name' => 'name of the owner' // optional, default to main author value
* 'email' => 'email of the owner' // optional, default to main email value
* ) // Owner of the podcast // optional
* 'image' => 'album/podcast art' // optional, default to the main image value
* 'subtitle' => 'short description' // optional, default to the main description value
* 'summary' => 'longer description' // optional, default to the main description value
* 'block' => 'Prevent an episode from appearing (yes|no)' // optional
* 'category' => array(
* array('main' => 'main category', // required
* 'sub' => 'sub category' // optional
* ),
* // up to 3 rows
* ) // 'Category column and in iTunes Music Store Browse' // required
* 'explicit' => 'parental advisory graphic (yes|no|clean)' // optional
* 'keywords' => 'a comma separated list of 12 keywords maximum' // optional
* 'new-feed-url' => 'used to inform iTunes of new feed URL location' // optional
* ) // Itunes extension data // optional, ignored if atom is used
* 'entries' => array(
* array(
* 'title' => 'title of the feed entry', //required
* 'link' => 'url to a feed entry', //required
* 'description' => 'short version of a feed entry', // only text, no html, required
* 'guid' => 'id of the article, if not given link value will used', //optional
* 'content' => 'long version', // can contain html, optional
* 'lastUpdate' => 'timestamp of the publication date', // optional
* 'comments' => 'comments page of the feed entry', // optional
* 'commentRss' => 'the feed url of the associated comments', // optional
* 'source' => array(
* 'title' => 'title of the original source' // required,
* 'url' => 'url of the original source' // required
* ) // original source of the feed entry // optional
* 'category' => array(
* array(
* 'term' => 'first category label' // required,
* 'scheme' => 'url that identifies a categorization scheme' // optional
* ),
* array(
* //data for the second category and so on
* )
* ) // list of the attached categories // optional
* 'enclosure' => array(
* array(
* 'url' => 'url of the linked enclosure' // required
* 'type' => 'mime type of the enclosure' // optional
* 'length' => 'length of the linked content in octets' // optional
* ),
* array(
* //data for the second enclosure and so on
* )
* ) // list of the enclosures of the feed entry // optional
* ),
* array(
* //data for the second entry and so on
* )
* )
* );
* </code>
*
* @param array $data
* @return void
*/
public function __construct(array $data)
{
$this->_data = $data;
$this->_createHeader($data);
if (isset($data['entries'])) {
$this->_createEntries($data['entries']);
}
}
/**
* Returns an instance of Zend_Feed_Builder_Header
* describing the header of the feed
*
* @return Zend_Feed_Builder_Header
*/
public function getHeader()
{
return $this->_header;
}
/**
* Returns an array of Zend_Feed_Builder_Entry instances
* describing the entries of the feed
*
* @return array of Zend_Feed_Builder_Entry
*/
public function getEntries()
{
return $this->_entries;
}
/**
* Create the Zend_Feed_Builder_Header instance
*
* @param array $data
* @throws Zend_Feed_Builder_Exception
* @return void
*/
protected function _createHeader(array $data)
{
$mandatories = array('title', 'link', 'charset');
foreach ($mandatories as $mandatory) {
if (!isset($data[$mandatory])) {
/**
* @see Zend_Feed_Builder_Exception
*/
require_once 'Zend/Feed/Builder/Exception.php';
throw new Zend_Feed_Builder_Exception("$mandatory key is missing");
}
}
$this->_header = new Zend_Feed_Builder_Header($data['title'], $data['link'], $data['charset']);
if (isset($data['lastUpdate'])) {
$this->_header->setLastUpdate($data['lastUpdate']);
}
if (isset($data['published'])) {
$this->_header->setPublishedDate($data['published']);
}
if (isset($data['description'])) {
$this->_header->setDescription($data['description']);
}
if (isset($data['author'])) {
$this->_header->setAuthor($data['author']);
}
if (isset($data['email'])) {
$this->_header->setEmail($data['email']);
}
if (isset($data['webmaster'])) {
$this->_header->setWebmaster($data['webmaster']);
}
if (isset($data['copyright'])) {
$this->_header->setCopyright($data['copyright']);
}
if (isset($data['image'])) {
$this->_header->setImage($data['image']);
}
if (isset($data['generator'])) {
$this->_header->setGenerator($data['generator']);
}
if (isset($data['language'])) {
$this->_header->setLanguage($data['language']);
}
if (isset($data['ttl'])) {
$this->_header->setTtl($data['ttl']);
}
if (isset($data['rating'])) {
$this->_header->setRating($data['rating']);
}
if (isset($data['cloud'])) {
$mandatories = array('domain', 'path', 'registerProcedure', 'protocol');
foreach ($mandatories as $mandatory) {
if (!isset($data['cloud'][$mandatory])) {
/**
* @see Zend_Feed_Builder_Exception
*/
require_once 'Zend/Feed/Builder/Exception.php';
throw new Zend_Feed_Builder_Exception("you have to define $mandatory property of your cloud");
}
}
$uri_str = 'http://' . $data['cloud']['domain'] . $data['cloud']['path'];
$this->_header->setCloud($uri_str, $data['cloud']['registerProcedure'], $data['cloud']['protocol']);
}
if (isset($data['textInput'])) {
$mandatories = array('title', 'description', 'name', 'link');
foreach ($mandatories as $mandatory) {
if (!isset($data['textInput'][$mandatory])) {
/**
* @see Zend_Feed_Builder_Exception
*/
require_once 'Zend/Feed/Builder/Exception.php';
throw new Zend_Feed_Builder_Exception("you have to define $mandatory property of your textInput");
}
}
$this->_header->setTextInput($data['textInput']['title'],
$data['textInput']['description'],
$data['textInput']['name'],
$data['textInput']['link']);
}
if (isset($data['skipHours'])) {
$this->_header->setSkipHours($data['skipHours']);
}
if (isset($data['skipDays'])) {
$this->_header->setSkipDays($data['skipDays']);
}
if (isset($data['itunes'])) {
$itunes = new Zend_Feed_Builder_Header_Itunes($data['itunes']['category']);
if (isset($data['itunes']['author'])) {
$itunes->setAuthor($data['itunes']['author']);
}
if (isset($data['itunes']['owner'])) {
$name = isset($data['itunes']['owner']['name']) ? $data['itunes']['owner']['name'] : '';
$email = isset($data['itunes']['owner']['email']) ? $data['itunes']['owner']['email'] : '';
$itunes->setOwner($name, $email);
}
if (isset($data['itunes']['image'])) {
$itunes->setImage($data['itunes']['image']);
}
if (isset($data['itunes']['subtitle'])) {
$itunes->setSubtitle($data['itunes']['subtitle']);
}
if (isset($data['itunes']['summary'])) {
$itunes->setSummary($data['itunes']['summary']);
}
if (isset($data['itunes']['block'])) {
$itunes->setBlock($data['itunes']['block']);
}
if (isset($data['itunes']['explicit'])) {
$itunes->setExplicit($data['itunes']['explicit']);
}
if (isset($data['itunes']['keywords'])) {
$itunes->setKeywords($data['itunes']['keywords']);
}
if (isset($data['itunes']['new-feed-url'])) {
$itunes->setNewFeedUrl($data['itunes']['new-feed-url']);
}
$this->_header->setITunes($itunes);
}
}
/**
* Create the array of article entries
*
* @param array $data
* @throws Zend_Feed_Builder_Exception
* @return void
*/
protected function _createEntries(array $data)
{
foreach ($data as $row) {
$mandatories = array('title', 'link', 'description');
foreach ($mandatories as $mandatory) {
if (!isset($row[$mandatory])) {
/**
* @see Zend_Feed_Builder_Exception
*/
require_once 'Zend/Feed/Builder/Exception.php';
throw new Zend_Feed_Builder_Exception("$mandatory key is missing");
}
}
$entry = new Zend_Feed_Builder_Entry($row['title'], $row['link'], $row['description']);
if (isset($row['author'])) {
$entry->setAuthor($row['author']);
}
if (isset($row['guid'])) {
$entry->setId($row['guid']);
}
if (isset($row['content'])) {
$entry->setContent($row['content']);
}
if (isset($row['lastUpdate'])) {
$entry->setLastUpdate($row['lastUpdate']);
}
if (isset($row['comments'])) {
$entry->setCommentsUrl($row['comments']);
}
if (isset($row['commentRss'])) {
$entry->setCommentsRssUrl($row['commentRss']);
}
if (isset($row['source'])) {
$mandatories = array('title', 'url');
foreach ($mandatories as $mandatory) {
if (!isset($row['source'][$mandatory])) {
/**
* @see Zend_Feed_Builder_Exception
*/
require_once 'Zend/Feed/Builder/Exception.php';
throw new Zend_Feed_Builder_Exception("$mandatory key of source property is missing");
}
}
$entry->setSource($row['source']['title'], $row['source']['url']);
}
if (isset($row['category'])) {
$entry->setCategories($row['category']);
}
if (isset($row['enclosure'])) {
$entry->setEnclosures($row['enclosure']);
}
$this->_entries[] = $entry;
}
}
}
|