|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lifeeka\jsql\Extractor; |
|
4
|
|
|
|
|
5
|
|
|
use lifeeka\jsql\Helpers\Json; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class JsonExtractor. |
|
9
|
|
|
*/ |
|
10
|
|
|
class JsonExtractor |
|
11
|
|
|
{ |
|
12
|
|
|
public $json; |
|
13
|
|
|
public $need_id = true; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* JsonExtractor constructor. |
|
17
|
|
|
* |
|
18
|
|
|
* @param Json $json |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(Json $json) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->json = $json; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param bool $data |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function toMysqlTables($data = false, $prefix = '') |
|
31
|
|
|
{ |
|
32
|
|
|
$sql_tables = ''; |
|
33
|
|
|
|
|
34
|
|
|
if (!$data) { |
|
35
|
|
|
$data = $this->json->toObject(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
foreach ($data as $key => $value) { |
|
39
|
|
|
if (is_array($value) && is_object($value[0])) { |
|
40
|
|
|
$sql_tables .= $this->getTables($prefix . $key, $this->getHighestColumnArray($value)); |
|
41
|
|
|
$sql_tables .= $this->toMysqlTables($this->getHighestColumnArray($value), $prefix . $key . '_'); |
|
42
|
|
|
} elseif (is_array($value)) { |
|
43
|
|
|
$sql_tables .= $this->getTables($prefix . $key, $value); |
|
44
|
|
|
} elseif (is_object($value)) { |
|
45
|
|
|
$sql_tables .= $this->getTables($prefix . $key, $value); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $sql_tables; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $table |
|
54
|
|
|
* @param $data |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getTables($table, $data) |
|
59
|
|
|
{ |
|
60
|
|
|
$sql = ''; |
|
61
|
|
|
$column_sql = ''; |
|
62
|
|
|
|
|
63
|
|
|
$column = $this->getColumn($data); |
|
64
|
|
|
|
|
65
|
|
|
if ($this->need_id) { |
|
66
|
|
|
$column_sql .= '`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,'; |
|
67
|
|
|
$column_sql .= 'PRIMARY KEY (`id`)'; |
|
68
|
|
|
$column_sql .= $column ? ',' : ''; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
foreach ($column as $count => $column_item) { |
|
72
|
|
|
|
|
73
|
|
|
switch ($column_item['type']) { |
|
74
|
|
|
case 'integer': |
|
75
|
|
|
$column_sql .= "`{$column_item['name']}` int(20)"; |
|
76
|
|
|
break; |
|
77
|
|
|
case 'boolean': |
|
78
|
|
|
$column_sql .= "`{$column_item['name']}` BIT(1) COLLATE 'utf8_unicode_ci'"; |
|
79
|
|
|
break; |
|
80
|
|
|
case 'double': |
|
81
|
|
|
$column_sql .= "`{$column_item['name']}` float(50) COLLATE 'utf8_unicode_ci'"; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ((count($column) - $count) > 1) { |
|
85
|
|
|
$column_sql .= ','; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
$table = $this->toUnderscore($table); |
|
89
|
|
|
$sql .= "CREATE TABLE `$table` ($column_sql);\n"; |
|
90
|
|
|
|
|
91
|
|
|
return $sql; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $data |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getColumn($data) |
|
100
|
|
|
{ |
|
101
|
|
|
$Columns = []; |
|
102
|
|
|
|
|
103
|
|
|
if (is_object($data)) { |
|
104
|
|
|
foreach ($data ?? [] as $Column => $Value) { |
|
105
|
|
|
if (!is_array($Value) && !is_object($Value) && !empty($Column) && !is_numeric($Column)) { |
|
106
|
|
|
$Columns[] = ['name' => $this->toUnderscore($Column), 'type' => gettype($Value)]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} elseif (is_array($data)) { |
|
110
|
|
|
$Columns[] = ['name' => 'value', 'type' => gettype($data[0])]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $Columns; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $input |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function toUnderscore($input) |
|
122
|
|
|
{ |
|
123
|
|
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
|
124
|
|
|
$ret = $matches[0]; |
|
125
|
|
|
foreach ($ret as &$match) { |
|
126
|
|
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return implode('_', $ret); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $array |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function getHighestColumnArray($array) |
|
138
|
|
|
{ |
|
139
|
|
|
$Highest = false; |
|
140
|
|
|
$ColumnCount = 0; |
|
141
|
|
|
|
|
142
|
|
|
foreach ($array as $array_item) { |
|
143
|
|
|
if ($ColumnCount < count($array_item)) { |
|
144
|
|
|
$Highest = $array_item; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $Highest; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|