uni-method/json-to-php-class

There is no license information available for the latest version (0.5.0) of this package.

0.5.0 2023-10-17 00:07 UTC

This package is auto-updated.

Last update: 2024-12-17 02:28:28 UTC


README

Easy converts json to php classes, parse json array to array of classes. Good tool to create bridge with excited API based on json format.

Go to section

Install via composer

composer require --dev uni-method/json-to-php-class

For example

Current json

{
  "count": 150,
  "same": [
    {
      "length": 22.34,
      "tag": {
        "name": "zip"
      }
    },
    {
      "length": 160.84
    }
  ]
}

will be converted into three php files

<?php

namespace App\Model;

class Root
{
    protected int $count;
    /**
     * @var Same[]
     */
    protected array $same;
    public function getCount() : int
    {
        return $this->count;
    }
    public function setCount(int $count) : void
    {
        $this->count = $count;
    }
    /**
     * @return Same[]
     */
    public function getSame() : array
    {
        return $this->same;
    }
    /**
     * @param Same[] $same
     */
    public function setSame(array $same) : void
    {
        $this->same = $same;
    }
}
<?php

namespace App\Model;

class Same
{
    protected float $length;
    protected Tag $tag;
    public function getLength() : float
    {
        return $this->length;
    }
    public function setLength(float $length) : void
    {
        $this->length = $length;
    }
    public function getTag() : Tag
    {
        return $this->tag;
    }
    public function setTag(Tag $tag) : void
    {
        $this->tag = $tag;
    }
}
<?php

namespace App\Model;

class Tag
{
    protected string $name;
    public function getName() : string
    {
        return $this->name;
    }
    public function setName(string $name) : void
    {
        $this->name = $name;
    }
}

Camel case vs snake case

Library prefers camelCase over snake_case and automatically replace snake case with additional annotation to original name in snake case form.

use Symfony\Component\Serializer\Annotation\SerializedName;

/**
 * @SerializedName("reply_to_message")
 */
protected ReplyToMessage $replyToMessage;

How to use

Create script.php and copy code

<?php declare(strict_types=1);

use PhpParser\PrettyPrinter;
use UniMethod\JsonToPhpClass\{Builder\AstBuilder, Converter\Converter};

require_once __DIR__ . '/vendor/autoload.php';

$json = file_get_contents($argv[1]);
$path = $argv[2] ?? __DIR__;
$namespace = $argv[3] ?? 'App\\Model';

$scenarios = new Scenarios;
$scenarios->attributesOnDifferentNames = [
    'Symfony\Component\Serializer\Annotation\SerializedName' => [['SerializedName', ['{{ originalName }}']]]
];
$scenarios->attributesForNullAndUndefined = [
    false => [
        false => [
            'Symfony\Component\Validator\Constraints as Assert' => [['Assert\NotNull']]
        ],
        true => [],
    ],
    true => [
        false => [],
        true => [],
    ],
];

$converter = new Converter();
$prettyPrinter = new PrettyPrinter\Standard();
$ast = new AstBuilder();
$classes = $converter->convert($json);

foreach ($classes as $class) {
    $fullPath = $path . '/' . $class->name . '.php';
    file_put_contents($fullPath, $prettyPrinter->prettyPrintFile($ast->build($class)));
}

run local path

php script.php /some/local/path/input.json

specify destination path

php script.php /some/local/path/input.json /put/generated/files/here

specify namespace

php script.php /some/local/path/input.json /put/generated/files/here "App\Dto"

enjoy new classes

Development

Run tests

vendor/bin/phpunit

Run static analyser

vendor/bin/phpstan analyse src tests

Misc

Build image for developers

docker build -t php-debug .