1. Go to this page and download the library: Download swaggest/json-schema 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/ */
/**
* @property int $quantity PHPDoc defined dynamic properties will be validated on every set
*/
class User extends ClassStructure
{
/* Native (public) properties will be validated only on import and export of structure data */
/** @var int */
public $id;
public $name;
/** @var Order[] */
public $orders;
/** @var UserInfo */
public $info;
/**
* @param Properties|static $properties
* @param Schema $ownerSchema
*/
public static function setUpProperties($properties, Schema $ownerSchema)
{
// You can add custom meta to your schema
$dbTable = new DbTable;
$dbTable->tableName = 'users';
$ownerSchema->addMeta($dbTable);
// Setup property schemas
$properties->id = Schema::integer();
$properties->id->addMeta(new DbId($dbTable)); // You can add meta to property.
$properties->name = Schema::string();
// You can embed structures to main level with nested schemas
$properties->info = UserInfo::schema()->nested();
// You can set default value for property
$defaultOptions = new UserOptions();
$defaultOptions->autoLogin = true;
$defaultOptions->groupName = 'guest';
// UserOptions::schema() is safe to change as it is protected with lazy cloning
$properties->options = UserOptions::schema()->setDefault(UserOptions::export($defaultOptions));
// Dynamic (phpdoc-defined) properties can be used as well
$properties->quantity = Schema::integer();
$properties->quantity->minimum = 0;
// Property can be any complex structure
$properties->orders = Schema::create();
$properties->orders->items = Order::schema();
$ownerSchema->$properties->price = Schema::number();
$ownerSchema->setFromRef('#/definitions/order');
// Define default mapping if any.
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
// Use mapped name references after the default mapping was configured.
$names = self::names($ownerSchema->properties);
$ownerSchema->
$user = new User();
$user->quantity = -1; // Exception: Value more than 0 expected, -1 received
$user = new User();
$user->quantity = 10;
User::export($user); // Exception: Required property missing: id
$user = new User();
$user->id = 1;
$user->name = 'John Doe';
$order = new Order();
$order->dateTime = (new \DateTime())->format(DATE_RFC3339);
$user->orders[] = $order;
User::export($user); // Exception: Required property missing: id at #->properties:orders->items[0]