定制 uni-method/json-to-php-class 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

uni-method/json-to-php-class

Composer 安装命令:

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

包简介

README 文档

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 .

统计信息

  • 总下载量: 1.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-06-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固