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:
41
src/flextype/core/Endpoints/Endpoints.php
Normal file
41
src/flextype/core/Endpoints/Endpoints.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user