rss-bridge/lib/FormatAbstract.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2018-11-16 23:48:59 +03:00
/**
* 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 https://unlicense.org/ UNLICENSE
* @link https://github.com/rss-bridge/rss-bridge
*/
/**
* An abstract class for format implementations
*
* This class implements {@see FormatInterface}
*/
abstract class FormatAbstract implements FormatInterface
{
2018-11-16 23:48:59 +03:00
/** The default charset (UTF-8) */
const DEFAULT_CHARSET = 'UTF-8';
/** MIME type of format output */
const MIME_TYPE = 'text/plain';
2018-11-16 23:48:59 +03:00
/** @var string $charset The charset */
protected $charset;
2018-11-16 23:48:59 +03:00
/** @var array $items The items */
protected $items;
2018-11-16 23:48:59 +03:00
/**
* @var int $lastModified A timestamp to indicate the last modified time of
* the output data.
*/
protected $lastModified;
2018-11-16 23:48:59 +03:00
/** @var array $extraInfos The extra infos */
protected $extraInfos;
/** {@inheritdoc} */
public function getMimeType()
{
return static::MIME_TYPE;
}
2018-11-16 23:48:59 +03:00
/**
* {@inheritdoc}
*
* @param string $charset {@inheritdoc}
*/
public function setCharset($charset)
{
$this->charset = $charset;
return $this;
}
2018-11-16 23:48:59 +03:00
/** {@inheritdoc} */
public function getCharset()
{
$charset = $this->charset;
if (is_null($charset)) {
return static::DEFAULT_CHARSET;
}
return $charset;
}
2018-11-16 23:48:59 +03:00
/**
* Set the last modified time
*
* @param int $lastModified The last modified time
* @return void
*/
public function setLastModified($lastModified)
{
$this->lastModified = $lastModified;
}
2018-11-16 23:48:59 +03:00
/**
* {@inheritdoc}
*
* @param array $items {@inheritdoc}
*/
public function setItems(array $items)
{
$this->items = $items;
return $this;
}
2018-11-16 23:48:59 +03:00
/** {@inheritdoc} */
public function getItems()
{
if (!is_array($this->items)) {
throw new \LogicException(sprintf('Feed the %s with "setItems" method before !', get_class($this)));
}
return $this->items;
}
/**
2018-11-16 23:48:59 +03:00
* {@inheritdoc}
*
* @param array $extraInfos {@inheritdoc}
*/
public function setExtraInfos(array $extraInfos = [])
{
foreach (['name', 'uri', 'icon', 'donationUri'] as $infoName) {
if (!isset($extraInfos[$infoName])) {
$extraInfos[$infoName] = '';
}
}
$this->extraInfos = $extraInfos;
return $this;
}
2018-11-16 23:48:59 +03:00
/** {@inheritdoc} */
public function getExtraInfos()
{
if (is_null($this->extraInfos)) { // No extra info ?
$this->setExtraInfos(); // Define with default value
}
return $this->extraInfos;
}
}