Skip to content

Romchik38/api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

romchik38/php-api

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

About

Framework-independent PHP library for standardized API responses

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages