@@ -118,7 +118,7 @@ |
||
| 118 | 118 | //create tables |
| 119 | 119 | foreach ($JsonExtractor->getTablesArray() as $TableName => $TableColumn) { |
| 120 | 120 | $this->capsule::schema()->dropIfExists($TableName); |
| 121 | - $this->capsule::schema()->create($TableName, function ($table) use ($TableColumn) { |
|
| 121 | + $this->capsule::schema()->create($TableName, function($table) use ($TableColumn) { |
|
| 122 | 122 | foreach ($TableColumn as $column_item) { |
| 123 | 123 | switch ($column_item['type']) { |
| 124 | 124 | case 'int': |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | if (is_array($value) && is_object($value[0])) {//check whether it's a array and it's firs element is a object |
| 76 | 76 | |
| 77 | - $table_data = $this->getTable($prefix . $table_name, $value); //get table sql |
|
| 77 | + $table_data = $this->getTable($prefix.$table_name, $value); //get table sql |
|
| 78 | 78 | $this->table[$table_data['name']] = $table_data['column']; |
| 79 | 79 | |
| 80 | - $this->toMysqlTables($this->getHighestColumnArray($value), $prefix . $table_name . "_", $prefix . $table_name); //get it inside tables |
|
| 80 | + $this->toMysqlTables($this->getHighestColumnArray($value), $prefix.$table_name."_", $prefix.$table_name); //get it inside tables |
|
| 81 | 81 | } elseif (is_array($value) || is_object($value)) {//if it's a array and firs element is not a object |
| 82 | 82 | |
| 83 | - $table_data = $this->getTable($prefix . $table_name, $value); |
|
| 83 | + $table_data = $this->getTable($prefix.$table_name, $value); |
|
| 84 | 84 | $this->table[$table_data['name']] = $table_data['column']; |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -99,12 +99,12 @@ discard block |
||
| 99 | 99 | } |
| 100 | 100 | foreach ($data as $table_name => $value) { |
| 101 | 101 | if ($this->snake_case_table) { |
| 102 | - $table_name = $this->snakeCase($prefix . $table_name); |
|
| 102 | + $table_name = $this->snakeCase($prefix.$table_name); |
|
| 103 | 103 | } |
| 104 | 104 | if (is_array($value) && !empty($value) && is_object($value[0])) {//if it's a array and firs element is not a object |
| 105 | - $this->toMysqlData($value, $table_name . '_'); |
|
| 105 | + $this->toMysqlData($value, $table_name.'_'); |
|
| 106 | 106 | } elseif (is_object($value) || is_array($value)) { |
| 107 | - $this->toMysqlData($value, $table_name . '_'); |
|
| 107 | + $this->toMysqlData($value, $table_name.'_'); |
|
| 108 | 108 | $this->getTableData($table_name, $value); |
| 109 | 109 | } |
| 110 | 110 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | if ($this->snake_case_column) { |
| 162 | 162 | return [ |
| 163 | 163 | 'name' => $table, |
| 164 | - 'column' => array_map(function ($item) { |
|
| 164 | + 'column' => array_map(function($item) { |
|
| 165 | 165 | return [ |
| 166 | 166 | 'name' => $this->snakeCase($item['name']), |
| 167 | 167 | 'type' => $item['type'], |
@@ -339,13 +339,13 @@ discard block |
||
| 339 | 339 | } elseif (is_numeric($item)) { |
| 340 | 340 | $value[] = $item; |
| 341 | 341 | } else { |
| 342 | - $value[] = '"' . addcslashes($item, "W") . '"'; |
|
| 342 | + $value[] = '"'.addcslashes($item, "W").'"'; |
|
| 343 | 343 | } |
| 344 | 344 | } |
| 345 | - $String[] = '(' . implode(",", $value) . ')'; |
|
| 345 | + $String[] = '('.implode(",", $value).')'; |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - return "values" . implode(", ", $String); |
|
| 348 | + return "values".implode(", ", $String); |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | { |
| 357 | 357 | $String = []; |
| 358 | 358 | foreach ($array as $column) { |
| 359 | - $String[] = "`" . JsonExtractor::snakeCase($column['name']) . "`"; |
|
| 359 | + $String[] = "`".JsonExtractor::snakeCase($column['name'])."`"; |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | return implode(",", $String); |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | } elseif (empty($Data)) { |
| 377 | 377 | return $empty_val; |
| 378 | 378 | } else { |
| 379 | - return (string)$Data; |
|
| 379 | + return (string) $Data; |
|
| 380 | 380 | } |
| 381 | 381 | } |
| 382 | 382 | } |
@@ -194,12 +194,13 @@ |
||
| 194 | 194 | foreach ($data ?? [] as $Column => $Value) { |
| 195 | 195 | if (!is_array($Value) && !is_object($Value) && !empty($Column) && !is_numeric($Column)) { |
| 196 | 196 | |
| 197 | - if (isset($ForeignKeys[JsonExtractor::snakeCase($table)]) && $ForeignKeys[JsonExtractor::snakeCase($table)]['name'] == JsonExtractor::snakeCase($Column)) |
|
| 198 | - $Columns[] = ['name' => $Column, 'type' => 'foreign_key', 'ref' => $ForeignKeys[JsonExtractor::snakeCase($table)]['ref']]; |
|
| 199 | - elseif ($Column == 'id') |
|
| 200 | - $Columns[] = ['name' => $Column, 'type' => 'primary_key']; |
|
| 201 | - else |
|
| 202 | - $Columns[] = ['name' => $Column, 'type' => gettype($this->getActualDataType($Value, ""))]; |
|
| 197 | + if (isset($ForeignKeys[JsonExtractor::snakeCase($table)]) && $ForeignKeys[JsonExtractor::snakeCase($table)]['name'] == JsonExtractor::snakeCase($Column)) { |
|
| 198 | + $Columns[] = ['name' => $Column, 'type' => 'foreign_key', 'ref' => $ForeignKeys[JsonExtractor::snakeCase($table)]['ref']]; |
|
| 199 | + } elseif ($Column == 'id') { |
|
| 200 | + $Columns[] = ['name' => $Column, 'type' => 'primary_key']; |
|
| 201 | + } else { |
|
| 202 | + $Columns[] = ['name' => $Column, 'type' => gettype($this->getActualDataType($Value, ""))]; |
|
| 203 | + } |
|
| 203 | 204 | } |
| 204 | 205 | } |
| 205 | 206 | } elseif (is_array($data)) { |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | public function validate($json) |
| 65 | 65 | { |
| 66 | 66 | if (is_array($json)) { |
| 67 | - return (object)[ |
|
| 67 | + return (object) [ |
|
| 68 | 68 | $this->main_table_name => $json |
| 69 | 69 | ]; |
| 70 | 70 | } |
@@ -82,14 +82,14 @@ discard block |
||
| 82 | 82 | { |
| 83 | 83 | |
| 84 | 84 | if (!is_array($array)) |
| 85 | - $array = json_decode($array, true);//if this is not the first time decode the text |
|
| 85 | + $array = json_decode($array, true); //if this is not the first time decode the text |
|
| 86 | 86 | |
| 87 | - $return_array = $array;//return array |
|
| 87 | + $return_array = $array; //return array |
|
| 88 | 88 | |
| 89 | 89 | foreach ($array ?? [] as $key => $array_item) { |
| 90 | 90 | |
| 91 | 91 | if (!is_numeric($key) && $parent_table)//single array table, no column |
| 92 | - $table_name = $parent_table . '_' . $key; |
|
| 92 | + $table_name = $parent_table.'_'.$key; |
|
| 93 | 93 | elseif ($parent_table)//multiple array items |
| 94 | 94 | $table_name = $parent_table; |
| 95 | 95 | else {//first time loop |
@@ -97,16 +97,16 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | if (is_array($array_item)) {//if this is a array |
| 100 | - $array_item = $this->addID($array_item, $table_name, $this->increment);//recursive the array |
|
| 100 | + $array_item = $this->addID($array_item, $table_name, $this->increment); //recursive the array |
|
| 101 | 101 | if (empty($array_item['id']) && empty($array_item[0])) {//if this is a single array with no child |
| 102 | - $array_item = ['id' => $this->increment++] + $array_item;//add id |
|
| 102 | + $array_item = ['id' => $this->increment++] + $array_item; //add id |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if(isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])){//reference table |
|
| 105 | + if (isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])) {//reference table |
|
| 106 | 106 | |
| 107 | 107 | |
| 108 | - $array_item = (object)array_map(function ($item){ |
|
| 109 | - return (object)[ |
|
| 108 | + $array_item = (object) array_map(function($item) { |
|
| 109 | + return (object) [ |
|
| 110 | 110 | 'id' => $this->increment++, |
| 111 | 111 | 'value'=>$item |
| 112 | 112 | ]; |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | |
| 139 | 139 | |
| 140 | 140 | if (!is_numeric($key) && $parent_table)//single array table, no column |
| 141 | - $table_name = $parent_table . '_' . $key; |
|
| 141 | + $table_name = $parent_table.'_'.$key; |
|
| 142 | 142 | elseif ($parent_table)//multiple array items |
| 143 | 143 | $table_name = $parent_table; |
| 144 | 144 | elseif (!is_numeric($key)) //first time loop |
@@ -81,18 +81,22 @@ discard block |
||
| 81 | 81 | private function addID($array, $parent_table = false, $last_increment = false) |
| 82 | 82 | { |
| 83 | 83 | |
| 84 | - if (!is_array($array)) |
|
| 85 | - $array = json_decode($array, true);//if this is not the first time decode the text |
|
| 84 | + if (!is_array($array)) { |
|
| 85 | + $array = json_decode($array, true); |
|
| 86 | + } |
|
| 87 | + //if this is not the first time decode the text |
|
| 86 | 88 | |
| 87 | 89 | $return_array = $array;//return array |
| 88 | 90 | |
| 89 | 91 | foreach ($array ?? [] as $key => $array_item) { |
| 90 | 92 | |
| 91 | - if (!is_numeric($key) && $parent_table)//single array table, no column |
|
| 93 | + if (!is_numeric($key) && $parent_table) { |
|
| 94 | + //single array table, no column |
|
| 92 | 95 | $table_name = $parent_table . '_' . $key; |
| 93 | - elseif ($parent_table)//multiple array items |
|
| 96 | + } elseif ($parent_table) { |
|
| 97 | + //multiple array items |
|
| 94 | 98 | $table_name = $parent_table; |
| 95 | - else {//first time loop |
|
| 99 | + } else {//first time loop |
|
| 96 | 100 | $table_name = $this->main_table_name; |
| 97 | 101 | } |
| 98 | 102 | |
@@ -129,21 +133,25 @@ discard block |
||
| 129 | 133 | */ |
| 130 | 134 | private function addForeign($array, $parent_table = null, $parent_key = false) |
| 131 | 135 | { |
| 132 | - if (!is_array($array)) |
|
| 133 | - $array = json_decode($array, true); |
|
| 136 | + if (!is_array($array)) { |
|
| 137 | + $array = json_decode($array, true); |
|
| 138 | + } |
|
| 134 | 139 | |
| 135 | 140 | $return_array = $array; |
| 136 | 141 | foreach ($array ?? [] as $key => $array_item) { |
| 137 | 142 | |
| 138 | 143 | |
| 139 | 144 | |
| 140 | - if (!is_numeric($key) && $parent_table)//single array table, no column |
|
| 145 | + if (!is_numeric($key) && $parent_table) { |
|
| 146 | + //single array table, no column |
|
| 141 | 147 | $table_name = $parent_table . '_' . $key; |
| 142 | - elseif ($parent_table)//multiple array items |
|
| 148 | + } elseif ($parent_table) { |
|
| 149 | + //multiple array items |
|
| 143 | 150 | $table_name = $parent_table; |
| 144 | - elseif (!is_numeric($key)) //first time loop |
|
| 151 | + } elseif (!is_numeric($key)) { |
|
| 152 | + //first time loop |
|
| 145 | 153 | $table_name = $key; |
| 146 | - else {//first time loop |
|
| 154 | + } else {//first time loop |
|
| 147 | 155 | $table_name = $this->main_table_name; |
| 148 | 156 | } |
| 149 | 157 | |