|
1 |
| -# Swaggest JSON-schema enabled PHP code builder |
| 1 | +# Swaggest JSON-schema enabled PHP code builder |
| 2 | + |
| 3 | +This library generates PHP mapping structures defined by [JSON schema](http://json-schema.org/) |
| 4 | +using [`swaggest/json-schema`](https://github.com/swaggest/php-json-schema). |
| 5 | + |
| 6 | +## Example |
| 7 | + |
| 8 | +[Generated code](tests/src/Tmp) |
| 9 | + |
| 10 | +```php |
| 11 | +<?php |
| 12 | + |
| 13 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 14 | + |
| 15 | +$schemaData = json_decode(<<<'JSON' |
| 16 | +{ |
| 17 | + "type": "object", |
| 18 | + "properties": { |
| 19 | + "id": {"type": "integer"}, |
| 20 | + "name": {"type": "string"}, |
| 21 | + "parent": {"$ref": "#"}, |
| 22 | + "children": {"type": "array", "items": {"$ref": "#"}}, |
| 23 | + "info": {"$ref": "#/definitions/info"} |
| 24 | + }, |
| 25 | + "definitions": { |
| 26 | + "info": { |
| 27 | + "type": "object", |
| 28 | + "properties": { |
| 29 | + "lastName": {"type": "string"}, |
| 30 | + "birthDate": {"type": "string", "format": "date-time"}, |
| 31 | + "options": {"$ref": "#/definitions/options"} |
| 32 | + } |
| 33 | + }, |
| 34 | + "options": { |
| 35 | + "type": "object", |
| 36 | + "properties": { |
| 37 | + "rememberSession": {"type": "boolean"}, |
| 38 | + "allowNotifications": {"type": "boolean"} |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +JSON |
| 44 | +); |
| 45 | + |
| 46 | +$swaggerSchema = \Swaggest\JsonSchema\Schema::import($schemaData); |
| 47 | + |
| 48 | +$appPath = realpath(__DIR__ . '/tests/src/Tmp') . '/Example'; |
| 49 | +$appNs = 'Swaggest\PhpCodeBuilder\Tests\Tmp\Example'; |
| 50 | + |
| 51 | +$app = new \Swaggest\PhpCodeBuilder\App\PhpApp(); |
| 52 | +$app->setNamespaceRoot($appNs, '.'); |
| 53 | + |
| 54 | +$builder = new \Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder(); |
| 55 | +$builder->buildSetters = true; |
| 56 | +$builder->makeEnumConstants = true; |
| 57 | + |
| 58 | +$builder->classCreatedHook = new \Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback( |
| 59 | + function (\Swaggest\PhpCodeBuilder\PhpClass $class, $path, $schema) use ($app, $appNs) { |
| 60 | + $desc = ''; |
| 61 | + if ($schema->title) { |
| 62 | + $desc = $schema->title; |
| 63 | + } |
| 64 | + if ($schema->description) { |
| 65 | + $desc .= "\n" . $schema->description; |
| 66 | + } |
| 67 | + if ($fromRefs = $schema->getFromRefs()) { |
| 68 | + $desc .= "\nBuilt from " . implode("\n" . ' <- ', $fromRefs); |
| 69 | + } |
| 70 | +
|
| 71 | + $class->setDescription(trim($desc)); |
| 72 | +
|
| 73 | + $class->setNamespace($appNs); |
| 74 | + if ('#' === $path) { |
| 75 | + $class->setName('User'); // Class name for root schema |
| 76 | + } elseif ('#/definitions/' === substr($path, 0, strlen('#/definitions/'))) { |
| 77 | + $class->setName(\Swaggest\PhpCodeBuilder\PhpCode::makePhpClassName( |
| 78 | + substr($path, strlen('#/definitions/')))); |
| 79 | + } |
| 80 | + $app->addClass($class); |
| 81 | + } |
| 82 | +); |
| 83 | +
|
| 84 | +$builder->getType($swaggerSchema); |
| 85 | +$app->clearOldFiles($appPath); |
| 86 | +$app->store($appPath); |
| 87 | +``` |
| 88 | +
|
| 89 | +Creating and exporting an instance |
| 90 | +
|
| 91 | +```php |
| 92 | +$user = new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User(); |
| 93 | +$user->name = "John"; |
| 94 | +$user->info = (new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\Info()) |
| 95 | + ->setLastName("Doe") |
| 96 | + ->setBirthDate("1980-01-01") |
| 97 | + ->setOptions( |
| 98 | + (new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\Options()) |
| 99 | + ->setRememberSession(true) |
| 100 | + ->setAllowNotifications(false) |
| 101 | + ); |
| 102 | +
|
| 103 | +// No exception on exporting valid data |
| 104 | +$jsonData = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::export($user); |
| 105 | +
|
| 106 | +// {"name":"John","info":{"lastName":"Doe","birthDate":"1980-01-01","options":{"rememberSession":true,"allowNotifications":false}}} |
| 107 | +echo json_encode($jsonData); |
| 108 | +
|
| 109 | +// Setting invalid value (integer instead of string) |
| 110 | +$user->name = 123; |
| 111 | +
|
| 112 | +// Exception: String expected, 123 received at #->$ref[#]->properties:name |
| 113 | +$jsonData = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::export($user); |
| 114 | +``` |
| 115 | +
|
| 116 | +Creating class instance from raw data |
| 117 | +
|
| 118 | +```php |
| 119 | +// Importing raw data to entity class instance will do validation and mapping |
| 120 | +$user = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::import( |
| 121 | + json_decode('{"name":"John","info":{"lastName":"Doe","birthDate":"1980-01-01","options":{"rememberSession":true,"allowNotifications":false}}}') |
| 122 | +); |
| 123 | +
|
| 124 | +var_dump($user->info->options->allowNotifications); // bool(false) |
| 125 | +``` |
0 commit comments