romchik38/php-api

Framework-independent PHP library for standardized API responses

Maintainers

Package info

github.com/Romchik38/api

pkg:composer/romchik38/php-api

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-05-12 16:58 UTC

This package is auto-updated.

Last update: 2026-05-12 17:45:04 UTC


README

Framework-independent PHP library for standardized API responses.

Requirements

  • PHP 8.1+

Installation

composer require romchik38/php-api

Usage

Error response

use Romchik38\Api\ErrorDto;

$dto = new ErrorDto('Resource not found');
echo json_encode($dto);
// {"status":"error","result":"Resource not found"}

Success response

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"}}

Development

composer tests     # phpunit + phpstan + phpcs
composer cs-check  # coding standard check
composer cs-fix    # auto-fix coding standard