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 | private $json_text; |
||||||
13 | private $main_table_name = "main"; |
||||||
14 | private $foreign_keys = []; |
||||||
15 | private $increment = 1; |
||||||
16 | |||||||
17 | |||||||
18 | /** |
||||||
19 | * Json constructor. |
||||||
20 | * @param String $json |
||||||
21 | * @param $mainTableName |
||||||
22 | */ |
||||||
23 | public function __construct(String $json, $mainTableName) |
||||||
24 | { |
||||||
25 | $this->json_text = $json; |
||||||
26 | $this->json_text = json_encode($this->addID($this->json_text)); |
||||||
27 | $this->json_text = json_encode($this->addForeign($this->json_text)); |
||||||
28 | $this->main_table_name = $mainTableName; |
||||||
29 | } |
||||||
30 | |||||||
31 | /** |
||||||
32 | * @return mixed |
||||||
33 | */ |
||||||
34 | public function toArray() |
||||||
35 | { |
||||||
36 | return json_decode($this->json_text, true); |
||||||
37 | } |
||||||
38 | |||||||
39 | /** |
||||||
40 | * @return mixed |
||||||
41 | */ |
||||||
42 | public function toObject() |
||||||
43 | { |
||||||
44 | $json_object = $this->validate(json_decode($this->json_text)); |
||||||
45 | return $json_object; |
||||||
46 | } |
||||||
47 | |||||||
48 | /** |
||||||
49 | * @return mixed |
||||||
50 | */ |
||||||
51 | public function getForeignKeys() |
||||||
52 | { |
||||||
53 | return $this->foreign_keys; |
||||||
54 | } |
||||||
55 | |||||||
56 | |||||||
57 | /** |
||||||
58 | * @param $json |
||||||
59 | * @return object |
||||||
60 | */ |
||||||
61 | public function validate($json) |
||||||
62 | { |
||||||
63 | if (is_array($json)) { |
||||||
64 | return (object)[ |
||||||
65 | $this->main_table_name => $json |
||||||
66 | ]; |
||||||
67 | } |
||||||
68 | return $json; |
||||||
69 | } |
||||||
70 | |||||||
71 | |||||||
72 | /** |
||||||
73 | * @param $array |
||||||
74 | * @param bool $parent_table |
||||||
75 | * @return array|mixed |
||||||
76 | */ |
||||||
77 | private function addID($array, $parent_table = false) |
||||||
78 | { |
||||||
79 | if (!is_array($array)) { |
||||||
80 | $array = json_decode($array, true); |
||||||
81 | }//if this is not the first time decode the text |
||||||
82 | |||||||
83 | $return_array = $array;//return array |
||||||
84 | |||||||
85 | foreach ($array ?? [] as $key => $array_item) { |
||||||
86 | if (!is_numeric($key) && $parent_table) {//single array table, no column |
||||||
87 | $table_name = $parent_table . '_' . $key; |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
88 | } elseif ($parent_table) {//multiple array items |
||||||
89 | $table_name = $parent_table; |
||||||
90 | } else {//first time loop |
||||||
91 | $table_name = $this->main_table_name; |
||||||
92 | } |
||||||
93 | |||||||
94 | if (is_array($array_item)) {//if this is a array |
||||||
95 | $array_item = $this->addID($array_item, $table_name, $this->increment);//recursive the array |
||||||
0 ignored issues
–
show
The call to
Lifeeka\JSQL\Helpers\Json::addID() has too many arguments starting with $this->increment .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
96 | if (empty($array_item['id']) && empty($array_item[0])) {//if this is a single array with no child |
||||||
97 | $array_item = ['id' => $this->increment++] + $array_item;//add id |
||||||
98 | } |
||||||
99 | |||||||
100 | if (isset($array_item[0]) && !is_array($array_item[0]) && !is_object($array_item[0])) {//reference table |
||||||
101 | |||||||
102 | |||||||
103 | $array_item = (object)array_map(function ($item) { |
||||||
104 | return (object)[ |
||||||
105 | 'id' => $this->increment++, |
||||||
106 | 'value'=>$item |
||||||
107 | ]; |
||||||
108 | }, $array_item); |
||||||
109 | } |
||||||
110 | |||||||
111 | $return_array[$key] = $array_item; |
||||||
112 | } |
||||||
113 | } |
||||||
114 | |||||||
115 | return $return_array; |
||||||
116 | } |
||||||
117 | |||||||
118 | /** |
||||||
119 | * @param $array |
||||||
120 | * @param bool $parent_table |
||||||
121 | * @param bool $parent_key |
||||||
122 | * @return array|mixed |
||||||
123 | */ |
||||||
124 | private function addForeign($array, $parent_table = null, $parent_key = false) |
||||||
125 | { |
||||||
126 | if (!is_array($array)) { |
||||||
127 | $array = json_decode($array, true); |
||||||
128 | } |
||||||
129 | |||||||
130 | $return_array = $array; |
||||||
131 | foreach ($array ?? [] as $key => $array_item) { |
||||||
132 | if (!is_numeric($key) && $parent_table) {//single array table, no column |
||||||
133 | $table_name = $parent_table . '_' . $key; |
||||||
0 ignored issues
–
show
Are you sure
$parent_table of type true can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
134 | } elseif ($parent_table) {//multiple array items |
||||||
135 | $table_name = $parent_table; |
||||||
136 | } elseif (!is_numeric($key)) { //first time loop |
||||||
137 | $table_name = $key; |
||||||
138 | } else {//first time loop |
||||||
139 | $table_name = $this->main_table_name; |
||||||
140 | } |
||||||
141 | |||||||
142 | |||||||
143 | if (is_array($array_item)) { |
||||||
144 | $array_item = $this->addForeign($array_item, $table_name, $array_item['id'] ?? ($parent_key ?? false)); |
||||||
145 | $return_array[$key] = $array_item; |
||||||
146 | |||||||
147 | |||||||
148 | if ($parent_key && empty($array_item[0])) { |
||||||
149 | $return_array[$key] = $array_item + ['foreign_key' => $parent_key]; |
||||||
150 | } |
||||||
151 | |||||||
152 | if (!is_numeric($key)) { |
||||||
153 | $this->foreign_keys[JsonExtractor::snakeCase($table_name)] = [ |
||||||
154 | 'ref' => $parent_table, |
||||||
155 | 'name' => JsonExtractor::snakeCase('foreign_key') |
||||||
156 | ]; |
||||||
157 | } |
||||||
158 | } |
||||||
159 | } |
||||||
160 | |||||||
161 | return $return_array; |
||||||
162 | } |
||||||
163 | } |
||||||
164 |