Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit f5f06ea

Browse files
Added more consice mime types to default standards (#396)
1 parent 23248f7 commit f5f06ea

File tree

8 files changed

+33
-11
lines changed

8 files changed

+33
-11
lines changed

src/FeedIo/FeedIo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ public function getPsrResponse(FeedInterface $feed, string $standard, int $maxAg
143143
{
144144
$this->logAction($feed, "creating a PSR 7 Response in $standard format");
145145

146-
$formatter = $this->specification->getStandard($standard)->getFormatter();
146+
$feedStandard = $this->specification->getStandard($standard);
147147
$responseBuilder = new ResponseBuilder($maxAge, $public);
148148

149-
return $responseBuilder->createResponse($standard, $formatter, $feed);
149+
return $responseBuilder->createResponse($feedStandard->getMimeType(), $feedStandard->getFormatter(), $feed);
150150
}
151151

152152
/**

src/FeedIo/Http/ResponseBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public function __construct(
2222
}
2323

2424
/**
25-
* @param string $format
25+
* @param string $mimeType
2626
* @param FormatterInterface $formatter
2727
* @param FeedInterface $feed
2828
* @return ResponseInterface
2929
*/
30-
public function createResponse(string $format, FormatterInterface $formatter, FeedInterface $feed): ResponseInterface
30+
public function createResponse(string $mimeType, FormatterInterface $formatter, FeedInterface $feed): ResponseInterface
3131
{
3232
$headers = [
33-
'Content-Type' => ($format === 'json') ? 'application/json' : 'application/xhtml+xml',
33+
'Content-Type' => $mimeType,
3434
'Cache-Control' => ($this->public ? 'public' : 'private') . ", max-age={$this->maxAge}",
3535
];
3636

src/FeedIo/Standard/Atom.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Atom extends XmlAbstract
2626

2727
public const DATETIME_FORMAT = \DateTime::ATOM;
2828

29+
public const MIME_TYPE = 'application/atom+xml';
30+
2931
public function format(DOMDocument $document): DOMDocument
3032
{
3133
$element = $document->createElement('feed');

src/FeedIo/Standard/Json.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Json extends StandardAbstract
1313
{
1414
public const SYNTAX_FORMAT = 'Json';
1515

16+
public const MIME_TYPE = 'application/feed+json';
17+
1618
protected array $mandatoryFields = ['version', 'title', 'items'];
1719

1820
/**

src/FeedIo/Standard/Rdf.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Rdf extends Rss
2727
*/
2828
public const DATE_NODE_TAGNAME = 'dc:date';
2929

30+
public const MIME_TYPE = 'application/rdf+xml';
31+
3032
/**
3133
* Tells if the parser can handle the feed or not
3234
* @param Document $document

src/FeedIo/Standard/Rss.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Rss extends XmlAbstract
4040
*/
4141
public const DATE_NODE_TAGNAME = 'pubDate';
4242

43+
public const MIME_TYPE = 'application/rss+xml';
44+
4345
protected array $mandatoryFields = ['channel'];
4446

4547
/**

src/FeedIo/StandardAbstract.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ abstract class StandardAbstract
1515
*/
1616
public const DATETIME_FORMAT = \DateTime::RFC2822;
1717

18+
/**
19+
* Standard mime type
20+
*/
21+
public const MIME_TYPE = '';
22+
1823
/**
1924
* Supported format
2025
*/
@@ -63,4 +68,13 @@ public function getSyntaxFormat(): string
6368
{
6469
return static::SYNTAX_FORMAT;
6570
}
71+
72+
/**
73+
* Returns the mime type for the standard
74+
* @return string
75+
*/
76+
public function getMimeType(): string
77+
{
78+
return static::MIME_TYPE;
79+
}
6680
}

tests/FeedIo/Http/ResponseBuilderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public function testCreateJsonResponse()
1919
$formatter = new JsonFormatter();
2020
$feed = $this->getFeed();
2121

22-
$response = $responseBuilder->createResponse('json', $formatter, $feed);
22+
$response = $responseBuilder->createResponse('application/feed+json', $formatter, $feed);
2323

2424
$headers = $response->getHeaders();
2525
$this->assertEquals(['Content-Type', 'Cache-Control', 'Last-Modified'], array_keys($headers));
26-
$this->assertEquals('application/json', $headers['Content-Type'][0]);
26+
$this->assertEquals('application/feed+json', $headers['Content-Type'][0]);
2727

2828
$body = $response->getBody()->getContents();
2929
$this->assertJson($body);
@@ -37,11 +37,11 @@ public function testCreateAtomResponse()
3737
$formatter = new XmlFormatter(new Atom($dateTimeBuilder));
3838
$feed = $this->getFeed();
3939

40-
$response = $responseBuilder->createResponse('atom', $formatter, $feed);
40+
$response = $responseBuilder->createResponse('application/atom+xml', $formatter, $feed);
4141

4242
$headers = $response->getHeaders();
4343
$this->assertEquals(['Content-Type', 'Cache-Control', 'Last-Modified'], array_keys($headers));
44-
$this->assertEquals('application/xhtml+xml', $headers['Content-Type'][0]);
44+
$this->assertEquals('application/atom+xml', $headers['Content-Type'][0]);
4545

4646
$body = $response->getBody()->getContents();
4747
$document = new \DOMDocument();
@@ -62,13 +62,13 @@ public function testResponseOnEmptyFeed()
6262
$feed->setUrl('http://localhost');
6363
$feed->setTitle('test feed');
6464

65-
$response = $responseBuilder->createResponse('atom', $formatter, $feed);
65+
$response = $responseBuilder->createResponse('application/atom+xml', $formatter, $feed);
6666

6767
$headers = $response->getHeaders();
6868
$headerNames = array_keys($headers);
6969
$this->assertEquals(['Content-Type', 'Cache-Control'], $headerNames);
7070
$this->assertArrayNotHasKey('Last-Modified', $headerNames);
71-
$this->assertEquals('application/xhtml+xml', $headers['Content-Type'][0]);
71+
$this->assertEquals('application/atom+xml', $headers['Content-Type'][0]);
7272

7373
$body = $response->getBody()->getContents();
7474
$document = new \DOMDocument();

0 commit comments

Comments
 (0)