PHP code example of uuf6429 / phpdoc-to-jsonschema
1. Go to this page and download the library: Download uuf6429/phpdoc-to-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/ */
uuf6429 / phpdoc-to-jsonschema example snippets
namespace MyApp;
// Define an example class to be featured in the json schema
class Person
{
public function __construct(
public readonly string $name,
public readonly int $height,
) {
}
}
// Load a PHPDoc block that should return an instance of the Person class
$docblock = \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Factory::createInstance()
->createFromComment('/** @return \MyApp\Person */');
// Retrieve the @return tag for that docblock.
/** @var \PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode $returnTag */
$returnTag = $docblock->getTag('@return');
// Convert that @return tag to JSON Schema
// (note that convertTag() takes typed tags, for example: @param, @var, @property[-read/-write] and of course @return)
$converter = new \uuf6429\PHPDocToJSONSchema\Converter();
$result = $converter->convertType($returnTag->type, null);
// Export the schema and print it out as json
echo json_encode(\Swaggest\JsonSchema\Schema::export($result), JSON_PRETTY_PRINT);
function convertType(\phpDocumentor\Reflection\Type $type, ?string $currentClass): \Swaggest\JsonSchema\Schema
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.