Skip to content

Commit

Permalink
Added more consice mime types to default standards (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-glumpler authored Apr 14, 2022
1 parent 23248f7 commit f5f06ea
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/FeedIo/FeedIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/FeedIo/Http/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
];

Expand Down
2 changes: 2 additions & 0 deletions src/FeedIo/Standard/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions src/FeedIo/Standard/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

/**
Expand Down
2 changes: 2 additions & 0 deletions src/FeedIo/Standard/Rdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/FeedIo/Standard/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

/**
Expand Down
14 changes: 14 additions & 0 deletions src/FeedIo/StandardAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ abstract class StandardAbstract
*/
public const DATETIME_FORMAT = \DateTime::RFC2822;

/**
* Standard mime type
*/
public const MIME_TYPE = '';

/**
* Supported format
*/
Expand Down Expand Up @@ -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;
}
}
12 changes: 6 additions & 6 deletions tests/FeedIo/Http/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit f5f06ea

Please sign in to comment.