version = $version;
$this->type = $type;
$this->charset = $charset;
$this->lang = $lang;
}
/**
* 设置标题
*
* @param string $title 标题
*/
public function setTitle(string $title)
{
$this->title = $title;
}
/**
* 设置副标题
*
* @param string|null $subTitle 副标题
*/
public function setSubTitle(?string $subTitle)
{
$this->subTitle = $subTitle;
}
/**
* 设置聚合地址
*
* @param string $feedUrl 聚合地址
*/
public function setFeedUrl(string $feedUrl)
{
$this->feedUrl = $feedUrl;
}
/**
* 设置主页
*
* @param string $baseUrl 主页地址
*/
public function setBaseUrl(string $baseUrl)
{
$this->baseUrl = $baseUrl;
}
/**
* $item的格式为
*
* array (
* 'title' => 'xxx',
* 'content' => 'xxx',
* 'excerpt' => 'xxx',
* 'date' => 'xxx',
* 'link' => 'xxx',
* 'author' => 'xxx',
* 'comments' => 'xxx',
* 'commentsUrl'=> 'xxx',
* 'commentsFeedUrl' => 'xxx',
* )
*
*
* @param array $item
*/
public function addItem(array $item)
{
$this->items[] = $item;
}
/**
* 输出字符串
*
* @return string
*/
public function __toString(): string
{
$result = 'charset . '"?>' . self::EOL;
if (self::RSS1 == $this->type) {
$result .= '' . self::EOL;
$content = '';
$links = [];
$lastUpdate = 0;
foreach ($this->items as $item) {
$content .= '- ' . self::EOL;
$content .= '' . htmlspecialchars($item['title']) . '' . self::EOL;
$content .= '' . $item['link'] . '' . self::EOL;
$content .= '' . $this->dateFormat($item['date']) . '' . self::EOL;
$content .= '' . strip_tags($item['content']) . '' . self::EOL;
if (!empty($item['suffix'])) {
$content .= $item['suffix'];
}
$content .= '
' . self::EOL;
$links[] = $item['link'];
if ($item['date'] > $lastUpdate) {
$lastUpdate = $item['date'];
}
}
$result .= '
' . htmlspecialchars($this->title) . '
' . $this->baseUrl . '
' . htmlspecialchars($this->subTitle ?? '') . '
' . self::EOL;
foreach ($links as $link) {
$result .= '' . self::EOL;
}
$result .= '
' . self::EOL;
$result .= $content . '';
} elseif (self::RSS2 == $this->type) {
$result .= '
' . self::EOL;
$content = '';
$lastUpdate = 0;
foreach ($this->items as $item) {
$content .= '- ' . self::EOL;
$content .= '' . htmlspecialchars($item['title']) . '' . self::EOL;
$content .= '' . $item['link'] . '' . self::EOL;
$content .= '' . $item['link'] . '' . self::EOL;
$content .= '' . $this->dateFormat($item['date']) . '' . self::EOL;
$content .= '' . htmlspecialchars($item['author']->screenName)
. '' . self::EOL;
if (!empty($item['category']) && is_array($item['category'])) {
foreach ($item['category'] as $category) {
$content .= '' . self::EOL;
}
}
if (!empty($item['excerpt'])) {
$content .= '' . self::EOL;
}
if (!empty($item['content'])) {
$content .= '' . self::EOL;
}
if (isset($item['comments']) && strlen($item['comments']) > 0) {
$content .= '' . $item['comments'] . '' . self::EOL;
}
$content .= '' . $item['link'] . '#comments' . self::EOL;
if (!empty($item['commentsFeedUrl'])) {
$content .= '' . $item['commentsFeedUrl'] . '' . self::EOL;
}
if (!empty($item['suffix'])) {
$content .= $item['suffix'];
}
$content .= '
' . self::EOL;
if ($item['date'] > $lastUpdate) {
$lastUpdate = $item['date'];
}
}
$result .= '' . htmlspecialchars($this->title) . '
' . $this->baseUrl . '
' . $this->lang . '
' . htmlspecialchars($this->subTitle ?? '') . '
' . $this->dateFormat($lastUpdate) . '
' . $this->dateFormat($lastUpdate) . '' . self::EOL;
$result .= $content . '
';
} elseif (self::ATOM1 == $this->type) {
$result .= '' . self::EOL;
$content = '';
$lastUpdate = 0;
foreach ($this->items as $item) {
$content .= '' . self::EOL;
$content .= '' . self::EOL;
$content .= '' . self::EOL;
$content .= '' . $item['link'] . '' . self::EOL;
$content .= '' . $this->dateFormat($item['date']) . '' . self::EOL;
$content .= '' . $this->dateFormat($item['date']) . '' . self::EOL;
$content .= '
' . $item['author']->screenName . '
' . $item['author']->url . '
' . self::EOL;
if (!empty($item['category']) && is_array($item['category'])) {
foreach ($item['category'] as $category) {
$content .= '' . self::EOL;
}
}
if (!empty($item['excerpt'])) {
$content .= '' . self::EOL;
}
if (!empty($item['content'])) {
$content .= '' . self::EOL;
}
if (isset($item['comments']) && strlen($item['comments']) > 0) {
$content .= '' . self::EOL;
if (!empty($item['commentsFeedUrl'])) {
$content .= '' . self::EOL;
}
}
if (!empty($item['suffix'])) {
$content .= $item['suffix'];
}
$content .= '' . self::EOL;
if ($item['date'] > $lastUpdate) {
$lastUpdate = $item['date'];
}
}
$result .= '' . htmlspecialchars($this->title) . '
' . htmlspecialchars($this->subTitle ?? '') . '
' . $this->dateFormat($lastUpdate) . '
Typecho
' . $this->feedUrl . '
';
$result .= $content . '';
}
return $result;
}
/**
* 获取Feed时间格式
*
* @param integer $stamp 时间戳
* @return string
*/
public function dateFormat(int $stamp): string
{
if (self::RSS2 == $this->type) {
return date(self::DATE_RFC822, $stamp);
} elseif (self::RSS1 == $this->type || self::ATOM1 == $this->type) {
return date(self::DATE_W3CDTF, $stamp);
}
return '';
}
}