PHP code example of osushi / apidoc

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

    

osushi / apidoc example snippets



Osushi\Apidoc\Apidoc;
use Osushi\Apidoc\Permission;
use Osushi\Apidoc\Parameter;
use Osushi\Apidoc\Request;
use Osushi\Apidoc\Response;

Apidoc::init();
$apiDoc = Apidoc::getInstance();

$permission = new Permission();
$permission->add('users:*');
$permission->add('users:get');

$parameter = new Parameter();
$parameter->add('name', ['isa' => 'string', 't
);

$request = new Request([
    'method' => 'GET',
    'path' => '/users',
    'parameters' => ['status' => 10, 'name' => 'tarou'],
    'headers' => ['Content-Type' => 'application/json'],
]);

$response = new Response([
    'code' => 200,
    'headers' => ['Content-Type' => 'application/json; charset=utf-8'],
    'body' => '{"id": 1,"name": "tarou","status": 10,"created_at": "2015-04-21T14:55:09.351Z","updated_at": "2015-04-21T14:55:09.351Z"}',
]);

$documents->example(
    'users:/users:GET',  # This key format is {filename}:{path}:{method}
    $request,
    $response,
    '200 Success' # It's able to add comment
);

$apiDoc->render();


Osushi\Apidoc\Apidoc;

Apidoc::init();

register_shutdown_function(function(){
   $apiDoc = Apidoc::getInstance();
   $apiDoc->render();
})



use Tests\TestCase;
use Osushi\Apidoc\Apidoc;
use Osushi\Apidoc\Permission;
use Osushi\Apidoc\Parameter;
use Osushi\Apidoc\Request;
use Osushi\Apidoc\Response;

class UserIndexTest extends TestCase
{
    public static $apiDoc;

    public static function setUpBeforeClass()
    {
		# Set Permission Details
        $permission = new Permission();
        $permission->add('users:*');
        $permission->add('users:get');

        # Set Parameter Details
        $parameter = new Parameter();
        $parameter->add('name', ['isa' => 'string', 'u',
       ];
       $response = $this->call('GET', '/users', $params);
       $response->assertStatus(200);

       $request = new Request([
           'method' => 'GET',
           'path' => '/users',
           'parameters' => $params,
           'headers' => ['Content-Type' => 'application/json'],
       ]);

       $response = new Response([
           'code' => $response->getStatusCode(),
           'headers' => $response->getHeaders(),
           'body' => (string) $response->getBody(),
       ]);

       self::$apiDoc->example(
           'users:/users:GET',  # This key format is {filename}:{path}:{method}
           $request,
           $response,
           '200 Success' # It's able to add comment
       );
   }
}

Apidoc::init($config);

use Osushi\Apidoc\Config;

Apidoc::init([Parameter.php
  Config::OUTPUT_PATH => 'yourdir',
  Config::TOC => false,
]);

use Osushi\Apidoc\Parameter;

$parameter = new Parameter();
$parameter->add('name', ['isa' => 'string', ''10', '20', '30']]);
$parameter->note('here is note');

use Osushi\Apidoc\Request;

$request = new Request([
    'method' => {string method},
    'path' => {string path},
    'parameters' => {array params},
    'headers' => {array headers},
]);
# or
$request = new Request();
$request = setMethod({string method});
$request = setPath({string method});
$request = setParameters({array params});
$request = setHeaders({array headers});

use Osushi\Apidoc\Response;

$response = new Response([
    'code' => {int status_code},
    'headers' => {array headers},
    'body' => {string json_body},
]);
# or
$response = new Response();
$response = setCode({int status_code});
$response = setHeaders({array headers});
$response = setBody({string json_body});
bash
$ php apidoc.php APIDOC
$ tree docs
docs
├── toc.md
└── users.md