Files
project-manager/src/Http/Controllers/Controller.php
T

56 lines
1.4 KiB
PHP
Raw Normal View History

2026-09-03 17:35:10 +01:00
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
2026-09-03 18:30:52 +01:00
use Psr\Http\Message\ServerRequestInterface as Request;
2026-09-03 17:35:10 +01:00
/**
* Shared helpers for HTTP controllers.
*/
abstract class Controller
{
/**
* Write a JSON body and return the response with the appropriate headers.
*
* @param array<string, mixed> $data
*/
protected function json(Response $response, array $data, int $status = 200): Response
{
$response->getBody()->write(
(string) json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
);
return $response
->withHeader('Content-Type', 'application/json')
->withStatus($status);
}
2026-09-03 18:30:52 +01:00
/**
* The parsed JSON request body as an array (empty when absent or not an object).
*
* @return array<string, mixed>
*/
protected function body(Request $request): array
{
$parsed = $request->getParsedBody();
return is_array($parsed) ? $parsed : [];
}
/**
* The authenticated user attached by AuthMiddleware.
*
* @return array{id: int, email: string, email_verified_at: string|null, created_at: string}
*/
protected function user(Request $request): array
{
/** @var array{id: int, email: string, email_verified_at: string|null, created_at: string} $user */
$user = $request->getAttribute('user');
return $user;
}
2026-09-03 17:35:10 +01:00
}