Skip to content

Commit

Permalink
support iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 7, 2023
1 parent d39e05a commit 29ddf37
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 46 deletions.
6 changes: 5 additions & 1 deletion src/Response/Handler/JsonResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public function handle(ResponseType $responseType): ResponseInterface
}

$payload = $responseType->getPayload();
if (is_iterable($payload) && !\is_array($payload)) {
$payload = iterator_to_array($payload);
}

if (!$this->isJsonEncodable($payload)) {
throw new InvalidArgumentException('Response type payload is not json encodable.');
throw new InvalidArgumentException('Response type payload is not JSON encodable.');
}

$response = $this->getResponse($responseType);
Expand Down
7 changes: 6 additions & 1 deletion src/Response/Handler/TwigViewResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public function handle(ResponseType $responseType): ResponseInterface
);
}

$responseContent = $this->viewRenderer->fetch($responseType->getTemplate(), $responseType->getParameters());
$parameters = $responseType->getParameters();
if (!\is_array($parameters)) {
$parameters = iterator_to_array($parameters);
}

$responseContent = $this->viewRenderer->fetch($responseType->getTemplate(), $parameters);

$response = $this->getResponse($responseType);
$response->getBody()
Expand Down
17 changes: 13 additions & 4 deletions src/Response/Handler/XmlResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ public function handle(ResponseType $responseType): ResponseInterface
);
}

$converter = new ArrayToXml($responseType->getPayload(), '', false);
$responseContent = $this->prettify ? $this->prettify($converter) : $this->asSingleLine($converter);
$payload = $responseType->getPayload();
if (is_iterable($payload) && !\is_array($payload)) {
$payload = iterator_to_array($payload);
}

if (!\is_array($payload)) {
throw new InvalidArgumentException('Response type payload is not XML encodable.');
}

$converter = new ArrayToXml($payload, '', false);
$responseContent = $this->prettify ? $this->getPrettified($converter) : $this->getCompressed($converter);

