-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from hiqsol/allow-raw-formatter
Change `Formatter` interface to allow returning complete PSR 7 response
- Loading branch information
Showing
20 changed files
with
183 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use Lcobucci\ContentNegotiation\UnformattedResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
|
||
abstract class ContentOnly implements Formatter | ||
{ | ||
/** @throws ContentCouldNotBeFormatted */ | ||
public function format(UnformattedResponse $response, StreamFactoryInterface $streamFactory): ResponseInterface | ||
{ | ||
return $response->withBody( | ||
$streamFactory->createStream( | ||
$this->formatContent($response->getUnformattedContent(), $response->getAttributes()) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed $content | ||
* @param mixed[] $attributes | ||
*/ | ||
abstract public function formatContent($content, array $attributes = []): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Fig\Http\Message\StatusCodeInterface; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use Lcobucci\ContentNegotiation\UnformattedResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
|
||
class NotAcceptable implements Formatter | ||
{ | ||
public function format(UnformattedResponse $response, StreamFactoryInterface $streamFactory): ResponseInterface | ||
{ | ||
return $response->withBody($streamFactory->createStream()) | ||
->withStatus(StatusCodeInterface::STATUS_NOT_ACCEPTABLE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use Laminas\Diactoros\Response; | ||
use Laminas\Diactoros\StreamFactory; | ||
use Lcobucci\ContentNegotiation\Formatter\ContentOnly; | ||
use Lcobucci\ContentNegotiation\UnformattedResponse; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\ContentOnly | ||
*/ | ||
final class ContentOnlyTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @covers ::format | ||
* | ||
* @uses \Lcobucci\ContentNegotiation\UnformattedResponse | ||
*/ | ||
public function formatShouldDelegateTheFormattingToTheConcreteClasses(): void | ||
{ | ||
$formatter = $this->getMockForAbstractClass(ContentOnly::class); | ||
$formatter->method('formatContent')->willReturn('A fancy result'); | ||
|
||
$response = $formatter->format(new UnformattedResponse(new Response(), 'testing'), new StreamFactory()); | ||
|
||
self::assertSame('A fancy result', $response->getBody()->getContents()); | ||
} | ||
} |
Oops, something went wrong.