PHP code example of basillangevin / laravel-data-json-schemas

1. Go to this page and download the library: Download basillangevin/laravel-data-json-schemas 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/ */

    

basillangevin / laravel-data-json-schemas example snippets


use BasilLangevin\LaravelDataJsonSchemas\Enums\JsonSchemaDialect;

return [
    /*
    |--------------------------------------------------------------------------
    | JSON Schema Dialect
    |--------------------------------------------------------------------------
    |
    | If this value is not null, a "$schema" keyword will be set to
    | this identifier in the root of each generated JSON Schema.
    | This value won't change how JSON Schemas are generated.
    */
    'dialect' => JsonSchemaDialect::Draft201909,
];

/**
 * Transform a Spatie Data class into a JSON Schema.
 *
 * @param  class-string<Data>  $dataClass
 */
JsonSchema::toArray(string $dataClass): array;

/**
 * Transform a Spatie Data class into an array schema.
 * The "items" will be instances of the Data class.
 *
 * @param  class-string<Data>  $dataClass
 */
JsonSchema::collectToArray(string $dataClass): array;

/**
 * Transform a Spatie Data class into a JSON Schema object.
 * The object can then be modified with keyword methods.
 *
 * @param  class-string<Data>  $dataClass
 */
JsonSchema::make(string $dataClass): BasilLangevin\LaravelDataJsonSchemas\Schemas\ObjectSchema;

/**
 * Transform a Spatie Data class into an ArraySchema object.
 * The object can then be modified with keyword methods.
 *
 * @param  class-string<Data>  $dataClass
 */
JsonSchema::collect(string $dataClass): BasilLangevin\LaravelDataJsonSchemas\Schemas\ArraySchema;

use Spatie\LaravelData\Attributes\Validation\In;
use Spatie\LaravelData\Attributes\Validation\Min;
use Spatie\LaravelData\Data;

class BikeData extends Data
{
    public function __construct(
        public string $brand,
        /** The model name of the bike. */
        public string $model,
        #[Min(2010)]
        public int $year,
        #[In(['red', 'blue', 'green'])]
        public ?string $color = null,
        public bool $is_electric = false,
    ) {}
}

$schema = JsonSchema::toArray(BikeData::class);

$schema = [
    '$schema' => 'https://json-schema.org/draft/2019-09/schema',
    'title' => 'Bike',
    'type' => 'object',
    'properties' => [
        'brand' => [
            'type' => 'string',
        ],
        'model' => [
            'type' => 'string',
            'description' => 'The model name of the bike.',
        ],
        'year' => [
            'type' => 'integer',
            'minimum' => 2010,
        ],
        'color' => [
            'default' => null,
            'type' => ['string', 'null'],
            'enum' => ['red', 'blue', 'green'],
        ],
        'is_electric' => [
            'default' => false,
            'type' => 'boolean',
        ],
    ],
    '

$schema = JsonSchema::collectToArray(BikeData::class);

$schema = [
    '$schema' => 'https://json-schema.org/draft/2019-09/schema',
    'items' => [
        'title' => 'Bike',
        'type' => 'object',
        'properties' => [
            ...
        ],
        '

#[Title('The title of a data class.')]
class BikeData extends Data
{
    public function __construct(
        #[Title('The title of a property.')]
        public string $brand,
    ) {}
}

/**
 * The title of a data class.
 *
 * The description of a data class.
 */
class BikeData extends Data
{
    public function __construct(
        /**
         * The title of a property.
         *
         * The description of a property.
         */
        public string $brand,
    ) {}
}

#[Description('The description of a data class.')]
class BikeData extends Data
{
    public function __construct(
        #[Description('The description of a property.')]
        public string $brand,
    ) {}
}

/**
 * @var string $model The description of the model property.
 */
class BikeData extends Data
{
    public function __construct(
        /** @param string $brand The description of the brand property. */
        public string $brand,
        public string $model,
    ) {}
}

/**
 * The description of a data class.
 */
class BikeData extends Data
{
    /**
     * The description of a property.
     */
    public string $brand;
}

/**
 * The title of a data class.
 *
 * The description of a data class.
 */
class BikeData extends Data
{
    /**
     * The title of a property.
     *
     * The description of a property.
     */
    public string $brand;
}

class BikeData extends Data
{
    public function __construct(
        public string $brand = 'Trek',
    ) {}
}

use Spatie\LaravelData\Attributes\Validation\Min;

class BikeData extends Data
{
    public function __construct(
        #[Min(3)]
        public string $name,
        #[Min(10)]
        public int $quantity,
    ) {}
}

$schema = JsonSchema::toArray(BikeData::class);

$schema = [
    '$schema' => 'https://json-schema.org/draft/2019-09/schema',
    'title' => 'Bike',
    'type' => 'object',
    'properties' => [
        'name' => [
            'minLength' => 3,
        ],
        'quantity' => [
            'minimum' => 10,
        ],
    ],
];