Skip to content

Commit 099a5a3

Browse files
committed
new resposne factory method
1 parent dc9d594 commit 099a5a3

11 files changed

Lines changed: 405 additions & 241 deletions

File tree

‎src/Data/Common/Response.php‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleAsFuck\ApiToolkit\Data\Common;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\StreamInterface;
9+
10+
abstract readonly class Response implements ResponseInterface
11+
{
12+
public function __construct(
13+
private ResponseInterface $response,
14+
) {
15+
}
16+
17+
public function withProtocolVersion(string $version): static
18+
{
19+
return $this->clone($this->response->withProtocolVersion($version));
20+
}
21+
22+
/**
23+
* @param string|array<string> $value
24+
*/
25+
public function withHeader(string $name, $value): static
26+
{
27+
return $this->clone($this->response->withHeader($name, $value));
28+
}
29+
30+
/**
31+
* @param string $name
32+
* @param string|array<string> $value
33+
*/
34+
public function withAddedHeader(string $name, $value): static
35+
{
36+
return $this->clone($this->response->withAddedHeader($name, $value));
37+
}
38+
39+
public function withoutHeader(string $name): static
40+
{
41+
return $this->clone($this->response->withoutHeader($name));
42+
}
43+
44+
public function withBody(StreamInterface $body): static
45+
{
46+
return $this->clone($this->response->withBody($body));
47+
}
48+
49+
public function withStatus(int $code, string $reasonPhrase = ''): static
50+
{
51+
return $this->clone($this->response->withStatus($code, $reasonPhrase));
52+
}
53+
54+
public function getProtocolVersion(): string
55+
{
56+
return $this->response->getProtocolVersion();
57+
}
58+
59+
/**
60+
* @return array<string, array<string>>
61+
*/
62+
public function getHeaders(): array
63+
{
64+
/** @var array<string, array<string>> */
65+
return $this->response->getHeaders();
66+
}
67+
68+
public function hasHeader(string $name): bool
69+
{
70+
return $this->response->hasHeader($name);
71+
}
72+
73+
/**
74+
* @return array<string>
75+
*/
76+
public function getHeader(string $name): array
77+
{
78+
return $this->response->getHeader($name);
79+
}
80+
81+
public function getHeaderLine(string $name): string
82+
{
83+
return $this->response->getHeaderLine($name);
84+
}
85+
86+
public function getBody(): StreamInterface
87+
{
88+
return $this->response->getBody();
89+
}
90+
91+
public function getStatusCode(): int
92+
{
93+
return $this->response->getStatusCode();
94+
}
95+
96+
public function getReasonPhrase(): string
97+
{
98+
return $this->response->getReasonPhrase();
99+
}
100+
101+
abstract protected function clone(ResponseInterface $response): static;
102+
}

‎src/Data/Server/Response.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleAsFuck\ApiToolkit\Data\Server;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
final readonly class Response extends \SimpleAsFuck\ApiToolkit\Data\Common\Response
10+
{
11+
/**
12+
* @param int $jsonEncodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php
13+
*/
14+
public function withJson(
15+
int $jsonEncodeFlags = 0,
16+
): ResponseJson {
17+
return new ResponseJson($this->withHeader('Content-Type', 'application/json'), $jsonEncodeFlags);
18+
}
19+
20+
/**
21+
* @param int $jsonEncodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php
22+
*/
23+
public function withJsonl(
24+
int $jsonEncodeFlags = 0,
25+
): ResponseStream {
26+
return new ResponseStream($this->withHeader('Content-Type', 'application/jsonl'), $jsonEncodeFlags);
27+
}
28+
29+
protected function clone(ResponseInterface $response): self
30+
{
31+
return new self($response);
32+
}
33+
}

