PHP code example of goldspecdigital / oooas

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

    

goldspecdigital / oooas example snippets


use GoldSpecDigital\ObjectOrientedOAS\Objects\{
    Info, MediaType, Operation, PathItem, Response, Schema, Tag
};
use GoldSpecDigital\ObjectOrientedOAS\OpenApi;

// Create a tag for all the user endpoints.
$usersTag = Tag::create()
    ->name('Users')
    ->description('All user related endpoints');

// Create the info section.
$info = Info::create()
    ->title('API Specification')
    ->version('v1')
    ->description('For using the Example App API');
    
// Create the user schema.
$userSchema = Schema::object()
    ->properties(
        Schema::string('id')->format(Schema::FORMAT_UUID),
        Schema::string('name'),
        Schema::integer('age')->example(23),
        Schema::string('created_at')->format(Schema::FORMAT_DATE_TIME)
    );
    
// Create the user response.
$userResponse = Response::create()
    ->statusCode(200)
    ->description('OK')
    ->content(
        MediaType::json()->schema($userSchema)
    );
    
// Create the operation for the route (i.e. GET, POST, etc.).
$showUser = Operation::get()
    ->responses($userResponse)
    ->tags($usersTag)
    ->summary('Get an individual user')
    ->operationId('users.show');
    
// Define the /users path along with the supported operations.
$usersPath = PathItem::create()
    ->route('/users')
    ->operations($showUser);
    
// Create the main OpenAPI object composed off everything created above.
$openApi = OpenApi::create()
    ->openapi(OpenApi::OPENAPI_3_0_2)
    ->info($info)
    ->paths($usersPath)
    ->tags($usersTag);
    
header('Content-Type: application/json');
echo $openApi->toJson();

use GoldSpecDigital\ObjectOrientedOAS\OpenApi;
use Symfony\Component\Yaml\Yaml;

$openApi = OpenApi::create();

$json = $openApi->toJson();
$array = $openApi->toArray();
$yaml = Yaml::dump($array);

$info = Info::create()
    ->title('Example API');

$openApi = OpenApi::create()
    ->info($info);
echo $openApi->toJson(); // '{"info": {"title": "Example API"}}'

$openApi = $openApi->info(null);
echo $openApi->toJson(); // '{}'

$path = PathItem::create()
    ->route('/users');

$openApi = OpenApi::create()
    ->paths($path);
echo $openApi->toJson(); // '{"paths": {"/users": []}}'

$openApi = $openApi->paths();
echo $openApi->toJson(); // '{}'

$info = Info::create()->title('Example API');

echo $info->title; // 'Example API'

$schema = Schema::create()
    ->type(Schema::TYPE_OBJECT)
    ->properties(
        Schema::create('username')->type(Schema::TYPE_STRING),
        Schema::create('age')->type(Schema::TYPE_INTEGER)
    );
    
echo $schema->toJson();
/* 
{
  "type": "object",
  "properties": {
    "username": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  }
} 
*/

$schema = Schema::object()
    ->properties(
        Schema::string('username'),
        Schema::integer('age')
    );
    
echo $schema->toJson();
/* 
{
  "type": "object",
  "properties": {
    "username": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  }
} 
*/

$schema = AllOf::create()
    ->schemas(
        Schema::ref('#/components/schemas/ExampleSchema')
    );
    
echo $schema->toJson();
/*
{
  "allOf": [
    ["$ref": "#/components/schemas/ExampleSchema"]
  ]
}
*/

$schema = Schema::create()
    ->x('foo', 'bar')
    ->x('items', Schema::array()->items(Schema::string()));
    
echo $schema->toJson();
/*
{
  "x-foo": "bar",
  "x-items": {
    "type": "array",
    "items": {
      "type": "string"
    }
  }
}
*/

echo $schema->{'x-foo'}; // 'bar'

$schema = Schema::create()
    ->x('foo', 'bar')
    ->x('items', Schema::array()->items(Schema::string()));

$schema = $schema->x('foo');
    
echo $schema->toJson();
/*
{
  "x-items": {
    "type": "array",
    "items": {
      "type": "string"
    }
  }
}
*/

$schema = Schema::create()
    ->x('foo', 'bar')
    ->x('items', Schema::array()->items(Schema::string()));

echo json_encode($schema->x);
/*
{
  "x-foo": "bar",
  "x-items": {
    "type": "array",
    "items": {
      "type": "string"
    }
  }
}
*/

$openApi = OpenApi::create();
$openApi->validate();