PHP code example of zeusi / json-schema-extractor

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

    

zeusi / json-schema-extractor example snippets


final class UserProfile
{
    public function __construct(
        public string $id,

        /** @var list<string> */
        public array $roles = [],

        /** @var array{theme: string, notifications: bool, locale?: string} */
        public array $preferences = [],
    ) {}
}

use Zeusi\JsonSchemaExtractor\Discoverer\ReflectionDiscoverer;
use Zeusi\JsonSchemaExtractor\Enricher\PhpStanEnricher;
use Zeusi\JsonSchemaExtractor\Mapper\StandardJsonSchemaMapper;
use Zeusi\JsonSchemaExtractor\SchemaExtractor;
use Zeusi\JsonSchemaExtractor\Serialization\JsonEncodeSerializationStrategy;

$extractor = new SchemaExtractor(
    new ReflectionDiscoverer(setTitleFromClassName: true),
    [
        new PhpStanEnricher(),
    ],
    new JsonEncodeSerializationStrategy(),
    new StandardJsonSchemaMapper(),
);

$schema = $extractor->extract(UserProfile::class);

echo json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

use Zeusi\JsonSchemaExtractor\Mapper\ClassReferenceStrategy;
use Zeusi\JsonSchemaExtractor\Mapper\JsonSchemaDialect;
use Zeusi\JsonSchemaExtractor\Mapper\StandardJsonSchemaMapper;
use Zeusi\JsonSchemaExtractor\Mapper\StandardJsonSchemaMapperOptions;

$mapper = new StandardJsonSchemaMapper(new StandardJsonSchemaMapperOptions(
    dialect: JsonSchemaDialect::Draft202012,