ado-dg/integration-model-generator

A package that allows you to generate PHP model classes from a JSON object and simplify API integrations using these models

dev-main 2021-06-05 17:54 UTC

This package is not auto-updated.

Last update: 2024-05-05 06:40:14 UTC


README

Generates PHP model classes from JSON objects, to be used in API requests, using wol-soft's php generator(https://github.com/wol-soft/php-json-schema-model-generator).

Table of Contents

Motivation

This library was made as part of a bachelor's dissertation and was created with the goal of improving API integration's efficiency. This was achieved by giving the developers the possibility to generate PHP models for their API JSON bodies. This library is a work in progress project.

Installation

The recommended way to install integration-model-generator is through Composer:

$ composer require ado-dg/integration-model-generator

Basic usage

The first step is preparing your JSON, every field that should be dynamic in the model needs its value between % characters:

{
  "name": "%name%"
}

Then, the generator needs to be initialized with following parameters:

  • namespace: the target namespace of the to be generated model
  • model path: the target directory of the to be generated model, will be made if doesn't exist and will be cleared if exists
  • schema path: the target directory where the JSON schema's require by wol-soft's library will be stored, will be made if doesn't exist

Finally, the generation itself can happen by calling the "generateIntegrationModels" method of the generator object. This requires the following parameters:

  • json: JSON body to be converted to model classes
  • model name: the name of the to be generated model class (both filename and classname)

Examples

Following example is made for a JSON body required by Azure's TTS API.

JSON:

{
  "documents": [
    {
      "id": 1,
      "text": "%text%"
    }
  ]
}

PHP code for generating the model class to corresponding JSON:

use ado_dg\IntegrationModelGenerator\Generator;

// Autoload files using the Composer autoloader.
require_once __DIR__ . '/../../vendor/autoload.php';

$json = file_get_contents(__DIR__ . '/../../storage/azure.json');
$generator = new Generator('App\Models\IntegrationModels', __DIR__ . '/../Models/IntegrationModels', __DIR__ . '/../schema');
$generator->generateIntegrationModels($json, 'AzureJsonBody');

Now that the model class is generated it can be used as follows:

$body = new App\Models\IntegrationModels\TestJsonBody(["text" => "This is a test sentence"]);
$json =  $body->jsonSerialize();

The json variable could now be passed along to an HTTP client as JSON body