PHP code example of lucite / apispec

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

    

lucite / apispec example snippets


$bookSchema = (new Lucite\ApiSpec\Schema('Book'))
    ->addProperty(new Property('bookId', type: 'number', primaryKey: true))
    ->addProperty(new Property('title', rules: ['minLength' => 1]))
    ->addProperty(new Property('description'));

$spec = new Lucite\ApiSpec\Specification('testspec', version: '1.2.3');
$spec->addRestMethods('/books/', $bookSchema);

$spec = new Lucite\ApiSpec\Specification('myapi', 'v1.0.0');
# Add schemas, routes, etc to spec

file_put_contents(
    __DIR__.'/spec.json',
    json_encode($spec->finalize()),
);

$bookSchema = (new Lucite\ApiSpec\Schema('Book'))
    ->addProperty(new Lucite\ApiSpec\Property('bookId', type: 'number', primaryKey: true))
    ->addProperty(new Lucite\ApiSpec\Property('title', rules: ['minLength' => 1]))
    ->addProperty(new Lucite\ApiSpec\Property('description'));

$spec = new Lucite\ApiSpec\Specification('testspec', '1.2.3');
$spec->addRestMethods('/books/', $bookSchema);


$app = something(); # Framework dependent

foreach ($spec->generateRoutes() as $method => $details) {
    [$path, $schemaName, $function] = $details;
    
    # Lumen framework, ept params:
    # - Psr\Http\Message\ServerRequestInterface $request
    # - Psr\Http\Message\ResponseInterface $response
    # - array $args
    # and return Psr\Http\Message\ResponseInterface.
    # See slim framework docs
    $app->$method($path, $schemaName.':'.$function);
}

$bookSchema = (new Lucite\ApiSpec\Schema('Book'))
    ->addProperty(new Lucite\ApiSpec\Property('bookId', 'number', primaryKey: true))
    ->addProperty(new Lucite\ApiSpec\Property('title', rules: ['minLength' => 1]))
    ->addProperty(new Lucite\ApiSpec\Property('description'));

$data = ['name' => '']);
$result = $bookSchema->getValidator->validate($data);
# Will return an array containing: ['name' => '{field} must be at least 1 characters long']

$bookSchema = (new Lucite\ApiSpec\Schema('Book'))
    ->addProperty(new Lucite\ApiSpec\Property('bookId', type: 'number', primaryKey: true))
    ->addProperty(new Lucite\ApiSpec\Property('title', rules: ['minLength' => 1]))
    ->addProperty(new Lucite\ApiSpec\Property('description'));

$spec = new Lucite\ApiSpec\Specification('testspec', '1.2.3');
$spec->addRestMethods('/books/', $bookSchema);
$validator = $spec->getSchema('Book')->getValidator();