PHP code example of fillincode / swagger

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

    

fillincode / swagger example snippets


'openapi' => '3.0.0',

'info' => [
    'title' => 'API documentation',
    'description' => 'API documentation',
    'version' => '1.0.0',
    'termsOfService' => 'https://example.com/terms',
    'contact' => [
        'name' => 'example',
        'url' => 'https://example.com',
        'email' => '[email protected]',
    ],
    'license' => [
        'name' => 'CC Attribution-ShareAlike 4.0 (CC BY-SA 4.0)',
        'url' => 'https://openweathermap.org/price',
    ],
],

'securitySchemes' => [
    'passport' => [
        'type' => 'http',
        'in' => 'header',
        'name' => 'Authorization',
        'scheme' => 'Bearer',
        'description' => 'To authorize, use the key ',
    ],
    'sanctum' => [
        'type' => 'http',
        'in' => 'header',
        'name' => 'Authorization',
        'scheme' => 'Bearer',
        'description' => 'To authorize, use the key ',
    ],
],

'servers' => [
    [
        'url' => env('APP_URL'),
        'description' => 'Server for testing',
    ],

    public function __invoke(): string|array
    {
        $user = User::whereEmail('[email protected]')->first();

        return $user?->createToken('user-token')->accessToken;
    }

    

'auth' => [
    'has_auth' => true,
    'middleware' => 'auth.api',
    'security_schema' => 'passport',
    'make_token' => [
        'action' => 'makeTokenFunction',
    ],
],

'storage' => [
    'driver' => 'local',
    'path' => 'data',
],

'pre_key' => 'data',

'resources_keys' => [
    'has_pre_key' => false,
    'use_wrap' => true,
],

'data' => [
    'user' => [
        'id' => 12,
        'name' => 'user_name'
    ]
]

    class UserResource extends JsonResource

    public function toArray(Request $request)
    {
        return [
            'user' => [
                'id' => $this->id,
                'name' => $this->name
            ]           
        ]       
    }

'pre_key' => 'data',

'resources_keys' => [
    'has_pre_key' => true,
    'use_wrap' => false,
],

'user' => [
    'id' => 12,
    'name' => 'user_name'
]

    class UserResource extends JsonResource

    public static $wrap = 'user';
    
    public function toArray(Request $request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name        
        ]       
    }

'pre_key' => '',

'resources_keys' => [
    'has_pre_key' => true,
    'use_wrap' => true,
],

[
    'id' => 12,
    'name' => 'user_name'
]

    class UserResource extends JsonResource
    
    public function toArray(Request $request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name        
        ]       
    }

    public function boot(): void
    {
        JsonResource::withoutWrapping();
    }

'pre_key' => '',

'resources_keys' => [
    'has_pre_key' => false,
    'use_wrap' => false,
],

'codes' => [
    200 => 'Request completed successfully',
    201 => 'Object created successfully',
    204 => 'Not content',
    401 => 'Not authentication',
    403 => 'Not authorization',
    404 => 'Not found',
    422 => 'Data validation error',
    500 => 'Server error',
],

    use Fillincode\Swagger\Attributes\Group;
    
    #[Group('group_name')]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\Summary;
     
    #[Summary('summary')]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\Description;
     
    #[Description('description')]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\Code;
     
    #[Code(201, 'Object update')]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\PathParameter;
     
    #[PathParameter('parameter_name', 'enum', 'description parameter', ['string_1', 'string_2', 12], false)]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\QueryParameter;
     
    #[QueryParameter('parameter_name', 'string', 'example_string', 'description parameter')]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\Resource;
     
    #[Resource(ProjectResource::class)]
    public function example_method()
    {
        //
    }

    

    use Fillincode\Swagger\Attributes\FormRequest;
     
    #[FormRequest(FormRequest::class)]
    public function example_method()
    {
        //
    }

    use Fillincode\Swagger\Attributes\Property;
     
    #[Property('age', 'integer', 'student age')]
    class ProjectRequest extends FormRequest
    {
        //
    }

    

    use Fillincode\Swagger\Attributes\Property;
    
    #[Property('id', 'string', 'user id')]
    #[Property('data.info_1', 'string', 'user info 1')]
    #[Property('data.info_2', 'string', 'user info 2')]
    class UserResource extends JsonResource
    {
        public function toArray(Request $request)
        {
            return [
                'id' => $this->id,
                'data' => [
                    'info_1' => $this->data['info_1'],
                    'info_2' => $this->data['info_2'],
                ]       
            ]       
        }
    }
shell
php artisan vendor:publish --provider="Fillincode\Swagger\SwaggerServiceProvider"