2017 © Pedro Peláez
 

library php-code-builder

Swaggest JSON-schema enabled PHP code builder

image

swaggest/php-code-builder

Swaggest JSON-schema enabled PHP code builder

  • Monday, July 16, 2018
  • by vearutop
  • Repository
  • 1 Watchers
  • 4 Stars
  • 1,268 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 11 Versions
  • 22 % Grown

The README.md

Swaggest JSON-schema enabled PHP code builder

Build Status Scrutinizer Code Quality Code Climate codecov, (*1)

This library generates PHP mapping structures defined by JSON schema using swaggest/json-schema., (*2)

Example

Generated code, (*3)

You need to add swaggest/json-schema to your dependencies., (*4)

<?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, (*5)

$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, (*6)

// 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., (*7)

The Versions

16/07 2018

dev-master

9999999-dev https://github.com/swaggest/php-code-builder

Swaggest JSON-schema enabled PHP code builder

  Sources   Download

MIT

The Requires

 

The Development Requires

library

24/05 2018

v0.2.1

0.2.1.0 https://github.com/swaggest/php-code-builder

Swaggest JSON-schema enabled PHP code builder

  Sources   Download

MIT

The Requires

 

The Development Requires

library

15/05 2018

v0.2.0

0.2.0.0 https://github.com/swaggest/php-code-builder

Swaggest JSON-schema enabled PHP code builder

  Sources   Download

MIT

The Requires

 

The Development Requires

library

12/04 2018

dev-stabilizing

dev-stabilizing https://github.com/swaggest/php-code-builder

PHP code generation library

  Sources   Download

MIT

The Requires

 

The Development Requires

library

26/03 2018

dev-wip-client-2

dev-wip-client-2 https://github.com/swaggest/php-code-builder

PHP code generation library

  Sources   Download

MIT

The Requires

 

The Development Requires

library

02/01 2018

dev-wip-client

dev-wip-client https://github.com/swaggest/php-code-builder

PHP code generation library

  Sources   Download

MIT

The Requires

 

The Development Requires

library

14/12 2017
20/06 2017
22/05 2017
28/04 2017
27/04 2017