Framework-independent PHP library for standardized API responses.
- PHP 8.1+
composer require romchik38/php-apiuse Romchik38\Api\ErrorDto;
$dto = new ErrorDto('Resource not found');
echo json_encode($dto);
// {"status":"error","result":"Resource not found"}use JsonSerializable;
use Romchik38\Api\SuccessDto;
class UserView implements JsonSerializable
{
public function __construct(
public readonly int $id,
public readonly string $name,
) {}
public function jsonSerialize(): mixed
{
return ['id' => $this->id, 'name' => $this->name];
}
}
$dto = new SuccessDto(new UserView(1, 'Alice'));
echo json_encode($dto);
// {"status":"success","result":{"id":1,"name":"Alice"}}composer tests # phpunit + phpstan + phpcs
composer cs-check # coding standard check
composer cs-fix # auto-fix coding standard