PHP code example of guuzen / json-schema-codegen

1. Go to this page and download the library: Download guuzen/json-schema-codegen 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/ */

    

guuzen / json-schema-codegen example snippets




uuzen\JsonSchemaCodegen\Nette\NetteFilesGeneratorFactory;

NetteFilesGeneratorFactory::create(
    baseNamespace: 'App\Dto',
    schemaPath: '/path/to/schemas',
    outputPath: '/path/to/output',
    schemaSuffix: '.json',
    undefinedPath: 'Undefined.json',
)->run();



declare(strict_types=1);

namespace App\Dto;

/**
 * A sellable product
 */
final class Product
{
    public function __construct(
        /**
         * The product name
         *
         * @var non-empty-string
         */
        public $name,
        /**
         * @var int
         */
        public $stock,
        /**
         * @var int<0, 1000>
         */
        public $quantity,
        /**
         * @var int|null
         */
        public $rating,
        /**
         * @var float
         */
        public $price,
        /**
         * @var list<string>
         */
        public $tags,
        /**
         * @var int|string
         */
        public $externalId,
    ) {
    }
}

final class Order
{
    public function __construct(
        /** @var Customer */
        public $customer,
        /** @var Note|null */
        public $note,
        /** @var list<OrderItem> */
        public $items,
        /** @var CreditCardPayment|BankTransferPayment */
        public $payment,
    ) {
    }
}

use Guuzen\JsonSchemaCodegen\Nette\NetteFilesGeneratorFactory;

NetteFilesGeneratorFactory::create(
    baseNamespace: 'App\Dto',
    schemaPath: '/path/to/schemas',
    outputPath: '/path/to/output',
    schemaSuffix: '.yaml',
    undefinedPath: 'Undefined.yaml',
)->run();
json
{
    "type": "object",
    "properties": {
        "customer": {"$ref": "Customer.json"},
        "note":     {"oneOf": [{"$ref": "Note.json"}, {"type": "null"}]},
        "items":    {"type": "array", "items": {"$ref": "OrderItem.json"}},
        "payment":  {"oneOf": [{"$ref": "CreditCardPayment.json"}, {"$ref": "BankTransferPayment.json"}]}
    }
}