PHP code example of everlution / simple-rest-api

1. Go to this page and download the library: Download everlution/simple-rest-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/ */

    

everlution / simple-rest-api example snippets




namespace App\Api\Sms;

use App\ApiBusinessLogic\Sms\SendApiBusinessLogic;
use App\JsonSchema\Api\Sms\Send\RequestJsonSchema;
use App\JsonSchema\Api\Sms\Send\ResponseJsonSchema;
use Everlution\SimpleRestApi\Api\AbstractApi;
use Everlution\SimpleRestApi\Api\JsonSchemaApiInterface;
use Everlution\SimpleRestApi\ApiRequestHandler\DefaultApiRequestHandler;
use Everlution\SimpleRestApi\ApiValidator\JsonSchemaApiValidator;
use Symfony\Component\HttpFoundation\Request;

class SendApi extends AbstractApi implements JsonSchemaApiInterface
{
    private $requestJsonSchema;

    private $responseJsonSchema;

    public function __construct(
        JsonSchemaApiValidator $apiValidator,
        DefaultApiRequestHandler $apiRequestHandler,
        SendApiBusinessLogic $apiBusinessLogic,
        RequestJsonSchema $requestJsonSchema,
        ResponseJsonSchema $responseJsonSchema
    ) {
        parent::__construct($apiValidator, $apiRequestHandler, $apiBusinessLogic);

        $this->requestJsonSchema = $requestJsonSchema;
        $this->responseJsonSchema = $responseJsonSchema;
    }

    public function getTitle(): string
    {
        return 'SMS Send';
    }

    public function isDeprecated(): bool
    {
        return false;
    }

    public static function getMethods(): array
    {
        return [Request::METHOD_POST];
    }

    public static function getRoutesPaths(): array
    {
        return [
            '/sms/send',
        ];
    }

    public function isEnabled(): bool
    {
        return true;
    }

    public function getStatusCodes(): array
    {
        return parent::getStatusCodes() + [401 => 'Unauthorized'];
    }

    public function getRequestJsonSchema(): string
    {
        return $this->requestJsonSchema->generate();
    }

    public function getResponseJsonSchema(): string
    {
        return $this->responseJsonSchema->generate();
    }
}




namespace App\JsonSchema\Api\Sms\Send;

class RequestJsonSchema
{
    public function toArray(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'number' => [
                    'type' => 'string',
                    'description' => 'The phone number',
                ],
                'text' => [
                    'type' => 'string',
                    'description' => 'The content of the message',
                    'maxLength' => 255
                ]
            ]
        ];
    }
    
    public function generate(): string
    {
        return json_encode($this->toArray());
    }
}




namespace App\ApiBusinessLogic\Sms;

use Everlution\SimpleRestApi\Api\ApiInterface;
use Everlution\SimpleRestApi\ApiBusinessLogic\ApiBusinessLogicInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class SendApiBusinessLogic implements ApiBusinessLogicInterface
{
    public function execute(ApiInterface $api, Request $request): Response
    {
        // Logic here
    }
}




use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use App\Api\Sms;

$apis = [
    Sms\SendApi::class,
    // ... other APIs
];

return function (RoutingConfigurator $routes) use ($apis) {
    foreach ($apis as $api) {
        $i = 0;
        foreach ($api::getRoutesPaths() as $routePath) {
            $pathName = sprintf('%s_%s', str_replace('\\', '_', $api), $i);
            $routes
                ->add($pathName, $routePath)
                ->controller([$api, 'sendResponse'])
                ->methods($api::getMethods());

            $i++;
        }
    }
};