swaggest / php-code-builder
Swaggest JSON-schema enabled PHP code builder
Installs: 353 619
Dependents: 5
Suggesters: 0
Security: 0
Stars: 73
Watchers: 2
Forks: 19
Open Issues: 3
Requires
- php: >=5.6.0
- ext-json: *
- swaggest/code-builder: ^0.3.8
- swaggest/json-schema: ^0.12.33
Requires (Dev)
- phperf/phpunit: 4.8.38
- dev-master
- v0.2.42
- v0.2.41
- v0.2.40
- v0.2.39
- v0.2.38
- v0.2.37
- v0.2.36
- v0.2.35
- v0.2.34
- v0.2.33
- v0.2.32
- v0.2.31
- v0.2.30
- v0.2.29
- v0.2.28
- v0.2.27
- v0.2.26
- v0.2.25
- v0.2.24
- v0.2.23
- v0.2.22
- v0.2.21
- v0.2.20
- v0.2.19
- v0.2.18
- v0.2.17
- v0.2.16
- v0.2.15
- v0.2.14
- v0.2.13
- v0.2.12
- v0.2.11
- v0.2.10
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-wip-nullable-properties
This package is auto-updated.
Last update: 2024-11-19 19:07:05 UTC
README
This library generates PHP mapping structures defined by JSON schema
using swaggest/json-schema
.
Example
You need to add swaggest/json-schema
to your dependencies.
<?php require_once __DIR__ . '/vendor/autoload.php'; $schemaData = json_decode(<<<'JSON' { "type": "object", "properties": { "id": {"type": "integer"}, "name": {"type": "string"}, "parent": {"$ref": "#"}, "children": {"type": "array", "items": {"$ref": "#"}}, "info": {"$ref": "#/definitions/info"} }, "definitions": { "info": { "type": "object", "properties": { "lastName": {"type": "string"}, "birthDate": {"type": "string", "format": "date-time"}, "options": {"$ref": "#/definitions/options"} } }, "options": { "type": "object", "properties": { "rememberSession": {"type": "boolean"}, "allowNotifications": {"type": "boolean"} } } } } JSON ); $swaggerSchema = \Swaggest\JsonSchema\Schema::import($schemaData); $appPath = realpath(__DIR__ . '/tests/src/Tmp') . '/Example'; $appNs = 'Swaggest\PhpCodeBuilder\Tests\Tmp\Example'; $app = new \Swaggest\PhpCodeBuilder\App\PhpApp(); $app->setNamespaceRoot($appNs, '.'); $builder = new \Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder(); $builder->buildSetters = true; $builder->makeEnumConstants = true; $builder->classCreatedHook = new \Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback( function (\Swaggest\PhpCodeBuilder\PhpClass $class, $path, $schema) use ($app, $appNs) { $desc = ''; if ($schema->title) { $desc = $schema->title; } if ($schema->description) { $desc .= "\n" . $schema->description; } if ($fromRefs = $schema->getFromRefs()) { $desc .= "\nBuilt from " . implode("\n" . ' <- ', $fromRefs); } $class->setDescription(trim($desc)); $class->setNamespace($appNs); if ('#' === $path) { $class->setName('User'); // Class name for root schema } elseif (strpos($path, '#/definitions/') === 0) { $class->setName(\Swaggest\PhpCodeBuilder\PhpCode::makePhpClassName( substr($path, strlen('#/definitions/')))); } $app->addClass($class); } ); $builder->getType($swaggerSchema); $app->clearOldFiles($appPath); $app->store($appPath);
Creating and exporting an instance
$user = new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User(); $user->name = "John"; $user->info = (new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\Info()) ->setLastName("Doe") ->setBirthDate("1980-01-01") ->setOptions( (new \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\Options()) ->setRememberSession(true) ->setAllowNotifications(false) ); // No exception on exporting valid data $jsonData = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::export($user); // {"name":"John","info":{"lastName":"Doe","birthDate":"1980-01-01","options":{"rememberSession":true,"allowNotifications":false}}} echo json_encode($jsonData); // Setting invalid value (integer instead of string) $user->name = 123; // Exception: String expected, 123 received at #->$ref[#]->properties:name $jsonData = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::export($user);
Creating class instance from raw data
// Importing raw data to entity class instance will do validation and mapping $user = \Swaggest\PhpCodeBuilder\Tests\Tmp\Example\User::import( json_decode('{"name":"John","info":{"lastName":"Doe","birthDate":"1980-01-01","options":{"rememberSession":true,"allowNotifications":false}}}') ); var_dump($user->info->options->allowNotifications); // bool(false)
CLI Tool
You can use json-cli to render JSON Schema into PHP classes from command line.