PHP code example of amsify42 / php-swagger-postman

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

    

amsify42 / php-swagger-postman example snippets


$attribute = new \Amsify42\PhpSwaggerPostman\Swagger\Attribute();
echo $attribute->generate();

echo $attribute->generate(
  ['name' => '

$attribute->setSuccessData(['id' => 42, 'name' => 'SomeUser']);
echo $attribute->generate();

$attribute->checkRules(
                function ($name, $rule) {
                    if ($rule == 'some_custom_rule') {
                        return [
                            'property' => // Pass some custom property attribute syntax
                        ];
                    } else if ($rule == 'enum') {
                        return  [
                            'enum' => ['paid', 'unpaid']
                        ];
                    } else if ($rule == 'boolean') {
                        return  [
                            'tinyint' => true
                        ];
                    }
                    return NULL;
                }
            );

$attribute->checkRules(
                function ($name, $rule) use($attribute) {
                    if ($rule == 'array') {
                        return [
                            'property' => $attribute->createObjectOrArrayProperty(
                                $_POST[$name], // or $_GET[$name] or value from body data
                                $name
                              )
                        ];
                    }
                    return NULL;
                }
            );

$attribute->setSecurity('XApiKey'); // The XApiKey will be from the security attribute already added

#[OA\SecurityScheme(
    securityScheme: "XApiKey",
    type: "apiKey",
    in: "header",
    name: "X-Api-Key"
)]

$attribute->setHeader(['Auth-Key' => 'some_key']);

$attribute->setResponse([
                [
                    'code' => 422,
                    'ref' => '#/components/responses/Validation' // Validation here is the name of response which is already defined somewhere
                ]
            ]);

#[OA\Response(
    response: "Validation",
    description: "Validation Errors",
    content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "message",
                example: "Please check the inputs provided"
            ),
            new OA\Property(
                property: "errors",
                type: "object"
            )
        ]
    )
)]

$annotation = new \Amsify42\PhpSwaggerPostman\Swagger\Annotation();
echo $annotation->generate();

$swagger = new \Amsify42\PhpSwaggerPostman\Swagger;
$swagger->getGeneratedJson(
    "path/to/scan-directory",
    "path/to/export-swagger-and-postman-json/"
);

use OpenApi\Attributes as OA;

#[OA\Info(title: "My API", version: "1.0.0")]

#[OA\Get(path:"/api/users")]
#[OA\Response(response:"200", description:"An example endpoint")]

/**
 * @OA\Info(
 *   version="1.0.0",
 *   title="My API"
 * )
 */

/**
 * @OA\Get(
 *     path="/api/users",
 *     @OA\Response(response="200", description="An example endpoint")
 * )
 */


$swagger = new \Amsify42\PhpSwaggerPostman\Swagger('http://www.site.com');
$swagger->getGeneratedJson(
    "path/to/scan-directory",
    "path/to/export-swagger-and-postman-json/"
);

#[OA\Server(
    url: 'http://www.site.com',
    description: 'some description about site'
)]

/**
 * @OA\Server(
 *     url="http://www.site.com",
 *     description="some description about site"
 * )
 */

$swagger = new \Amsify42\PhpSwaggerPostman\Swagger('http://www.site.com', 'Local');

composer