PHP code example of hongxunpan / simple-api

1. Go to this page and download the library: Download hongxunpan/simple-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hongxunpan / simple-api example snippets


use HongXunPan\SimpleApi\Response\StandardResponseContent;

final class ProjectResponseContent extends StandardResponseContent
{
    public function toArray(): array
    {
        return parent::toArray() + [
            'info' => [
                'version' => config('app.version'),
            ],
        ];
    }
}

use HongXunPan\SimpleApi\Response\ResponseContentContract;
use HongXunPan\SimpleApi\Response\StandardJsonResponse;

final class ProjectResponse extends StandardJsonResponse
{
    protected function createContent(mixed $content): ResponseContentContract
    {
        return new ProjectResponseContent($content);
    }
}

use HongXunPan\SimpleApi\Exception\ExceptionMapper;
use HongXunPan\SimpleApi\Exception\ExceptionResponse;
use HongXunPan\SimpleApi\Response\StandardResponseContent;
use Throwable;

final class ProjectExceptionMapper implements ExceptionMapper
{
    public function map(Throwable $throwable): ?ExceptionResponse
    {
        if ($throwable instanceof ProjectApiException) {
            return new ExceptionResponse(
                status: 200,
                body: new StandardResponseContent(
                    data: $throwable->getData(),
                    code: $throwable->getCode(),
                    message: $throwable->getMessage(),
                ),
            );
        }

        return new ExceptionResponse(
            status: 200,
            body: new StandardResponseContent(
                data: [],
                code: -1,
                message: 'Server Error',
            ),
        );
    }
}

use HongXunPan\SimpleApi\Exception\ExceptionMapperRegistry;

$app->make(ExceptionMapperRegistry::class)->append(
    $app->make(ProjectExceptionMapper::class),
);
bash
composer le module:enable api