PHP code example of apie / schema-generator

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

    

apie / schema-generator example snippets



use Apie\Fixtures\Enums\Gender;
use Apie\CommonValueObjects\Ranges\DateTimeRange;
use Apie\SchemaGenerator\ComponentsBuilderFactory;

$factory = ComponentsBuilderFactory::createComponentsBuilderFactory();
// $schema = ['type' => 'enum', 'enum' => ['M', 'V']]
$schema = $factory->addCreationSchemaFor(Gender::class);
/**
 * $schema = [
 *     'type' => 'object',
 *     'properties' => [
 *          'start' => ['$ref' => '#/components/schemas/DateWithTimezone-post'],
 *          'end' => ['$ref' => '#/components/schemas/DateWithTimezone-post'],
 *     ]
 * ]
 */
$schema = $factory->addCreationSchemaFor(DateTimeRange::class);
// $components = ['mixed', 'Gender-post', 'DateTimeRange-post', 'DateWithTimezone-post']
$components = array_keys($factory->getComponents()->schemas);


use Apie\Core\Attributes\Optional;
use Apie\Core\Dto\DtoInterface;

class ExampleDto implements DtoInterface {
    string $example;

    int $number = 42;

    #[Optional()]
    Gender $gender;
}


use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
use Apie\Core\ValueObjects\IsStringValueObject;

#[SchemaMethod('getSchema')]
class Example implements StringValueObject {
    use IsStringValueObject;

    public static function getSchema(): array
    {
        return [
            'type' => 'string',
            'format' => 'password',
            'max' => 12,
        ];
    }
}