uni-method/json-to-php-class

本包的最新版本(0.5.0)没有可用的许可信息。

0.5.0 2023-10-17 00:07 UTC

This package is auto-updated.

Last update: 2024-09-17 01:56:57 UTC


README

轻松将json转换为php类,解析json数组到类数组。是创建基于json格式的API桥梁的好工具。

转到章节

通过composer安装

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

例如

当前json

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

将被转换为三个php文件

<?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;
    }
}

驼峰命名法 vs 蛇形命名法

库偏好使用 camelCase 而不是 snake_case,并且自动将蛇形命名法的额外注释替换到原始的蛇形命名法名称中。

use Symfony\Component\Serializer\Annotation\SerializedName;

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

如何使用

创建 script.php 并复制代码

<?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)));
}

运行本地路径

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

指定目标路径

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

指定命名空间

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

享受新类

开发

运行测试

vendor/bin/phpunit

运行静态分析器

vendor/bin/phpstan analyse src tests

杂项

为开发者构建镜像

docker build -t php-debug .