From cb81652f74e029dc119e3a34d25566b0cf3ad368 Mon Sep 17 00:00:00 2001 From: Fred Date: Tue, 15 Sep 2020 07:29:17 +0800 Subject: [PATCH] corrections --- README.md | 59 ++++++++++++++++++++++++++++++----------- examples/apigoat1.hjson | 32 ++++++++++++++++++++++ src/Database.php | 7 ++--- src/ForeignKey.php | 2 +- src/Parameter.php | 7 ++++- src/Xml.php | 2 +- 6 files changed, 85 insertions(+), 24 deletions(-) create mode 100644 examples/apigoat1.hjson diff --git a/README.md b/README.md index 0afebfa..deb4169 100644 --- a/README.md +++ b/README.md @@ -6,30 +6,52 @@ I always found Propel(http://propelorm.org/) schema tedious to write. This shoul Now you can write that: ``` { - goatcheese: // database name + // database name + goatcheese: { - add_validator:true, // custom behavior, not parameter - table_stamp_behavior:true, // timestamp behavior - /* parameters for the APIgoat behavior from here */ + // 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 */ - "authy('User')":{ // table name='authy' decription='User' - set_parent_menu:"Settings", // parameters from the APIgoat behavior - id_authy:["primary"], // Primary column name=id_authy primaryKey=true autoincrement=true - validation_key:"string(32)", // default string column type=VARCHAR size=50 - "username(Username)":["string(32)", "not-required", "unique"], // Unique markup will be added for the table - is_root(Root):["enum(Yes, No)", "default:No"], // set the defaultValue=No - id_authy_group:["foreign(authy_group)", "required"], // Add a default colunm, type=integer and add the foreign-key markup + // 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: { - is_cross_ref:true, // cross reference table, will add isCrossRef=true to the table - id_authy:["foreign(authy)", "primary", "onDelete:cascade"], // change the default settings on the foreign key + // 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"], } } @@ -78,17 +100,22 @@ And it will translate to: # USE $text = file_get_contents($this->rootDir . DIRECTORY_SEPARATOR . $hjson_file); - $std = mb_ereg_replace('/\r/', "", $text); // make sure we have unix style text regardless of the input + + // 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); # TODO -* Make more keyword shortcut (String(32)), and find the best defaults +* Make more keyword shortcut (String(32)), and find the best defaults! * Propel validations -* Support for ALL propel parameters, right now only the most used are supported * Add table validations, warn on potential problems * Add custom behavior validations * Tests diff --git a/examples/apigoat1.hjson b/examples/apigoat1.hjson new file mode 100644 index 0000000..5cb604f --- /dev/null +++ b/examples/apigoat1.hjson @@ -0,0 +1,32 @@ +{ + goatcheese: // database name + { + add_validator:true, // custom behavior, not parameter + table_stamp_behavior:true, // timestamp behavior + # parameters from the APIgoat behavior + set_debug_level:3, + is_builder:true, + add_hooks:{}, + with_api:true, + # !parameters from the APIgoat behavior + + "authy('User')":{ // table name='authy' decription='User' + set_parent_menu:"Settings", // parameters from the APIgoat behavior + + id_authy:["primary"], // Primary column name=id_authy primaryKey=true autoincrement=true + validation_key:"string(32)", // default string column type=VARCHAR size=50 + "username(Username)":["string(32)", "not-required", "unique"], // Unique markup will be added for the table + is_root(Root):["enum(Yes, No)", "default:No"], // set the defaultValue=No + id_authy_group:["foreign(authy_group)", "required"], // Add a default colunm, type=integer and add the foreign-key markup + expire(Expiration): ["date()"] + }, + authy_group_x: + { + is_cross_ref:true, // cross reference table, will add isCrossRef=true to the table + + id_authy:["foreign(authy)", "primary", "onDelete:cascade"], // change the default settings on the foreign key + id_authy_group:["foreign(authy_group)", "primary"], + + } + } +} \ No newline at end of file diff --git a/src/Database.php b/src/Database.php index 5b81d69..6b75ed8 100644 --- a/src/Database.php +++ b/src/Database.php @@ -172,7 +172,7 @@ public function getXml(): string { $Xml = new Xml(); - $Xml->addElement('database', $this->getAttributes()); + $Xml->addElement('database', $this->getAttributes(), false); foreach ($this->Behaviors as $Behavior) { $Xml->addElement('behavior', $Behavior->getAttributes()); @@ -183,9 +183,6 @@ public function getXml(): string $Xml->addElementClose('database'); - - return $tables . "Table Count: " . count($this->Tables) - . \PHP_EOL - . $Xml->getXml(); + return $Xml->getXml(); } } diff --git a/src/ForeignKey.php b/src/ForeignKey.php index 8a4eab2..415d9ae 100644 --- a/src/ForeignKey.php +++ b/src/ForeignKey.php @@ -39,7 +39,7 @@ public function getXml() { $Xml = new Xml(); $Xml->addElement('foreign-key', $this->getAttributes('key'), false); - $Xml->addElement('reference', $this->getAttributes('reference'), false); + $Xml->addElement('reference', $this->getAttributes('reference'), true); $Xml->addElementClose('foreign-key'); return $Xml->getXml(); } diff --git a/src/Parameter.php b/src/Parameter.php index 6d60141..6022034 100644 --- a/src/Parameter.php +++ b/src/Parameter.php @@ -14,7 +14,12 @@ public function __construct($key, $value) private function setAttributes($key, $value) { - $this->attributes[$key] = json_encode($value); + if(is_array($value)){ + $this->attributes[$key] = json_encode($value); + }else{ + $this->attributes[$key] = $value; + } + } public function getAttributes(): array diff --git a/src/Xml.php b/src/Xml.php index a1a19fa..f1b4d92 100644 --- a/src/Xml.php +++ b/src/Xml.php @@ -37,7 +37,7 @@ public function addElement(string $name, array $attributes, $close = true): Obje public function addElementClose(string $name): void { - $this->xml .= $this->pad($name) . "<$name />" . \PHP_EOL; + $this->xml .= $this->pad($name) . "" . \PHP_EOL; } public function getXml(): string