250 lines
7.6 KiB
PHP
250 lines
7.6 KiB
PHP
<?php
|
|
|
|
/** DomFramework
|
|
* @package domframework
|
|
* @author Dominique Fournier <dominique@fournier38.fr>
|
|
* @license BSD
|
|
*/
|
|
|
|
namespace Domframework;
|
|
|
|
/** Create the RSS pages for the websites
|
|
* Specification http://www.rssboard.org/rss-specification
|
|
*
|
|
* Will use RssItem class
|
|
*/
|
|
class Rss
|
|
{
|
|
/** The title of the site (MANDATORY)
|
|
*/
|
|
private $title;
|
|
|
|
/** The link (URL) to the site (MANDATORY)
|
|
*/
|
|
private $link;
|
|
|
|
/** The Description of the site (MANDATORY)
|
|
*/
|
|
private $description;
|
|
|
|
/** The language of the feed
|
|
*/
|
|
private $language;
|
|
|
|
/** The lastBuildDate
|
|
*/
|
|
private $lastBuildDate;
|
|
|
|
/** The items linked to this RSS
|
|
*/
|
|
private $items = array();
|
|
|
|
/** Get/Set the value
|
|
* @param string|null $title The title to get/set
|
|
*/
|
|
public function title($title = null)
|
|
{
|
|
if ($title === null) {
|
|
return $this->title;
|
|
}
|
|
if (! is_string($title)) {
|
|
throw new \Exception("Title provided to RSS is not a string", 500);
|
|
}
|
|
if ($title === "") {
|
|
$title = null;
|
|
}
|
|
$this->title = $title;
|
|
return $this;
|
|
}
|
|
|
|
/** Get/Set the value
|
|
* @param string|null $link The link to get/set
|
|
*/
|
|
public function link($link = null)
|
|
{
|
|
if ($link === null) {
|
|
return $this->link;
|
|
}
|
|
if (! is_string($link)) {
|
|
throw new \Exception("Link provided to RSS is not a string", 500);
|
|
}
|
|
$verify = new Verify();
|
|
if (! $verify->is_URL($link)) {
|
|
throw new \Exception("Link provided to RSS is not an URL", 500);
|
|
}
|
|
if ($link === "") {
|
|
$link = null;
|
|
}
|
|
$this->link = $link;
|
|
return $this;
|
|
}
|
|
|
|
/** Get/Set the value
|
|
* @param string|null $description The description to get/set
|
|
*/
|
|
public function description($description = null)
|
|
{
|
|
if ($description === null) {
|
|
return $this->description;
|
|
}
|
|
if (! is_string($description)) {
|
|
throw new \Exception("Description provided to RSS is not a string", 500);
|
|
}
|
|
if ($description === "") {
|
|
$description = null;
|
|
}
|
|
$this->description = $description;
|
|
return $this;
|
|
}
|
|
|
|
/** Get/Set the value
|
|
* @param string|null $language The language to get/set
|
|
*/
|
|
public function language($language = null)
|
|
{
|
|
if ($language === null) {
|
|
return $this->language;
|
|
}
|
|
if (! is_string($language)) {
|
|
throw new \Exception("Language provided to RSS is not a string", 500);
|
|
}
|
|
if (
|
|
! in_array(
|
|
$language,
|
|
array("af", "sq", "eu", "be", "bg", "ca", "zh-cn", "zh-tw", "hr", "cs",
|
|
"da", "nl", "nl-be", "nl-nl", "en", "en-au", "en-bz", "en-ca",
|
|
"en-ie", "en-jm", "en-nz", "en-ph", "en-za", "en-tt", "en-gb",
|
|
"en-us", "en-zw", "et", "fo", "fi", "fr", "fr-be", "fr-ca",
|
|
"fr-fr", "fr-lu", "fr-mc", "fr-ch", "gl", "gd", "de", "de-at",
|
|
"de-de", "de-li", "de-lu", "de-ch", "el", "haw", "hu", "is",
|
|
"in", "ga", "it", "it-it", "it-ch", "ja", "ko", "mk", "no", "pl",
|
|
"pt", "pt-br", "pt-pt", "ro", "ro-mo", "ro-ro", "ru", "ru-mo",
|
|
"ru-ru", "sr", "sk", "sl", "es", "es-ar", "es-bo", "es-cl",
|
|
"es-co", "es-cr", "es-do", "es-ec", "es-sv", "es-gt", "es-hn",
|
|
"es-mx", "es-ni", "es-pa", "es-py", "es-pe", "es-pr", "es-es",
|
|
"es-uy", "es-ve", "sv", "sv-fi", "sv-se", "tr", "uk")
|
|
)
|
|
) {
|
|
throw new \Exception("Provided language for RSS is not allowed", 500);
|
|
}
|
|
if ($language === "") {
|
|
$language = null;
|
|
}
|
|
$this->language = $language;
|
|
return $this;
|
|
}
|
|
|
|
/** Get/Set the value
|
|
* The date must be in the SQL format Y-m-d H:i:s
|
|
* @param string|null $lastBuildDate The lastBuildDate to get/set
|
|
*/
|
|
public function lastBuildDate($lastBuildDate = null)
|
|
{
|
|
if ($lastBuildDate === null) {
|
|
return $this->lastBuildDate;
|
|
}
|
|
if (! is_string($lastBuildDate)) {
|
|
throw new \Exception(
|
|
"lastBuildDate provided to RSS is not a string",
|
|
500
|
|
);
|
|
}
|
|
$verify = new Verify();
|
|
if (! $verify->is_datetimeSQL($lastBuildDate)) {
|
|
throw new \Exception(
|
|
"lastBuildDate provided to RSS is not a valid date",
|
|
500
|
|
);
|
|
}
|
|
if ($lastBuildDate === "") {
|
|
$lastBuildDate = null;
|
|
} else {
|
|
$lastBuildDate = Convert::convertDate(
|
|
$lastBuildDate,
|
|
"Y-m-d H:i:s",
|
|
\DateTime::RFC2822
|
|
);
|
|
}
|
|
$this->lastBuildDate = $lastBuildDate;
|
|
return $this;
|
|
}
|
|
|
|
/** Add a new item to the RSS
|
|
* Return the new rss object created
|
|
*/
|
|
public function addItem()
|
|
{
|
|
$rssitem = new RssItem();
|
|
$this->items[] = $rssitem;
|
|
return $rssitem;
|
|
}
|
|
|
|
/** Return the complete RSS in XML format
|
|
* Throw an exception if there is some mandatory fields missing
|
|
*/
|
|
public function asXML()
|
|
{
|
|
if ($this->title === null) {
|
|
throw new \Exception("No title is provided for this RSS", 500);
|
|
}
|
|
if ($this->link === null) {
|
|
throw new \Exception("No link is provided for this RSS", 500);
|
|
}
|
|
if ($this->description === null) {
|
|
throw new \Exception("No description is provided for this RSS", 500);
|
|
}
|
|
$xmlstr =
|
|
'<?xml version="1.0"' . '?' . '>' . "\n" .
|
|
'<rss version="2.0">' . "\n" .
|
|
' <channel>' . "\n" .
|
|
' </channel>' . "\n" .
|
|
'</rss>';
|
|
$xml = new \SimpleXMLElement($xmlstr);
|
|
$xml->channel->title = $this->title;
|
|
$xml->channel->link = $this->link;
|
|
$xml->channel->description = $this->description;
|
|
if ($this->language !== null) {
|
|
$xml->channel->language = $this->language;
|
|
}
|
|
if ($this->lastBuildDate !== null) {
|
|
$xml->channel->lastBuildDate = $this->lastBuildDate;
|
|
}
|
|
foreach ($this->items as $item) {
|
|
if ($item->title() === null && $item->description() === null) {
|
|
throw new \Exception(
|
|
"No title nor description defined in the RSS item",
|
|
500
|
|
);
|
|
}
|
|
$tmpItem = $xml->channel->addChild("item");
|
|
if ($item->title() !== null) {
|
|
$tmpItem->title = $item->title();
|
|
}
|
|
if ($item->link() !== null) {
|
|
$tmpItem->link = $item->link();
|
|
}
|
|
if ($item->description() !== null) {
|
|
$tmpItem->description = $item->description();
|
|
}
|
|
if ($item->author() !== null) {
|
|
$tmpItem->author = $item->author();
|
|
}
|
|
if ($item->comments() !== null) {
|
|
$tmpItem->comments = $item->comments();
|
|
}
|
|
if ($item->guid() !== null) {
|
|
$tmpItem->guid = $item->guid();
|
|
}
|
|
if ($item->pubDate() !== null) {
|
|
$tmpItem->pubDate = $item->pubDate();
|
|
}
|
|
}
|
|
// Ident correctely the xml created in SimpleXML
|
|
$dom = new \DOMDocument('1.0');
|
|
$dom->preserveWhiteSpace = false;
|
|
$dom->formatOutput = true;
|
|
$dom->loadXML($xml->asXML());
|
|
return $dom->saveXML();
|
|
}
|
|
}
|