$response = $this->getResponse($responseType);
$response->getBody()
Expand All @@ -50,7 +59,7 @@ public function handle(ResponseType $responseType): ResponseInterface
/**
* Return XML in a single line.
*/
private function asSingleLine(ArrayToXml $converter): string
private function getCompressed(ArrayToXml $converter): string
{
$xmlLines = explode("\n", $converter->toXml());
array_walk(
Expand All @@ -64,7 +73,7 @@ private function asSingleLine(ArrayToXml $converter): string
/**
* Prettify xml output.
*/
private function prettify(ArrayToXml $converter): string
private function getPrettified(ArrayToXml $converter): string
{
$domDocument = $converter->toDom();
$domDocument->formatOutput = true;
Expand Down
10 changes: 2 additions & 8 deletions src/Response/PayloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@
final class PayloadResponse extends AbstractResponse
{
public function __construct(
/**
* @var array<mixed> $payload
*/
protected array $payload,
protected mixed $payload,
ServerRequestInterface $request,
?ResponseInterface $response = null,
) {
parent::__construct($request, $response);
}

/**
* @return array<mixed>
*/
public function getPayload(): array
public function getPayload(): mixed
{
return $this->payload;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Response/ViewResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ final class ViewResponse extends AbstractResponse
public function __construct(
protected string $template,
/**
* @var array<string, mixed>
* @var iterable<string, mixed>
*/
protected array $parameters,
protected iterable $parameters,
ServerRequestInterface $request,
?ResponseInterface $response = null,
) {
Expand All @@ -36,9 +36,9 @@ public function getTemplate(): string
}

/**
* @return array<string, mixed>
* @return iterable<string, mixed>
*/
public function getParameters(): array
public function getParameters(): iterable
{
return $this->parameters;
}
Expand Down
10 changes: 4 additions & 6 deletions tests/Routing/Response/Handler/JsonResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Jgut\Slim\Routing\Tests\Response\Handler;

use ArrayIterator;
use InvalidArgumentException;
use Jgut\Slim\Routing\Response\Handler\JsonResponseHandler;
use Jgut\Slim\Routing\Response\PayloadResponse;
use Jgut\Slim\Routing\Tests\Stubs\ResponseStub;
use Laminas\Diactoros\ResponseFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testInvalidResponseType(): void
public function testNonEncodableResponseType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Response type payload is not json encodable');
$this->expectExceptionMessage('Response type payload is not JSON encodable');

$responseFactory = $this->getMockBuilder(ResponseFactoryInterface::class)
->getMock();
Expand All @@ -61,14 +61,13 @@ public function testNonEncodableResponseType(): void
->handle(new PayloadResponse(['data' => fopen('php://stdout', 'rb')], $this->request));
}

public function testHandleCollapsed(): void
public function testHandleCompressed(): void
{
$responseFactory = new ResponseFactory();

$response = (new JsonResponseHandler($responseFactory))
->handle(new PayloadResponse(['data' => ['param' => 'value']], $this->request));

static::assertInstanceOf(ResponseInterface::class, $response);
static::assertEquals('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
static::assertEquals('{"data":{"param":"value"}}', (string) $response->getBody());
}
Expand All @@ -78,9 +77,8 @@ public function testHandlePrettified(): void
$responseFactory = new ResponseFactory();

$response = (new JsonResponseHandler($responseFactory, true))
->handle(new PayloadResponse(['data' => ['param' => 'value']], $this->request));
->handle(new PayloadResponse(new ArrayIterator(['data' => ['param' => 'value']]), $this->request));

static::assertInstanceOf(ResponseInterface::class, $response);
static::assertEquals('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));

$responseContent = <<<'JSON'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Jgut\Slim\Routing\Tests\Response\Handler;

use ArrayIterator;
use InvalidArgumentException;
use Jgut\Slim\Routing\Response\Handler\TwigViewResponseHandler;
use Jgut\Slim\Routing\Response\ViewResponse;
use Jgut\Slim\Routing\Tests\Stubs\ResponseStub;
use Laminas\Diactoros\ResponseFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

Expand Down Expand Up @@ -53,7 +53,7 @@ public function testInvalidResponseType(): void
(new TwigViewResponseHandler($responseFactory, $twig))->handle(new ResponseStub($this->request));
}

public function testHandlePrettified(): void
public function testHandle(): void
{
$responseFactory = new ResponseFactory();
$twig = $this->getMockBuilder(Twig::class)
Expand All @@ -64,9 +64,8 @@ public function testHandlePrettified(): void
->willReturn('Template rendered!');

$response = (new TwigViewResponseHandler($responseFactory, $twig))
->handle(new ViewResponse('template.twig', [], $this->request));
->handle(new ViewResponse('user.twig', new ArrayIterator(['id' => null]), $this->request));

static::assertInstanceOf(ResponseInterface::class, $response);
static::assertEquals('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
static::assertEquals('Template rendered!', (string) $response->getBody());
}
Expand Down
40 changes: 22 additions & 18 deletions tests/Routing/Response/Handler/XmlResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace Jgut\Slim\Routing\Tests\Response\Handler;

use ArrayIterator;
use InvalidArgumentException;
use Jgut\Slim\Routing\Response\Handler\XmlResponseHandler;
use Jgut\Slim\Routing\Response\PayloadResponse;
use Jgut\Slim\Routing\Tests\Stubs\ResponseStub;
use Laminas\Diactoros\ResponseFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
Expand Down Expand Up @@ -49,21 +49,29 @@ public function testInvalidResponseType(): void
(new XmlResponseHandler($responseFactory))->handle(new ResponseStub($this->request));
}

public function testHandleCollapsed(): void
public function testNonEncodableResponseType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Response type payload is not XML encodable');

$responseFactory = $this->getMockBuilder(ResponseFactoryInterface::class)
->getMock();

(new XmlResponseHandler($responseFactory))
->handle(new PayloadResponse(fopen('php://stdout', 'rb'), $this->request));
}

public function testHandleCompressed(): void
{
$responseFactory = new ResponseFactory();

$response = (new XmlResponseHandler($responseFactory))
->handle(new PayloadResponse(
[
'data' => [
'param' => 'value',
],
->handle(new PayloadResponse([
'data' => [
'param' => 'value',
],
$this->request,
));
], $this->request));

static::assertInstanceOf(ResponseInterface::class, $response);
static::assertEquals('application/xml; charset=utf-8', $response->getHeaderLine('Content-Type'));
static::assertEquals(
'<?xml version="1.0"?><root><data><param>value</param></data></root>',
Expand All @@ -76,16 +84,12 @@ public function testHandlePrettified(): void
$responseFactory = new ResponseFactory();

$response = (new XmlResponseHandler($responseFactory, true))
->handle(new PayloadResponse(
[
'data' => [
'param' => 'value',
],
->handle(new PayloadResponse(new ArrayIterator([
'data' => [
'param' => 'value',
],
$this->request,
));
]), $this->request));

static::assertInstanceOf(ResponseInterface::class, $response);
static::assertEquals('application/xml; charset=utf-8', $response->getHeaderLine('Content-Type'));

$responseContent = <<<'XML'
Expand Down

0 comments on commit 29ddf37

Please sign in to comment.