‎src/Data/Server/ResponseArray.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleAsFuck\ApiToolkit\Data\Server;
6+
7+
use GuzzleHttp\Psr7\PumpStream;
8+
use Psr\Http\Message\ResponseInterface;
9+
use SimpleAsFuck\ApiToolkit\Service\Transformation\NotNull;
10+
use SimpleAsFuck\ApiToolkit\Service\Transformation\Transformer;
11+
12+
readonly class ResponseArray
13+
{
14+
/**
15+
* @param int $jsonEncodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php
16+
*/
17+
public function __construct(
18+
private ResponseInterface $response,
19+
private int $jsonEncodeFlags,
20+
) {
21+
}
22+
23+
/**
24+
* @template TBody
25+
* @param \Iterator<TBody> $body will be encoded as application/json array
26+
* @param Transformer<TBody>|null $transformer
27+
*/
28+
public function of(\Iterator $body, ?Transformer $transformer = null): ResponseInterface
29+
{
30+
$start = true;
31+
return $this->response->withBody(
32+
new PumpStream(function () use ($body, $transformer, &$start): ?string {
33+
$item = '';
34+
if ($start) {
35+
$start = false;
36+
if (! $body->valid()) {
37+
return '[]';
38+
}
39+
$item = '[';
40+
}
41+
42+
if (! $body->valid()) {
43+
return null;
44+
}
45+
46+
$item .= \json_encode(NotNull::toApi($body->current(), $transformer), $this->jsonEncodeFlags | \JSON_THROW_ON_ERROR);
47+
$body->next();
48+
if ($body->valid()) {
49+
$item .= ',';
50+
} else {
51+
$item .= ']';
52+
}
53+
54+
return $item;
55+
}),
56+
);
57+
}
58+
59+
/**
60+
* @template TBody
61+
* @param \Iterator<TBody> $body
62+
* @param Transformer<TBody> $transformer
63+
*/
64+
public function ofObject(\Iterator $body, Transformer $transformer): ResponseInterface
65+
{
66+
return $this->of($body, $transformer);
67+
}
68+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleAsFuck\ApiToolkit\Data\Server;
6+
7+
use GuzzleHttp\Psr7\PumpStream;
8+
use Psr\Http\Message\ResponseInterface;
9+
use SimpleAsFuck\ApiToolkit\Service\Transformation\NotNull;
10+
use SimpleAsFuck\ApiToolkit\Service\Transformation\Transformer;
11+
12+
final readonly class ResponseArrayAssoc extends ResponseArray
13+
{
14+
/**
15+
* @param int $jsonEncodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php
16+
*/
17+
public function __construct(
18+
private ResponseInterface $response,
19+
private int $jsonEncodeFlags,
20+
) {
21+
parent::__construct($this->response, $this->jsonEncodeFlags);
22+
}
23+
24+
/**
25+
* @template TBody
26+
* @param \Iterator<array-key, TBody> $body will be encoded as application/json object to preserve keys as properties in response
27+
* @param Transformer<TBody>|null $transformer
28+
*/
29+
public function of(\Iterator $body, ?Transformer $transformer = null): ResponseInterface
30+
{
31+
$start = true;
32+
return $this->response->withBody(
33+
new PumpStream(function () use ($body, $transformer, &$start): ?string {
34+
$item = '';
35+
if ($start) {
36+
$start = false;
37+
if (! $body->valid()) {
38+
return '{}';
39+
}
40+
$item = '{';
41+
}
42+
43+
if (! $body->valid()) {
44+
return null;
45+
}
46+
47+
$item .= \json_encode((string) $body->key(), \JSON_THROW_ON_ERROR) . ':' . \json_encode(NotNull::toApi($body->current(), $transformer), $this->jsonEncodeFlags | \JSON_THROW_ON_ERROR);
48+
$body->next();
49+
if ($body->valid()) {
50+
$item .= ',';
51+
} else {
52+
$item .= '}';
53+
}
54+
55+
return $item;
56+
})
57+
);
58+
}
59+
}

‎src/Data/Server/ResponseJson.php‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleAsFuck\ApiToolkit\Data\Server;
6+
7+
use GuzzleHttp\Psr7\Utils;
8+
use Psr\Http\Message\ResponseInterface;
9+
use SimpleAsFuck\ApiToolkit\Service\Transformation\Nullable;
10+
use SimpleAsFuck\ApiToolkit\Service\Transformation\Transformer;
11+
12+
final readonly class ResponseJson
13+
{
14+
/**
15+
* @param int $jsonEncodeFlags bitmask https://www.php.net/manual/en/function.json-decode.php
16+
*/
17+
public function __construct(
18+
private ResponseInterface $response,
19+
private int $jsonEncodeFlags,
20+
) {
21+
}
22+
23+
/**
24+
* @template TBody
25+
* @param TBody|null $body will be encoded as application/json
26+
* @param Transformer<TBody>|null $transformer
27+
*/
28+
public function nullable(mixed $body, ?Transformer $transformer = null): ResponseInterface
29+
{
30+
return $this->response->withBody(
31+
Utils::streamFor(
32+
\json_encode(
33+
Nullable::toApi($body, $transformer),
34+
$this->jsonEncodeFlags | \JSON_THROW_ON_ERROR,
35+
)
36+
)
37+
);
38+
}
39+
40+
/**
41+
* @template TBody
42+
* @param TBody $body will be encoded as application/json object
43+
* @param Transformer<TBody> $transformer
44+
*/
45+
public function object(mixed $body, Transformer $transformer): ResponseInterface
46+
{
47+
return $this->nullable($body, $transformer);
48+
}
49+
50+
public function array(): ResponseArray
51+
{
52+
return new ResponseArray($this->response, $this->jsonEncodeFlags);
53+
}
54+
55+
public function arrayAssoc(): ResponseArray
56+
{
57+
return new ResponseArrayAssoc($this->response, $this->jsonEncodeFlags);
58+
}
59+
}

‎src/Data/Server/ResponseStream.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleAsFuck\ApiToolkit\Data\Server;
6+
7+
use GuzzleHttp\Psr7\PumpStream;
8+
use Psr\Http\Message\ResponseInterface;
9+
use SimpleAsFuck\ApiToolkit\Service\Transformation\NotNull;
10+
use SimpleAsFuck\ApiToolkit\Service\Transformation\Transformer;
11+
12+
final readonly class ResponseStream extends ResponseArray
13+
{
14+
public function __construct(
15+
private ResponseInterface $response,
16+
private int $jsonEncodeFlags
17+
) {
18+
parent::__construct($response, $jsonEncodeFlags);
19+
}
20+
21+
/**
22+
* @template TBody
23+
* @param \Iterator<TBody> $body will be encoded as application/jsonl https://jsonlines.org/
24+
* @param Transformer<TBody>|null $transformer
25+
*/
26+
public function of(\Iterator $body, ?Transformer $transformer = null): ResponseInterface
27+
{
28+
return $this->response->withBody(
29+
new PumpStream(function () use ($body, $transformer): ?string {
30+
if (! $body->valid()) {
31+
return null;
32+
}
33+
34+
$responseData = NotNull::toApi($body->current(), $transformer);
35+
$body->next();
36+
return \json_encode($responseData, $this->jsonEncodeFlags | \JSON_THROW_ON_ERROR) . "\n";
37+
})
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)