PHPackages                             pilov-pa/class-from-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pilov-pa/class-from-json

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pilov-pa/class-from-json
========================

This package is for generating class code based on json structure

v0.1.1(4y ago)14MITPHPPHP &gt;=8.1

Since Apr 11Pushed 4y ago1 watchersCompare

[ Source](https://github.com/pilov-pa/class-from-json)[ Packagist](https://packagist.org/packages/pilov-pa/class-from-json)[ RSS](/packages/pilov-pa-class-from-json/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

class-from-json [![Unit tests](https://github.com/pilov-pa/class-from-json/actions/workflows/php.yml/badge.svg)](https://github.com/pilov-pa/class-from-json/actions/workflows/php.yml)
=======================================================================================================================================================================================

[](#class-from-json-)

This package uses for generating class code based on json.

Basic usage
-----------

[](#basic-usage)

```
$generator = new \PilovPa\ClassFromJson\ClassMapGenerator();
$renderer = new \PilovPa\ClassFromJson\ClassRenderer('8.1');
$classMap = $generator->generate('Some\Root\Ns', $someJson);

foreach ($classMap as $cls) {
    $classTemplate = $renderer->renderClass($cls);
    file_put_contents(__DIR__ . '/Some/Root/Ns/' . $cls->getName() . '.php', $classTemplate);
}
```

Example
-------

[](#example)

You can generate class file by any json. For example

```
{
    "booleanField": true,
    "stringField": "some string",
    "objectField": {
        "intField": 123,
        "nullField": null
    }
}
```

will translate to a couple of files:

```
