formats: Add getMimeType() function (#1299)

Allows getting the expected MIME type of the format's output. A
corresponding MIME_TYPE constant is also defined in FormatAbstract for
the format implementations to overwrite.
This commit is contained in:
Roliga 2019-10-31 19:00:12 +01:00 committed by LogMANOriginal
parent d1e4bd7285
commit c8d5c85c76
7 changed files with 30 additions and 5 deletions

View file

@ -7,6 +7,8 @@
* https://validator.w3.org/feed/ * https://validator.w3.org/feed/
*/ */
class AtomFormat extends FormatAbstract{ class AtomFormat extends FormatAbstract{
const MIME_TYPE = 'application/atom+xml';
const LIMIT_TITLE = 140; const LIMIT_TITLE = 140;
public function stringify(){ public function stringify(){
@ -147,7 +149,7 @@ EOD;
public function display(){ public function display(){
$this $this
->setContentType('application/atom+xml; charset=' . $this->getCharset()) ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -1,5 +1,7 @@
<?php <?php
class HtmlFormat extends FormatAbstract { class HtmlFormat extends FormatAbstract {
const MIME_TYPE = 'text/html';
public function stringify(){ public function stringify(){
$extraInfos = $this->getExtraInfos(); $extraInfos = $this->getExtraInfos();
$title = htmlspecialchars($extraInfos['name']); $title = htmlspecialchars($extraInfos['name']);
@ -120,7 +122,7 @@ EOD;
public function display() { public function display() {
$this $this
->setContentType('text/html; charset=' . $this->getCharset()) ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -8,6 +8,8 @@
* https://github.com/vigetlabs/json-feed-validator * https://github.com/vigetlabs/json-feed-validator
*/ */
class JsonFormat extends FormatAbstract { class JsonFormat extends FormatAbstract {
const MIME_TYPE = 'application/json';
const VENDOR_EXCLUDES = array( const VENDOR_EXCLUDES = array(
'author', 'author',
'title', 'title',
@ -119,7 +121,7 @@ class JsonFormat extends FormatAbstract {
public function display(){ public function display(){
$this $this
->setContentType('application/json; charset=' . $this->getCharset()) ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -25,6 +25,8 @@
* RSS 2.0 feed that works with feed readers that don't support the extension. * RSS 2.0 feed that works with feed readers that don't support the extension.
*/ */
class MrssFormat extends FormatAbstract { class MrssFormat extends FormatAbstract {
const MIME_TYPE = 'application/rss+xml';
const ALLOWED_IMAGE_EXT = array( const ALLOWED_IMAGE_EXT = array(
'.gif', '.jpg', '.png' '.gif', '.jpg', '.png'
); );
@ -150,7 +152,7 @@ EOD;
public function display(){ public function display(){
$this $this
->setContentType('application/rss+xml; charset=' . $this->getCharset()) ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -4,6 +4,8 @@
* Returns $this->items as raw php data. * Returns $this->items as raw php data.
*/ */
class PlaintextFormat extends FormatAbstract { class PlaintextFormat extends FormatAbstract {
const MIME_TYPE = 'text/plain';
public function stringify(){ public function stringify(){
$items = $this->getItems(); $items = $this->getItems();
$data = array(); $data = array();
@ -22,7 +24,7 @@ class PlaintextFormat extends FormatAbstract {
public function display(){ public function display(){
$this $this
->setContentType('text/plain; charset=' . $this->getCharset()) ->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
->callContentType(); ->callContentType();
return parent::display(); return parent::display();

View file

@ -21,6 +21,9 @@ abstract class FormatAbstract implements FormatInterface {
/** The default charset (UTF-8) */ /** The default charset (UTF-8) */
const DEFAULT_CHARSET = 'UTF-8'; const DEFAULT_CHARSET = 'UTF-8';
/** MIME type of format output */
const MIME_TYPE = 'text/plain';
/** @var string|null $contentType The content type */ /** @var string|null $contentType The content type */
protected $contentType = null; protected $contentType = null;
@ -39,6 +42,11 @@ abstract class FormatAbstract implements FormatInterface {
/** @var array $extraInfos The extra infos */ /** @var array $extraInfos The extra infos */
protected $extraInfos; protected $extraInfos;
/** {@inheritdoc} */
public function getMimeType(){
return static::MIME_TYPE;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *

View file

@ -66,6 +66,13 @@ interface FormatInterface {
*/ */
public function getExtraInfos(); public function getExtraInfos();
/**
* Return MIME type
*
* @return string The MIME type
*/
public function getMimeType();
/** /**
* Set charset * Set charset
* *