PHP code example of aaronjan / kobe

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

    

aaronjan / kobe example snippets




namespace App\Swagger;

use Kobe\Schemas\Definition;

/**
 * Class BaseDefinition
 *
 * @package App\Swagger
 */
abstract class BaseDefinition extends Definition
{
    /**
     * @return string
     */
    public function getBaseNamespace()
    {
        return 'App\\Swagger\\Definitions\\';
    }
}




namespace App\Swagger\Definitions\Responses;

use App\Swagger\BaseDefinition;
use Kobe\Kobe;

/**
 * Class ApiResponse
 *
 * @package App\Swagger\Definitions\Responses
 */
class ApiResponse extends BaseDefinition
{
    /**
     * DemoResponse constructor.
     */
    public function __construct()
    {
        $this->setProperties([
            'code'    => Kobe::makeInteger(),
            'message' => Kobe::makeString(),
            'type'    => Kobe::makeString(),
        ]);
    }
}




namespace App\Swagger\Definitions\Responses;

use Kobe\Kobe;

/**
 * Class FailureResponse
 *
 * @package App\Swagger\Definitions\Responses
 */
class FailureResponse extends ApiResponse
{
    /**
     * FailureResponse constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setProperties([
            'code'    => Kobe::makeInteger()->setExample(400),
            'message' => Kobe::makeString()->setExample('an error message'),
            'type'    => Kobe::makeString()->setExample('error'),
        ]);
    }
}



    // Here is your controller method that returns the JSON

    public function getJSON()
    {
        // zircote/swagger-php
        $swagger = \Swagger\scan([
            app_path('Http/Controllers/'),
        ]);

        // Scan the whole directory for any PHP file and parse the class instances to Swagger definitions
        $definitions = \Kobe\Kobe::scanPSR4(
            app_path('Swagger/Definitions/'),
            'App\\Swagger\\Definitions\\'
        );

        // Merge them together!
        $swaggerDefinition = array_merge(
            ((array) $swagger->jsonSerialize()), 
            ['definitions' => $definitions]
        );

        return response()->json($swaggerDefinition, 200);
    }





namespace App\Swagger\Definitions;

use App\Swagger\BaseDefinition;
use Kobe\Kobe;

/**
 * Class User
 *
 * @package App\Swagger\Definitions
 */
class User extends BaseDefinition
{
    /**
     * User constructor.
     */
    public function __construct()
    {
        $this->setProperties([
            'id'   => Kobe::makeInteger(),
            'name' => Kobe::makeString(),
        ]);
    }
}




namespace App\Swagger\Definitions;

use App\Swagger\BaseDefinition;
use Kobe\Kobe;

/**
 * Class Article
 *
 * @package App\Swagger\Definitions
 */
class Article extends BaseDefinition
{
    /**
     * Article constructor.
     */
    public function __construct()
    {
        $this->setProperties([
            'id'             => Kobe::makeInteger(),
            // $ref
            'author_user_id' => Kobe::referenceByClass(User::class),
            'title'          => Kobe::makeString(),
            'content'        => Kobe::makeString(),
        ]);
    }
}




namespace App\Swagger\Definitions;

use App\Swagger\BaseDefinition;
use Kobe\Kobe;

/**
 * Class ComplexStuff
 *
 * @package App\Swagger\Definitions
 */
class ComplexStuff extends BaseDefinition
{
    /**
     * ComplexStuff constructor.
     */
    public function __construct()
    {
        $configDefinition = Kobe::makeTempDefinition()
            ->setProperties([
                'boot'  => Kobe::makeString()
                    ->setDescription('boot mode: immediate, delay')
                    ->setExample('delay'),
                'retry' => Kobe::makeInteger(),
            ]);

        $extendedUserDefinition = Kobe::makeTempDefinitionFrom(User::class)
            ->setName('temp.ExtendedUser')
            ->setProperties([
                'permissions' => Kobe::makeString()->setExample('CREATE,UPDATE'),
            ]);

        $this->setProperties([
            'config'       => $configDefinition->getReference(),
            'extendedUser' => $extendedUserDefinition->getReference(),
            'article'      => Kobe::referenceByClass(Article::class),
        ]);
    }
}