From f5f06ea051e9751680430831d48052195670055d Mon Sep 17 00:00:00 2001 From: Boris Glumpler Date: Thu, 14 Apr 2022 13:37:19 +0200 Subject: [PATCH] Added more consice mime types to default standards (#396) --- src/FeedIo/FeedIo.php | 4 ++-- src/FeedIo/Http/ResponseBuilder.php | 6 +++--- src/FeedIo/Standard/Atom.php | 2 ++ src/FeedIo/Standard/Json.php | 2 ++ src/FeedIo/Standard/Rdf.php | 2 ++ src/FeedIo/Standard/Rss.php | 2 ++ src/FeedIo/StandardAbstract.php | 14 ++++++++++++++ tests/FeedIo/Http/ResponseBuilderTest.php | 12 ++++++------ 8 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/FeedIo/FeedIo.php b/src/FeedIo/FeedIo.php index ef0b91b..1bf3e00 100644 --- a/src/FeedIo/FeedIo.php +++ b/src/FeedIo/FeedIo.php @@ -143,10 +143,10 @@ public function getPsrResponse(FeedInterface $feed, string $standard, int $maxAg { $this->logAction($feed, "creating a PSR 7 Response in $standard format"); - $formatter = $this->specification->getStandard($standard)->getFormatter(); + $feedStandard = $this->specification->getStandard($standard); $responseBuilder = new ResponseBuilder($maxAge, $public); - return $responseBuilder->createResponse($standard, $formatter, $feed); + return $responseBuilder->createResponse($feedStandard->getMimeType(), $feedStandard->getFormatter(), $feed); } /** diff --git a/src/FeedIo/Http/ResponseBuilder.php b/src/FeedIo/Http/ResponseBuilder.php index 5d0838d..97de032 100644 --- a/src/FeedIo/Http/ResponseBuilder.php +++ b/src/FeedIo/Http/ResponseBuilder.php @@ -22,15 +22,15 @@ public function __construct( } /** - * @param string $format + * @param string $mimeType * @param FormatterInterface $formatter * @param FeedInterface $feed * @return ResponseInterface */ - public function createResponse(string $format, FormatterInterface $formatter, FeedInterface $feed): ResponseInterface + public function createResponse(string $mimeType, FormatterInterface $formatter, FeedInterface $feed): ResponseInterface { $headers = [ - 'Content-Type' => ($format === 'json') ? 'application/json' : 'application/xhtml+xml', + 'Content-Type' => $mimeType, 'Cache-Control' => ($this->public ? 'public' : 'private') . ", max-age={$this->maxAge}", ]; diff --git a/src/FeedIo/Standard/Atom.php b/src/FeedIo/Standard/Atom.php index 5f473ef..7e7abf4 100644 --- a/src/FeedIo/Standard/Atom.php +++ b/src/FeedIo/Standard/Atom.php @@ -26,6 +26,8 @@ class Atom extends XmlAbstract public const DATETIME_FORMAT = \DateTime::ATOM; + public const MIME_TYPE = 'application/atom+xml'; + public function format(DOMDocument $document): DOMDocument { $element = $document->createElement('feed'); diff --git a/src/FeedIo/Standard/Json.php b/src/FeedIo/Standard/Json.php index 30ebc32..cd591c0 100644 --- a/src/FeedIo/Standard/Json.php +++ b/src/FeedIo/Standard/Json.php @@ -13,6 +13,8 @@ class Json extends StandardAbstract { public const SYNTAX_FORMAT = 'Json'; + public const MIME_TYPE = 'application/feed+json'; + protected array $mandatoryFields = ['version', 'title', 'items']; /** diff --git a/src/FeedIo/Standard/Rdf.php b/src/FeedIo/Standard/Rdf.php index 7af1047..0ceb582 100644 --- a/src/FeedIo/Standard/Rdf.php +++ b/src/FeedIo/Standard/Rdf.php @@ -27,6 +27,8 @@ class Rdf extends Rss */ public const DATE_NODE_TAGNAME = 'dc:date'; + public const MIME_TYPE = 'application/rdf+xml'; + /** * Tells if the parser can handle the feed or not * @param Document $document diff --git a/src/FeedIo/Standard/Rss.php b/src/FeedIo/Standard/Rss.php index a1a943b..27a049f 100644 --- a/src/FeedIo/Standard/Rss.php +++ b/src/FeedIo/Standard/Rss.php @@ -40,6 +40,8 @@ class Rss extends XmlAbstract */ public const DATE_NODE_TAGNAME = 'pubDate'; + public const MIME_TYPE = 'application/rss+xml'; + protected array $mandatoryFields = ['channel']; /** diff --git a/src/FeedIo/StandardAbstract.php b/src/FeedIo/StandardAbstract.php index 9f748e5..4da16ee 100644 --- a/src/FeedIo/StandardAbstract.php +++ b/src/FeedIo/StandardAbstract.php @@ -15,6 +15,11 @@ abstract class StandardAbstract */ public const DATETIME_FORMAT = \DateTime::RFC2822; + /** + * Standard mime type + */ + public const MIME_TYPE = ''; + /** * Supported format */ @@ -63,4 +68,13 @@ public function getSyntaxFormat(): string { return static::SYNTAX_FORMAT; } + + /** + * Returns the mime type for the standard + * @return string + */ + public function getMimeType(): string + { + return static::MIME_TYPE; + } } diff --git a/tests/FeedIo/Http/ResponseBuilderTest.php b/tests/FeedIo/Http/ResponseBuilderTest.php index aba1a32..e13620b 100644 --- a/tests/FeedIo/Http/ResponseBuilderTest.php +++ b/tests/FeedIo/Http/ResponseBuilderTest.php @@ -19,11 +19,11 @@ public function testCreateJsonResponse() $formatter = new JsonFormatter(); $feed = $this->getFeed(); - $response = $responseBuilder->createResponse('json', $formatter, $feed); + $response = $responseBuilder->createResponse('application/feed+json', $formatter, $feed); $headers = $response->getHeaders(); $this->assertEquals(['Content-Type', 'Cache-Control', 'Last-Modified'], array_keys($headers)); - $this->assertEquals('application/json', $headers['Content-Type'][0]); + $this->assertEquals('application/feed+json', $headers['Content-Type'][0]); $body = $response->getBody()->getContents(); $this->assertJson($body); @@ -37,11 +37,11 @@ public function testCreateAtomResponse() $formatter = new XmlFormatter(new Atom($dateTimeBuilder)); $feed = $this->getFeed(); - $response = $responseBuilder->createResponse('atom', $formatter, $feed); + $response = $responseBuilder->createResponse('application/atom+xml', $formatter, $feed); $headers = $response->getHeaders(); $this->assertEquals(['Content-Type', 'Cache-Control', 'Last-Modified'], array_keys($headers)); - $this->assertEquals('application/xhtml+xml', $headers['Content-Type'][0]); + $this->assertEquals('application/atom+xml', $headers['Content-Type'][0]); $body = $response->getBody()->getContents(); $document = new \DOMDocument(); @@ -62,13 +62,13 @@ public function testResponseOnEmptyFeed() $feed->setUrl('http://localhost'); $feed->setTitle('test feed'); - $response = $responseBuilder->createResponse('atom', $formatter, $feed); + $response = $responseBuilder->createResponse('application/atom+xml', $formatter, $feed); $headers = $response->getHeaders(); $headerNames = array_keys($headers); $this->assertEquals(['Content-Type', 'Cache-Control'], $headerNames); $this->assertArrayNotHasKey('Last-Modified', $headerNames); - $this->assertEquals('application/xhtml+xml', $headers['Content-Type'][0]); + $this->assertEquals('application/atom+xml', $headers['Content-Type'][0]); $body = $response->getBody()->getContents(); $document = new \DOMDocument();