Skip to content

Commit

Permalink
corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
drez committed Sep 14, 2020
1 parent d15f14f commit cb81652
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 24 deletions.
59 changes: 43 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
}
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions examples/apigoat1.hjson
Original file line number Diff line number Diff line change
@@ -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"],

}
}
}
7 changes: 2 additions & 5 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
7 changes: 6 additions & 1 deletion src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . "</$name>" . \PHP_EOL;
}

public function getXml(): string
Expand Down

0 comments on commit cb81652

Please sign in to comment.