PHP code example of nicoandra / laravel-data-openapi-generator

1. Go to this page and download the library: Download nicoandra/laravel-data-openapi-generator 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/ */

    

nicoandra / laravel-data-openapi-generator example snippets




namespace App\Data;

use NicoAndra\OpenApiGenerator\Attributes\Description;
use NicoAndra\OpenApiGenerator\Attributes\Example;
use NicoAndra\OpenApiGenerator\Attributes\IgnoreFromOpenApi;
use Spatie\LaravelData\Attributes\FromRouteParameter;
use Spatie\LaravelData\Data;

class CreatePostData extends Data
{
    public function __construct(
        #[FromRouteParameter('author')]
        public int $authorId,
        #[Example('How to keep docs in sync with code')]
        public string $title,
        #[IgnoreFromOpenApi]
        public int $tenantId,
        public string $body,
    ) {}
}

#[Description('Created post')]
class PostData extends Data
{
    public function __construct(
        public int $id,
        public string $title,
    ) {}
}



namespace App\Http\Controllers;

use App\Data\CreatePostData;
use App\Data\PostData;
use NicoAndra\OpenApiGenerator\Attributes\Description;
use NicoAndra\OpenApiGenerator\Attributes\Summary;

class PostController
{
    #[Summary('Create a post')]
    #[Description('Creates a post for an author and returns the stored resource')]
    public function store(CreatePostData $data): PostData
    {
        // ...
    }
}

Route::post('/api/authors/{author}/posts', [PostController::class, 'store']);

#[HttpResponseStatus(201)]
class CreatedPostData extends Data
{
    // ...
}

#[CustomContentType(type: ['application/json', 'application/xml'])]
class ExportData extends Data
{
    // ...
}

use NicoAndra\OpenApiGenerator\Attributes\ExposedAs;
use Spatie\LaravelData\Data;

#[ExposedAs('string')]
class TransformedValueData extends Data
{
    public function __construct(
        public string $value,
    ) {}
}

class ResponseData extends Data
{
    public function __construct(
        #[ExposedAs('string')]
        public TransformedValueData $value,
    ) {}
}

'namespace_aliases' => [
    'App\\Domain\\Internal\\Api' => 'Public\\Api',
],

'overlay_files' => [
    resource_path('api/overlay.json'),
],
bash
php artisan vendor:publish --tag=openapi-generator-config
bash
php artisan openapi:generate
bash
php artisan openapi:generate