1. Go to this page and download the library: Download wwwision/types-jsonschema 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/ */
wwwision / types-jsonschema example snippets
class Contact {
public function __construct(public string $name, public int $age) {}
}
$schema = (new JsonSchemaGenerator())->fromClass(Contact::class);
$expected = <<<JSON
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"additionalProperties": false,
"
#[StringBased]
final class GivenName {
private function __construct(public readonly string $value) {}
}
#[StringBased]
final class FamilyName {
private function __construct(public readonly string $value) {}
}
final class FullName {
public function __construct(
public readonly GivenName $givenName,
public readonly FamilyName $familyName,
) {}
}
#[Description('honorific title of a person')]
enum HonorificTitle
{
#[Description('for men, regardless of marital status, who do not have another professional or academic title')]
case MR;
#[Description('for married women who do not have another professional or academic title')]
case MRS;
#[Description('for girls, unmarried women and married women who continue to use their maiden name')]
case MISS;
#[Description('for women, regardless of marital status or when marital status is unknown')]
case MS;
#[Description('for any other title that does not match the above')]
case OTHER;
}
#[Description('A contact in the system')]
final class Contact {
public function __construct(
public readonly HonorificTitle $title,
public readonly FullName $name,
#[Description('Whether the contact is registered or not')]
public bool $isRegistered = false,
) {}
}
$schema = (new JsonSchemaGenerator())->fromClass(Contact::class);
$expected = <<<JSON
{
"type": "object",
"description": "A contact in the system",
"properties": {
"title": {
"type": "string",
"description": "honorific title of a person",
"enum": [
"MR",
"MRS",
"MISS",
"MS",
"OTHER"
]
},
"name": {
"type": "object",
"properties": {
"givenName": {
"type": "string"
},
"familyName": {
"type": "string"
}
},
"additionalProperties": false,
"
// ...
class LoggingMiddleware implements SchemaGeneratorMiddleware {
public array $log = [];
public function __invoke(Types\Schema $schema, Closure $next): JsonSchema
{
$this->log[] = $schema->getName();
return $next($schema);
}
}
$middleware = new LoggingMiddleware();
$options = JsonSchemaGeneratorOptions::create()->withMiddleware($middleware);
$schema = (new JsonSchemaGenerator($options))->fromClass(Contact::class);
assert($middleware->log === ['Contact', 'HonorificTitle', 'FullName', 'GivenName', 'FamilyName', 'boolean']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.