drez/hjson-to-propel-xml

将人类可读的JSON (HJSON) 转换为Propel XML模式。

v1.0 2020-09-14 23:29 UTC

This package is auto-updated.

Last update: 2024-09-10 23:06:45 UTC


README

这是一个库,用于将HJSON文件转换为Propel XML模式。我总是觉得写Propel(http://propelorm.org/)模式很繁琐。这应该有助于使其更加易于接近。

使用composer安装

composer require drez/hjson-to-propel-xml

实时转换器

https://hjson2xml.apigoat.com

示例

现在您可以这样写

{
    // database name
    goatcheese:
    {
        // custom behavior, not parameter
        add_validator:true,
        
        // timestamp behavior
        table_stamp_behavior:true,

        /* parameters for the APIgoat behavior from here, configurable custom behaviors shortcuts */
        set_debug_level:3,
        is_builder:true,
        add_hooks:{},
        with_api:true,
        /* to here */

        // table name='authy' decription='User'
        "authy('User')":{ 
            
            // parameters from the APIgoat behavior
            set_parent_menu:"Settings",
            
            // Primary column name=id_authy primaryKey=true autoincrement=true
            id_authy:["primary"],
            
            // default string column type=VARCHAR size=50
            validation_key:"string(32)",
            
            // Unique markup will be added for the table
            "username(Username)":["string(32)", "not-required", "unique"],
            
            // set the defaultValue=No
            is_root(Root):["enum(Yes, No)", "default:No"],

            // Add a default colunm, type=integer and add the foreign-key markup
            id_authy_group:["foreign(authy_group)", "required"],
            expire(Expiration): ["date()"]
        },
        authy_group_x:
        {
            // cross reference table, will add isCrossRef=true to the table
            is_cross_ref:true,

            // change the default settings on the foreign key
            id_authy:["foreign(authy)", "primary", "onDelete:cascade"],

            id_authy_group:["foreign(authy_group)", "primary"],
        }
    }
}

将会转换为

<database name="goatcheese" defaultIdMethod="native" namespace="App" >
        <behavior name="add_validator" />
        <behavior name="table_stamp_behavior" />
        <behavior name="GoatCheese" >
            <parameter name="set_debug_level" value="3" />
            <parameter name="is_builder" value="1" />
            <parameter name="add_hooks" value="[]" />
            <parameter name="with_api" value="1" />
        </behavior>
    <table name="authy" description="User" >
        <behavior name="GoatCheese" >
            <parameter name="set_parent_menu" value="Settings" />
        </behavior>
        <column name="id_authy" type="INTEGER" size="11" required="true" primaryKey="true" autoIncrement="true" />
        <column name="validation_key" type="VARCHAR" size="32" required="false" />
        <column name="username" description="Username" type="VARCHAR" size="32" required="false" />
        <column name="is_root" description="Root" type="ENUM" valueSet="Yes, No" required="false" defaultValue="No" />
        <column name="id_authy_group" type="INTEGER" size="11" required="true" />
        <foreign-key foreignTable="authy_group" onDelete="restrict" onUpdate="restrict" >
            <reference local="id_authy_group" foreign="id_authy_group" />
        </foreign-key>
        <column name="expire" description="Expiration" type="DATE" required="false" />
        <unique >
            <unique-column name="username" />
        </unique>
    </table>
    <table name="authy_group_x" isCrossRef="true" >
        <column name="id_authy" type="INTEGER" size="11" required="true" primaryKey="true" />
        <foreign-key foreignTable="authy" onDelete="cascade" onUpdate="restrict" >
            <reference local="id_authy" foreign="id_authy" />
        </foreign-key>
        <column name="id_authy_group" type="INTEGER" size="11" required="true" primaryKey="true" />
        <foreign-key foreignTable="authy_group" onDelete="restrict" onUpdate="restrict" >
            <reference local="id_authy_group" foreign="id_authy_group" />
        </foreign-key>
    </table>
</database>

USE

$text = file_get_contents($this->rootDir . DIRECTORY_SEPARATOR . $hjson_file);

// make sure we have unix style text regardless of the input
$std = mb_ereg_replace('/\r/', "", $text);
$hjson = $cr ? mb_ereg_replace("\n", "\r\n", $std) : $std;

// use of laktak/hjson(https://github.com/hjson/hjson-php) to convert the HJSON to array
$parser = new \HJSON\HJSONParser();
$obj = $parser->parse($hjson, ['assoc' => true]);

// convert Hjson to Propel schema
$HjsonToXml = new \HjsonToPropelXml\HjsonToPropelXml();
$HjsonToXml->convert($obj);

Schema

  • 您可以使用所有Propel列类型
  • 您可以将任何Propel列属性指定为attribut:value
  • 鼓励您为最常用的类型添加自己的快捷方式!

待办事项

  • 创建更多关键字快捷方式(String(32)),并找到最佳默认值!
  • Propel验证
  • 添加表验证,警告潜在问题
  • 添加自定义行为验证
  • 测试

许可

MIT