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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
<?php
/**
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
* Atom feeds for websites that don't have one.
*
* For the full license information, please view the UNLICENSE file distributed
* with this source code.
*
* @package Core
* @license http://unlicense.org/ UNLICENSE
* @link https://github.com/rss-bridge/rss-bridge
*/
/**
* Represents a simple feed item for transformation into various feed formats.
*
* This class represents a feed item. A feed item is an entity that can be
* transformed into various feed formats. It holds a set of pre-defined
* properties:
*
* - **URI**: URI to the full article (i.e. "https://...")
* - **Title**: The title
* - **Timestamp**: A timestamp of when the item was first released
* - **Author**: Name of the author
* - **Content**: Body of the feed, as text or HTML
* - **Enclosures**: A list of links to media objects (images, videos, etc...)
* - **Categories**: A list of category names or tags to categorize the item
*
* _Note_: A feed item can have any number of additional parameters, all of which
* may or may not be transformed to the selected output format.
*
* _Remarks_: This class supports legacy items via {@see FeedItem::__construct()}
* (i.e. `$feedItem = \FeedItem($item);`). Support for legacy items may be removed
* in future versions of RSS-Bridge.
*/
class FeedItem {
/** @var string|null URI to the full article */
protected $uri = null;
/** @var string|null Title of the item */
protected $title = null;
/** @var int|null Timestamp of when the item was first released */
protected $timestamp = null;
/** @var string|null Name of the author */
protected $author = null;
/** @var string|null Body of the feed */
protected $content = null;
/** @var array List of links to media objects */
protected $enclosures = array();
/** @var array List of category names or tags */
protected $categories = array();
/** @var array Associative list of additional parameters */
protected $misc = array(); // Custom parameters
/**
* Create object from legacy item.
*
* The provided array must be an associative array of key-value-pairs, where
* keys may correspond to any of the properties of this class.
*
* Example use:
*
* ```PHP
* <?php
* $item = array();
*
* $item['uri'] = 'https://www.github.com/rss-bridge/rss-bridge/';
* $item['title'] = 'Title';
* $item['timestamp'] = strtotime('now');
* $item['autor'] = 'Unknown author';
* $item['content'] = 'Hello World!';
* $item['enclosures'] = array('https://github.com/favicon.ico');
* $item['categories'] = array('php', 'rss-bridge', 'awesome');
*
* $feedItem = new \FeedItem($item);
*
* ```
*
* The result of the code above is the same as the code below:
*
* ```PHP
* <?php
* $feedItem = \FeedItem();
*
* $feedItem->uri = 'https://www.github.com/rss-bridge/rss-bridge/';
* $feedItem->title = 'Title';
* $feedItem->timestamp = strtotime('now');
* $feedItem->autor = 'Unknown author';
* $feedItem->content = 'Hello World!';
* $feedItem->enclosures = array('https://github.com/favicon.ico');
* $feedItem->categories = array('php', 'rss-bridge', 'awesome');
* ```
*
* @param array $item (optional) A legacy item (empty: no legacy support).
* @return object A new object of this class
*/
public function __construct($item = array()) {
if(!is_array($item))
Debug::log('Item must be an array!');
foreach($item as $key => $value) {
$this->__set($key, $value);
}
}
/**
* Get current URI.
*
* Use {@see FeedItem::setURI()} to set the URI.
*
* @return string|null The URI or null if it hasn't been set.
*/
public function getURI() {
return $this->uri;
}
/**
* Set URI to the full article.
*
* Use {@see FeedItem::getURI()} to get the URI.
*
* _Note_: Removes whitespace from the beginning and end of the URI.
*
* _Remarks_: Uses the attribute "href" or "src" if the provided URI is an
* object of simple_html_dom_node.
*
* @param object|string $uri URI to the full article.
* @return self
*/
public function setURI($uri) {
$this->uri = null; // Clear previous data
if($uri instanceof simple_html_dom_node) {
if($uri->hasAttribute('href')) { // Anchor
$uri = $uri->href;
} elseif($uri->hasAttribute('src')) { // Image
$uri = $uri->src;
} else {
Debug::log('The item provided as URI is unknown!');
}
}
if(!is_string($uri)) {
Debug::log('URI must be a string!');
} elseif(!filter_var(
$uri,
FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
Debug::log('URI must include a scheme, host and path!');
} else {
$scheme = parse_url($uri, PHP_URL_SCHEME);
if($scheme !== 'http' && $scheme !== 'https') {
Debug::log('URI scheme must be "http" or "https"!');
} else {
$this->uri = trim($uri);
}
}
return $this;
}
/**
* Get current title.
*
* Use {@see FeedItem::setTitle()} to set the title.
*
* @return string|null The current title or null if it hasn't been set.
*/
public function getTitle() {
return $this->title;
}
/**
* Set title.
*
* Use {@see FeedItem::getTitle()} to get the title.
*
* _Note_: Removes whitespace from beginning and end of the title.
*
* @param string $title The title
* @return self
*/
public function setTitle($title) {
$this->title = null; // Clear previous data
if(!is_string($title)) {
Debug::log('Title must be a string!');
} else {
$this->title = trim($title);
}
return $this;
}
/**
* Get current timestamp.
*
* Use {@see FeedItem::setTimestamp()} to set the timestamp.
*
* @return int|null The current timestamp or null if it hasn't been set.
*/
public function getTimestamp() {
return $this->timestamp;
}
/**
* Set timestamp of first release.
*
* _Note_: The timestamp should represent the number of seconds since
* January 1 1970 00:00:00 GMT (Unix time).
*
* _Remarks_: If the provided timestamp is a string (not numeric), this
* function automatically attempts to parse the string using
* [strtotime](http://php.net/manual/en/function.strtotime.php)
*
* @link http://php.net/manual/en/function.strtotime.php strtotime (PHP)
* @link https://en.wikipedia.org/wiki/Unix_time Unix time (Wikipedia)
*
* @param string|int $timestamp A timestamp of when the item was first released
* @return self
*/
public function setTimestamp($timestamp) {
$this->timestamp = null; // Clear previous data
if(!is_numeric($timestamp)
&& !$timestamp = strtotime($timestamp)) {
Debug::log('Unable to parse timestamp!');
}
if($timestamp <= 0) {
Debug::log('Timestamp must be greater than zero!');
} else {
$this->timestamp = $timestamp;
}
return $this;
}
/**
* Get the current author name.
*
* Use {@see FeedItem::setAuthor()} to set the author.
*
* @return string|null The author or null if it hasn't been set.
*/
public function getAuthor() {
return $this->author;
}
/**
* Set the author name.
*
* Use {@see FeedItem::getAuthor()} to get the author.
*
* @param string $author The author name.
* @return self
*/
public function setAuthor($author) {
$this->author = null; // Clear previous data
if(!is_string($author)) {
Debug::log('Author must be a string!');
} else {
$this->author = $author;
}
return $this;
}
/**
* Get item content.
*
* Use {@see FeedItem::setContent()} to set the item content.
*
* @return string|null The item content or null if it hasn't been set.
*/
public function getContent() {
return $this->content;
}
/**
* Set item content.
*
* Note: This function casts objects of type simple_html_dom and
* simple_html_dom_node to string.
*
* Use {@see FeedItem::getContent()} to get the current item content.
*
* @param string|object $content The item content as text or simple_html_dom
* object.
* @return self
*/
public function setContent($content) {
$this->content = null; // Clear previous data
if($content instanceof simple_html_dom
|| $content instanceof simple_html_dom_node) {
$content = (string)$content;
}
if(!is_string($content)) {
Debug::log('Content must be a string!');
} else {
$this->content = $content;
}
return $this;
}
/**
* Get item enclosures.
*
* Use {@see FeedItem::setEnclosures()} to set feed enclosures.
*
* @return array Enclosures as array of enclosure URIs.
*/
public function getEnclosures() {
return $this->enclosures;
}
/**
* Set item enclosures.
*
* Use {@see FeedItem::getEnclosures()} to get the current item enclosures.
*
* @param array $enclosures Array of enclosures, where each element links to
* one enclosure.
* @return self
*/
public function setEnclosures($enclosures) {
$this->enclosures = array(); // Clear previous data
if(!is_array($enclosures)) {
Debug::log('Enclosures must be an array!');
} else {
foreach($enclosures as $enclosure) {
if(!filter_var(
$enclosure,
FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
Debug::log('Each enclosure must contain a scheme, host and path!');
} else {
$this->enclosures[] = $enclosure;
}
}
}
return $this;
}
/**
* Get item categories.
*
* Use {@see FeedItem::setCategories()} to set item categories.
*
* @param array The item categories.
*/
public function getCategories() {
return $this->categories;
}
/**
* Set item categories.
*
* Use {@see FeedItem::getCategories()} to get the current item categories.
*
* @param array $categories Array of categories, where each element defines
* a single category name.
* @return self
*/
public function setCategories($categories) {
$this->categories = array(); // Clear previous data
if(!is_array($categories)) {
Debug::log('Categories must be an array!');
} else {
foreach($categories as $category) {
if(!is_string($category)) {
Debug::log('Category must be a string!');
} else {
$this->categories[] = $category;
}
}
}
return $this;
}
/**
* Add miscellaneous elements to the item.
*
* @param string $key Name of the element.
* @param mixed $value Value of the element.
* @return self
*/
public function addMisc($key, $value) {
if(!is_string($key)) {
Debug::log('Key must be a string!');
} elseif(in_array($key, get_object_vars($this))) {
Debug::log('Key must be unique!');
} else {
$this->misc[$key] = $value;
}
return $this;
}
/**
* Transform current object to array
*
* @return array
*/
public function toArray() {
return array_merge(
array(
'uri' => $this->uri,
'title' => $this->title,
'timestamp' => $this->timestamp,
'author' => $this->author,
'content' => $this->content,
'enclosures' => $this->enclosures,
'categories' => $this->categories,
), $this->misc
);
}
/**
* Set item property
*
* Allows simple assignment to parameters. This method is slower, but easier
* to implement in some cases:
*
* ```PHP
* $item = new \FeedItem();
* $item->content = 'Hello World!';
* $item->my_id = 42;
* ```
*
* @param string $name Property name
* @param mixed $value Property value
*/
function __set($name, $value) {
switch($name) {
case 'uri': $this->setURI($value); break;
case 'title': $this->setTitle($value); break;
case 'timestamp': $this->setTimestamp($value); break;
case 'author': $this->setAuthor($value); break;
case 'content': $this->setContent($value); break;
case 'enclosures': $this->setEnclosures($value); break;
case 'categories': $this->setCategories($value); break;
default: $this->addMisc($name, $value);
}
}
/**
* Get item property
*
* Allows simple assignment to parameters. This method is slower, but easier
* to implement in some cases.
*
* @param string $name Property name
* @return mixed Property value
*/
function __get($name) {
switch($name) {
case 'uri': return $this->getURI();
case 'title': return $this->getTitle();
case 'timestamp': return $this->getTimestamp();
case 'author': return $this->getAuthor();
case 'content': return $this->getContent();
case 'enclosures': return $this->getEnclosures();
case 'categories': return $this->getCategories();
default:
if(array_key_exists($name, $this->misc))
return $this->misc[$name];
return null;
}
}
}
|