承接 kshabazz/json-to-src 相关项目开发

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

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

kshabazz/json-to-src

Composer 安装命令:

composer require kshabazz/json-to-src

包简介

A program to convert a JSON file to source code.

README 文档

README

Convert JSON to source code.

This tool takes a JSON file, along with other required input, and on success will parse the JSON and output (PHP by default) source files. It works recursively on the JSON, and will parse nested keys where the value is an object. In addition, an array that contain objects will produce a class, but only one since all elements are assumed to be of the same type.

Features

  • Parses JSON into an hash-array to be used in a template engine (see schema).
  • Outputs file(s) to a directory of your choice (assuming it is writable).
  • Can turn off/on generating unit tests.
  • Optional set a namespace.
  • set a callaback to manipulate the output of the source code generated.

Using the Default PHP template

  • Will convert to PHP, turning fields into properties with getters and setters (fluent style).
  • Optional scalar type hints for PHP 7 Function signatures. Example:
    /**
     * @var int
     */
    private $client;
    
    /**
     * Get ClientId.
     *
     * @param int $ClientId
     * @return $this;
     */
    public function setClientId(int $ClientId)
    {
        $this->ClientId = $ClientId; 
    }

Side-effect

Some things to make note of when using this tool include:

  • Bad characters like "$,-" Will automatically be removed from keys in the JSON file.
  • A field that is an array and has an object as its first element will result in a class being generated.
  • All objects found will produce a class file, the key that contained that object will be used as the name.
  • Will generate namespaces for nested objects to prevent class name collision.

Here's a rough idea of how it handles JSON types:

JSON PHP
null null
123 int
1.00 double (a.k.a float)
"string" string (no interpolation, ex: 'string')
"Company": {"name": "Kohirens"} class Company {}
"Clients": [{"id":1}] class Clients { private $id; getId,setId }
"Company": {} Will produce no class, since the class would have no properties.

Installation

Composer.json

composer require --dev kshabazz/json-to-src

- or -

{
  "require": {
    "kshabazz/json-to-src": "^1.0"
  }
}

Shell or Command Line

composer.phar require --dev kshabazz/json-to-src

Template Data Schema

This is the array that is passed to the tempalte engines render method:

$renderData = [
    "className" => Company
    "classProperties" => [
        [
            "name" => company
            "type" => string
            "isCustomType" => 
            "paramType" => string
            "value" => Kohirens
            "arrayType" => 
        ],
        [
            "name" => employees
            "type" => array
            "isCustomType" => 
            "paramType" => array
            "value" => ""
            "arrayType" => employees
        ],
        [
            "name" => location
            "type" => Location
            "isCustomType" => 1
            "paramType" => Location
            "value" => stdClass Object
                (
                    "type" => Point
                    "coordinates" => Array
                        (
                            [0" => -83.0466419
                            [1" => 42.3323378
                        )

                    "city" => Detroit
                    "state" => MI
                    "zip_code" => 48226
                )

            "arrayType" => 
        ],
        [
            "name" => categories
            "type" => array
            "isCustomType" => 
            "paramType" => array
            "value" => []
            "arrayType" => categories
        ]
    ],
    "classNamespace" => Tests
];

Set A Callback

You can also set a callback that will recieve the render data, to control thins such as the class name, namespace, and etc. Take the following example.

Callback script: preRenderCallback.php

return function (array $renderData, $isUnitTest) {
    if (!$isUnitTest) {
        $renderData['classNamespace'] .= '\\Bar';
    }
    
    return $renderData;
};

Pass preRenderCallback.php to the command line script:

jsontosrc -n Tests -c preRenderCallback.php company.json Company tmp

Command Line

The "jsontosrc" command line script take several arguments and options.

-n Takes a string to use as a namespace. -u A separate directory to output unit test. -a Set the property access, the default is "private." -c A callback function to modify template data before render. -r Control how deep to go for nested objects, the default is 20. -p An optional string to be used as a namespace prefix. -v Add debug messages to the output. -t Turn on PHP 7 type hints.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固