1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeeka\JSQL\Helpers; |
4
|
|
|
|
5
|
|
|
use Lifeeka\JSQL\Extractor\JsonExtractor; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MysqlExtractor. |
9
|
|
|
*/ |
10
|
|
|
class Json |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private $json_text; |
14
|
|
|
private $json_array; |
|
|
|
|
15
|
|
|
private $main_table_name = "main"; |
16
|
|
|
private $foreign_keys = []; |
17
|
|
|
private $increment = 0; |
18
|
|
|
|
19
|
|
|
private $t = 0; |
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Json constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $json |
26
|
|
|
*/ |
27
|
|
|
public function __construct(String $json) |
28
|
|
|
{ |
29
|
|
|
$this->json_text = $json; |
30
|
|
|
$this->json_text = json_encode($this->addID($this->json_text)); |
31
|
|
|
$this->json_text = json_encode($this->addForeign($this->json_text)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function toArray() |
38
|
|
|
{ |
39
|
|
|
return json_decode($this->json_text, true); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function toObject() |
46
|
|
|
{ |
47
|
|
|
$json_object = $this->validate(json_decode($this->json_text)); |
48
|
|
|
return $json_object; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function getForeignKeys() |
55
|
|
|
{ |
56
|
|
|
return $this->foreign_keys; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $json |
62
|
|
|
* @return object |
63
|
|
|
*/ |
64
|
|
|
public function validate($json) |
65
|
|
|
{ |
66
|
|
|
if (is_array($json)) { |
67
|
|
|
return (object)[ |
68
|
|
|
$this->main_table_name => $json |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
return $json; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $array |
77
|
|
|
* @param bool $parent_table |
78
|
|
|
* @param bool $last_increment |
79
|
|
|
* @return array|mixed |
80
|
|
|
*/ |
81
|
|
|
private function addID($array, $parent_table = false, $last_increment = false) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
if (!is_array($array)) |
85
|
|
|
$array = json_decode($array, true);//if this is not the first time decode the text |
86
|
|
|
|
87
|
|
|
$return_array = $array;//return array |
88
|
|
|
|
89
|
|
|
foreach ($array ?? [] as $key => $array_item) { |
90
|
|
|
|
91
|
|
|
if (!is_numeric($key) && $parent_table)//single array table, no column |
92
|
|
|
$table_name = $parent_table . '_' . $key; |
|
|
|
|
93
|
|
|
elseif ($parent_table)//multiple array items |
94
|
|
|
$table_name = $parent_table; |
95
|
|
|
else {//first time loop |
96
|
|
|
$table_name = $this->main_table_name; |
97
|
|
|
} |
98
|
|
|
|
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 |
|
|
|
|
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 |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if(isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])){//reference table |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
$array_item = (object)array_map(function ($item){ |
109
|
|
|
return (object)[ |
110
|
|
|
'id' => $this->increment++, |
111
|
|
|
'value'=>$item |
112
|
|
|
]; |
113
|
|
|
},$array_item); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$return_array[$key] = $array_item; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $return_array; |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $array |
126
|
|
|
* @param bool $parent_table |
127
|
|
|
* @param bool $parent_key |
128
|
|
|
* @return array|mixed |
129
|
|
|
*/ |
130
|
|
|
private function addForeign($array, $parent_table = null, $parent_key = false) |
131
|
|
|
{ |
132
|
|
|
if (!is_array($array)) |
133
|
|
|
$array = json_decode($array, true); |
134
|
|
|
|
135
|
|
|
$return_array = $array; |
136
|
|
|
foreach ($array ?? [] as $key => $array_item) { |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
if (!is_numeric($key) && $parent_table)//single array table, no column |
141
|
|
|
$table_name = $parent_table . '_' . $key; |
|
|
|
|
142
|
|
|
elseif ($parent_table)//multiple array items |
143
|
|
|
$table_name = $parent_table; |
144
|
|
|
elseif (!is_numeric($key)) //first time loop |
145
|
|
|
$table_name = $key; |
146
|
|
|
else {//first time loop |
147
|
|
|
$table_name = $this->main_table_name; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
if (is_array($array_item)) { |
152
|
|
|
$array_item = $this->addForeign($array_item, $table_name, $array_item['id'] ?? ($parent_key ?? false)); |
153
|
|
|
$return_array[$key] = $array_item; |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
if ($parent_key && empty($array_item[0])) { |
157
|
|
|
$return_array[$key] = $array_item + ['foreign_key' => $parent_key]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (!is_numeric($key)) { |
161
|
|
|
$this->foreign_keys[JsonExtractor::snakeCase($table_name)] = [ |
162
|
|
|
'ref' => $parent_table, |
163
|
|
|
'name' => JsonExtractor::snakeCase('foreign_key') |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $return_array; |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|