PHP code example of spiral / json-schema-generator
1. Go to this page and download the library: Download spiral/json-schema-generator 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/ */
spiral / json-schema-generator example snippets
namespace App\DTO;
use Spiral\JsonSchemaGenerator\Attribute\Field;
class Movie
{
public function __construct(
#[Field(title: 'Title', description: 'The title of the movie')]
public readonly string $title,
#[Field(title: 'Year', description: 'The year of the movie')]
public readonly int $year,
#[Field(title: 'Description', description: 'The description of the movie')]
public readonly ?string $description = null,
public readonly ?string $director = null,
#[Field(title: 'Release Status', description: 'The release status of the movie')]
public readonly ?ReleaseStatus $releaseStatus = null,
) {
}
}
namespace App\DTO;
enum ReleaseStatus: string
{
case Released = 'Released';
case Rumored = 'Rumored';
case PostProduction = 'Post Production';
case InProduction = 'In Production';
case Planned = 'Planned';
case Canceled = 'Canceled';
}
use Spiral\JsonSchemaGenerator\Generator;
use App\DTO\Movie;
$generator = new Generator();
$schema = $generator->generate(Movie::class);
[
'properties' => [
'title' => [
'title' => 'Title',
'description' => 'The title of the movie',
'type' => 'string',
],
'year' => [
'title' => 'Year',
'description' => 'The year of the movie',
'type' => 'integer',
],
'description' => [
'title' => 'Description',
'description' => 'The description of the movie',
'type' => 'string',
],
'director' => [
'type' => 'string',
],
'releaseStatus' => [
'title' => 'Release Status',
'description' => 'The release status of the movie',
'allOf' => [
[
'$ref' => '#/definitions/ReleaseStatus',
],
],
],
],
'
namespace App\DTO;
use Spiral\JsonSchemaGenerator\Attribute\Field;
final class Actor
{
public function __construct(
public readonly string $name,
/**
* @var array<Movie>
*/
public readonly array $movies = [],
) {
}
}
use Spiral\JsonSchemaGenerator\Generator;
use App\DTO\Actor;
$generator = new Generator();
$schema = $generator->generate(Actor::class);
[
'properties' => [
'name' => [
'type' => 'string',
],
'movies' => [
'type' => 'array',
'items' => [
'$ref' => '#/definitions/Movie',
],
'default' => [],
],
],
''description' => 'The title of the movie',
'type' => 'string',
],
'year' => [
'title' => 'Year',
'description' => 'The year of the movie',
'type' => 'integer',
],
'description' => [
'title' => 'Description',
'description' => 'The description of the movie',
'type' => 'string',
],
'director' => [
'type' => 'string',
],
'releaseStatus' => [
'title' => 'Release Status',
'description' => 'The release status of the movie',
'allOf' => [
[
'$ref' => '#/definitions/ReleaseStatus',
],
],
],
],
'
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.