|
1
|
|
|
<?php |
|
2
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed'); |
|
3
|
|
|
|
|
4
|
|
|
class RefactorCI |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* [private description] |
|
9
|
|
|
* @var [type] |
|
|
|
|
|
|
10
|
|
|
*/ |
|
11
|
|
|
private $ci; |
|
12
|
|
|
/** |
|
13
|
|
|
* [private description] |
|
14
|
|
|
* @var [type] |
|
|
|
|
|
|
15
|
|
|
*/ |
|
16
|
|
|
private $primaryKey; |
|
17
|
|
|
|
|
18
|
|
|
function __construct($params=null) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->ci =& get_instance(); |
|
|
|
|
|
|
21
|
|
|
$this->ci->load->config("refactor", false, true); |
|
22
|
|
|
$this->ci->load->splint('francis94c/jsonp', '+JSONP', null, 'jsonp'); |
|
23
|
|
|
require_once('RefactorPayload.php'); |
|
24
|
|
|
$this->init($params == null ? [] : $params); |
|
25
|
|
|
} |
|
26
|
|
|
/** |
|
27
|
|
|
* [init description] |
|
28
|
|
|
* @date 2019-11-02 |
|
29
|
|
|
* @param array $params [description] |
|
30
|
|
|
*/ |
|
31
|
|
|
public function init(array $params):void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->primaryKey = $params['primary_key'] ?? 'id'; |
|
34
|
|
|
} |
|
35
|
|
|
/** |
|
36
|
|
|
* [load description] |
|
37
|
|
|
* @date 2019-11-02 |
|
38
|
|
|
* @return RefactorCI [description] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function load($class):RefactorCI |
|
41
|
|
|
{ |
|
42
|
|
|
include APPPATH.'libraries/refactor/'.$class.'.php'; |
|
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
/** |
|
46
|
|
|
* [payload description] |
|
47
|
|
|
* @date 2019-11-02 |
|
48
|
|
|
* @param string $class [description] |
|
49
|
|
|
* @param [type] $object [description] |
|
|
|
|
|
|
50
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
public function payload(string $class, $object) |
|
53
|
|
|
{ |
|
54
|
|
|
return (new $class($object))->toArray(); |
|
55
|
|
|
} |
|
56
|
|
|
/** |
|
57
|
|
|
* [array description] |
|
58
|
|
|
* @date 2019-11-02 |
|
59
|
|
|
* @param string $class [description] |
|
60
|
|
|
* @param array $array [description] |
|
61
|
|
|
* @return array [description] |
|
62
|
|
|
*/ |
|
63
|
|
|
public function array(string $class, array $array):array |
|
64
|
|
|
{ |
|
65
|
|
|
$refactor = new $class(); |
|
66
|
|
|
$buff; |
|
|
|
|
|
|
67
|
|
|
for ($x = 0; $x < count($array); $x++) { |
|
|
|
|
|
|
68
|
|
|
$refactor->switchPayload($array[$x]); |
|
69
|
|
|
$buff = $refactor->toArray(); |
|
70
|
|
|
if ($buff != null) $array[$x] = $buff; |
|
71
|
|
|
} |
|
72
|
|
|
return $array; |
|
73
|
|
|
} |
|
74
|
|
|
/** |
|
75
|
|
|
* [run description] |
|
76
|
|
|
* @param array|string $object [description] |
|
77
|
|
|
* @param string $ruleName [description] |
|
78
|
|
|
*/ |
|
79
|
|
|
function run(array &$object, $ruleName):void |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
if ($object == null) return; |
|
82
|
|
|
// Reolve Rules. |
|
83
|
|
|
if (is_scalar($ruleName)) { |
|
|
|
|
|
|
84
|
|
|
$rule = $this->ci->config->item("refactor_$ruleName"); |
|
85
|
|
|
} else { |
|
86
|
|
|
// Rule was probablt passed in as an array (associative) and we support |
|
87
|
|
|
// that. |
|
88
|
|
|
$rule = is_array($ruleName) ? $ruleName : null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($rule == null) return; // No need to go further as rule doesn't exist. |
|
92
|
|
|
// Keep |
|
93
|
|
|
if (isset($rule['keep'])) { |
|
94
|
|
|
$keys = array_keys($object); |
|
95
|
|
|
for ($x = 0; $x < count($object); $x++) { |
|
|
|
|
|
|
96
|
|
|
if (!in_array($keys[$X], $rule['keep'])) { |
|
|
|
|
|
|
97
|
|
|
unset($object[$keys[$x]]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
// Unset |
|
102
|
|
|
if (isset($rule['unset'])) { |
|
103
|
|
|
$this->unset_values($object, $rule); |
|
104
|
|
|
} |
|
105
|
|
|
// Replace |
|
106
|
|
|
if (isset($rule['replace'])) { |
|
107
|
|
|
$this->replace_fields($object, $rule); |
|
108
|
|
|
} |
|
109
|
|
|
// Bools |
|
110
|
|
|
if (isset($rule['bools'])) { |
|
111
|
|
|
foreach($rule['bools'] as $boolKey) { |
|
112
|
|
|
$object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true'; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
// Cast |
|
116
|
|
|
if (isset($rule['cast'])) { |
|
117
|
|
|
$this->cast_fields($object, $rule); |
|
118
|
|
|
} |
|
119
|
|
|
// Inflate |
|
120
|
|
|
if (isset($rule['inflate'])) { |
|
121
|
|
|
foreach($rule['inflate'] as $field => $data) { |
|
122
|
|
|
$ids = json_decode($object[$field], true); |
|
123
|
|
|
if (is_scalar($ids)) { |
|
124
|
|
|
// JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
125
|
|
|
$this->ci->db->where($this->primaryKey, $ids); |
|
126
|
|
|
$query = $this->ci->db->get($data['table']); |
|
127
|
|
|
if ($query->num_rows() == 0) { |
|
128
|
|
|
$object[$field] = json_encode (json_decode ("{}")); |
|
129
|
|
|
continue; |
|
130
|
|
|
} |
|
131
|
|
|
$object[$field] = $query->result_array()[0]; |
|
132
|
|
|
if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
if (isset($data['path'])) { |
|
136
|
|
|
if ($ids == null) return; |
|
137
|
|
|
$object[$field] = $ids; |
|
138
|
|
|
$this->ci->jsonp->parse($object[$field]); |
|
139
|
|
|
if (is_array($object[$field])) { |
|
140
|
|
|
$refs = $this->ci->jsonp->get_reference($data['path']); |
|
141
|
|
|
for ($x = 0; $x < count($refs); $x++) { |
|
|
|
|
|
|
142
|
|
|
$refs[$x] = $this->inflate_value($data['table'], $refs[$x]); |
|
143
|
|
|
// Recursion |
|
144
|
|
|
if (isset($data['refactor'])) $this->run($refs[$x], $data['refactor']); |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
$this->ci->jsonp->set($data['path'], $this->inflate_value($data['table'], $ids)); |
|
148
|
|
|
// Recursion |
|
149
|
|
|
if (isset($data['refactor'])) $this->run($this->ci->jsonp->get_reference($data['path']), $data['refactor']); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
$object[$field] = []; |
|
154
|
|
|
if ($ids == null) return; |
|
155
|
|
|
foreach($ids as $id) { |
|
156
|
|
|
$this->ci->db->where($this->primaryKey, $id); |
|
157
|
|
|
$query = $this->ci->db->get($data['table']); |
|
158
|
|
|
if ($query->num_rows() == 0) { |
|
159
|
|
|
continue; |
|
160
|
|
|
} |
|
161
|
|
|
$object[$field][] = $query->result_array()[0]; |
|
162
|
|
|
// Recursion |
|
163
|
|
|
if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
private function inflate_value(string $table, $value) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->ci->db->where($this->primaryKey, $value); |
|
171
|
|
|
$query = $this->ci->db->get($table); |
|
172
|
|
|
return $query->num_rows() > 0 ? $query->result_array()[0] : null; |
|
173
|
|
|
} |
|
174
|
|
|
/** |
|
175
|
|
|
* [unset_values description] |
|
176
|
|
|
* @param array $object Object to Refactor. |
|
177
|
|
|
* @param array $rule Rule data, containing keys to unset in the given |
|
178
|
|
|
* associative array. |
|
179
|
|
|
*/ |
|
180
|
|
|
private function unset_values(array &$object, &$rule):void { |
|
181
|
|
|
foreach($rule['unset'] as $key) { |
|
182
|
|
|
unset($object[$key]); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
/** |
|
186
|
|
|
* [replace_fields description] |
|
187
|
|
|
* @param array $object [description] |
|
188
|
|
|
* @param [type] $rule [description] |
|
|
|
|
|
|
189
|
|
|
*/ |
|
190
|
|
|
private function replace_fields(array &$object, &$rule):void |
|
191
|
|
|
{ |
|
192
|
|
|
foreach ($rule['replace'] as $oldKey => $newKey) { |
|
193
|
|
|
$object[$newKey] = $object[$oldKey]; |
|
194
|
|
|
unset($object[$oldKey]); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
/** |
|
198
|
|
|
* [cast_fields description] |
|
199
|
|
|
* @param array $object [description] |
|
200
|
|
|
* @param [type] $rule [description] |
|
|
|
|
|
|
201
|
|
|
*/ |
|
202
|
|
|
private function cast_fields(array &$object, &$rule):void |
|
203
|
|
|
{ |
|
204
|
|
|
foreach ($rule['cast'] as $key => $type) { |
|
205
|
|
|
switch ($type) { |
|
206
|
|
|
case 'int': |
|
207
|
|
|
$object[$key] = (int) $object[$key]; |
|
208
|
|
|
break; |
|
209
|
|
|
case 'string': |
|
210
|
|
|
$object[$key] = (string) $object[$key]; |
|
211
|
|
|
break; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
?> |
|
|
|
|
|
|
217
|
|
|
|