PHP code example of cycle / schema-renderer

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

    

cycle / schema-renderer example snippets


$schema = new Schema([...]);
$converter = new \Cycle\Schema\Renderer\SchemaToArrayConverter();

$schemaArray = $converter->convert($schema);

$converter = new \Cycle\Schema\Renderer\SchemaToArrayConverter();

$schemaArray = $converter->convert($schema, [
    42,
    CustomClass::CUSTOM_PROPERTY,
    ...
]);

use Cycle\Schema\Renderer\OutputSchemaRenderer;

$output = new \Symfony\Component\Console\Output\ConsoleOutput();

$renderer = new OutputSchemaRenderer(colorize: true);

$output->write($renderer->render($schemaArray));

use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
use Cycle\Schema\Renderer\OutputSchemaRenderer;

class CustomPropertyRenderer implements Renderer {

    public function render(Formatter $formatter, array $schema, string $role): string
    {
        $data = $schema['my_custom_property'] ?? null;

        return \sprintf(
            '%s: %s',
            $formatter->title($this->title),
            $data === null ? $formatter->error('not defined') : $formatter->typecast($data)
        );
    }
}

$renderer = new OutputSchemaRenderer();

$renderer->addRenderer(
    new CustomPropertyRenderer(),
    new PropertyRenderer('my_custom_property', 'My super property')
);

$output->write($renderer->render($schemaArray))

use Cycle\Schema\Renderer\PhpSchemaRenderer;

$path = __DIR__. '/schema.php'

$renderer = new PhpSchemaRenderer();

\file_put_contents($path, $renderer->render($schemaArray));