-
Notifications
You must be signed in to change notification settings - Fork 0
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 #8 from tiny-blocks/release/2.1.0
Release/2.1.0
- Loading branch information
Showing
19 changed files
with
906 additions
and
7 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,34 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use TinyBlocks\Http\Internal\Response; | ||
|
||
/** | ||
* Define HTTP response following the PSR7 specification. | ||
* | ||
* @see https://www.php-fig.org/psr/psr-7 | ||
*/ | ||
final class HttpResponse | ||
{ | ||
public static function ok(mixed $data, array $headers = []): ResponseInterface | ||
{ | ||
return Response::from(code: HttpCode::OK, data: $data, headers: $headers); | ||
} | ||
|
||
public static function created(mixed $data, array $headers = []): ResponseInterface | ||
{ | ||
return Response::from(code: HttpCode::CREATED, data: $data, headers: $headers); | ||
} | ||
|
||
public static function accepted(mixed $data, array $headers = []): ResponseInterface | ||
{ | ||
return Response::from(code: HttpCode::ACCEPTED, data: $data, headers: $headers); | ||
} | ||
|
||
public static function noContent(array $headers = []): ResponseInterface | ||
{ | ||
return Response::from(code: HttpCode::NO_CONTENT, data: null, headers: $headers); | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal\Exceptions; | ||
|
||
use BadMethodCallException; | ||
|
||
final class BadMethodCall extends BadMethodCallException | ||
{ | ||
public function __construct(private readonly string $method) | ||
{ | ||
$template = 'Method <%s> cannot be used.'; | ||
parent::__construct(message: sprintf($template, $this->method)); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
final class MissingResourceStream extends RuntimeException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(message: 'No resource available.'); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
final class NonReadableStream extends RuntimeException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(message: 'Stream is not readable.'); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
final class NonSeekableStream extends RuntimeException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(message: 'Stream is not seekable.'); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
final class NonWritableStream extends RuntimeException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(message: 'Stream is not writable.'); | ||
} | ||
} |
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,97 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal; | ||
|
||
use Psr\Http\Message\MessageInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use TinyBlocks\Http\HttpCode; | ||
use TinyBlocks\Http\Internal\Exceptions\BadMethodCall; | ||
use TinyBlocks\Http\Internal\Stream\StreamFactory; | ||
|
||
final class Response implements ResponseInterface | ||
{ | ||
private function __construct( | ||
private readonly HttpCode $code, | ||
private readonly StreamInterface $body, | ||
private readonly array $headers | ||
) { | ||
} | ||
|
||
public static function from(HttpCode $code, mixed $data, array $headers): ResponseInterface | ||
{ | ||
$headers = empty($headers) ? ['Content-Type' => 'application/json'] : $headers; | ||
|
||
return new Response(code: $code, body: StreamFactory::from(data: $data), headers: $headers); | ||
} | ||
|
||
public function withBody(StreamInterface $body): MessageInterface | ||
{ | ||
throw new BadMethodCall(method: __METHOD__); | ||
} | ||
|
||
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface | ||
{ | ||
throw new BadMethodCall(method: __METHOD__); | ||
} | ||
|
||
public function withHeader(string $name, mixed $value): MessageInterface | ||
{ | ||
throw new BadMethodCall(method: __METHOD__); | ||
} | ||
|
||
public function withoutHeader(string $name): MessageInterface | ||
{ | ||
throw new BadMethodCall(method: __METHOD__); | ||
} | ||
|
||
public function withAddedHeader(string $name, mixed $value): MessageInterface | ||
{ | ||
throw new BadMethodCall(method: __METHOD__); | ||
} | ||
|
||
public function withProtocolVersion(string $version): MessageInterface | ||
{ | ||
throw new BadMethodCall(method: __METHOD__); | ||
} | ||
|
||
public function getProtocolVersion(): string | ||
{ | ||
return '1.1'; | ||
} | ||
|
||
public function getHeaders(): array | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
public function hasHeader(string $name): bool | ||
{ | ||
return isset($this->headers[$name]); | ||
} | ||
|
||
public function getHeader(string $name): array | ||
{ | ||
return $this->headers[$name] ?? []; | ||
} | ||
|
||
public function getHeaderLine(string $name): string | ||
{ | ||
return implode(', ', $this->getHeader(name: $name)); | ||
} | ||
|
||
public function getBody(): StreamInterface | ||
{ | ||
return $this->body; | ||
} | ||
|
||
public function getStatusCode(): int | ||
{ | ||
return $this->code->value; | ||
} | ||
|
||
public function getReasonPhrase(): string | ||
{ | ||
return $this->code->message(); | ||
} | ||
} |
Oops, something went wrong.