1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 13:46:42 +02:00

feat(endpoints): add basic Endpoints class #565

This commit is contained in:
Awilum
2021-08-10 12:02:38 +03:00
parent b38bbf3366
commit 587a2c83ec

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Endpoints;
use Psr\Http\Message\ResponseInterface;
class Endpoints
{
private array $statusCodeMessages = [
'400' => [
'title' => 'Bad Request',
'message' => 'Validation for this particular item failed',
],
'401' => [
'title' => 'Unauthorized',
'message' => 'Token is wrong',
],
'404' => [
'title' => 'Not Found',
'message' => 'Not Found',
]
];
public function getApiResponse(ResponseInterface $response, array $body = [], int $status = 200): ResponseInterface
{
if (count($body) > 0) {
$response->getBody()->write(serializers()->json()->encode($body));
}
$response->withStatus($status);
$response->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset'));
return $response;
}
}