承接 pilov-pa/class-from-json 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

pilov-pa/class-from-json

Composer 安装命令:

composer require pilov-pa/class-from-json

包简介

This package is for generating class code based on json structure

README 文档

README

This package uses for generating class code based on json.

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

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:

<?php

declare(strict_types=1);

namespace Some\Root\Ns;

class RootClass
{
    private bool $booleanField;
    private string $stringField;
    private ObjectField $objectField;

    public function getBooleanField(): bool
    {
        return $this->booleanField;
    }

    public function setBooleanField(bool $booleanField): RootClass
    {
        $this->booleanField = $booleanField;
        return $this;
    }

    public function getStringField(): string
    {
        return $this->stringField;
    }

    public function setStringField(string $stringField): RootClass
    {
        $this->stringField = $stringField;
        return $this;
    }

    public function getObjectField(): ObjectField
    {
        return $this->objectField;
    }

    public function setObjectField(ObjectField $objectField): RootClass
    {
        $this->objectField = $objectField;
        return $this;
    }
}

and

<?php

declare(strict_types=1);

namespace Some\Root\Ns;

class ObjectField
{
    private int $intField;
    private mixed $nullField;

    public function getIntField(): int
    {
        return $this->intField;
    }

    public function setIntField(int $intField): RootClass
    {
        $this->intField = $intField;
        return $this;
    }

    public function getNullField(): mixed
    {
        return $this->nullField;
    }

    public function setNullField(mixed $nullField): RootClass
    {
        $this->nullField = $nullField;
        return $this;
    }
}

Generated properties

Each property in generated classes will have private modifier, getter ang fluent setter.

Property names

Class's property names generates from a json field name. under_scored names will transform to camelCase.

Property types

Class's property types generates from a json field value type. Next json types are supported:

json value php type annotation
"string" string
123 int
0.25 float
true/false bool
null mixed
[] array
{"field1": 1} SomeChildrenClass
[1,2,3] array @var int[]
["a", "b"] array @var string[]
[{"field1": 1}, {"field1": 2}] array @var SomeChildrenClass[]

Two json fields in different places in json structure who have same names and different structures will transform to two different classes.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-12