@@ -3,135 +3,135 @@ |
||
3 | 3 | |
4 | 4 | class RefactorCI { |
5 | 5 | |
6 | - /** |
|
7 | - * [private description] |
|
8 | - * @var [type] |
|
9 | - */ |
|
10 | - private $ci; |
|
6 | + /** |
|
7 | + * [private description] |
|
8 | + * @var [type] |
|
9 | + */ |
|
10 | + private $ci; |
|
11 | 11 | |
12 | - private $primaryKey; |
|
12 | + private $primaryKey; |
|
13 | 13 | |
14 | - function __construct($params=null) { |
|
14 | + function __construct($params=null) { |
|
15 | 15 | $this->ci =& get_instance(); |
16 | 16 | $this->ci->load->config("refactor", false, true); |
17 | 17 | $this->init($params == null ? [] : $params); |
18 | - } |
|
18 | + } |
|
19 | 19 | |
20 | - public function init(array $params):void { |
|
20 | + public function init(array $params):void { |
|
21 | 21 | $this->primaryKey = $params['primary_key'] ?? 'id'; |
22 | - } |
|
23 | - /** |
|
24 | - * [run description] |
|
25 | - * @param array|string $object [description] |
|
26 | - * @param string $ruleName [description] |
|
27 | - */ |
|
28 | - function run(array &$object, $ruleName):void { |
|
22 | + } |
|
23 | + /** |
|
24 | + * [run description] |
|
25 | + * @param array|string $object [description] |
|
26 | + * @param string $ruleName [description] |
|
27 | + */ |
|
28 | + function run(array &$object, $ruleName):void { |
|
29 | 29 | // Reolve Rules. |
30 | 30 | if (is_scalar($ruleName)) { |
31 | - $rule = $this->ci->config->item("refactor_$ruleName"); |
|
31 | + $rule = $this->ci->config->item("refactor_$ruleName"); |
|
32 | 32 | } else { |
33 | - // Rule was probablt passed in as an array (associative) and we support |
|
34 | - // that. |
|
35 | - $rule = is_array($ruleName) ? $ruleName : null; |
|
33 | + // Rule was probablt passed in as an array (associative) and we support |
|
34 | + // that. |
|
35 | + $rule = is_array($ruleName) ? $ruleName : null; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | if ($rule == null) return; // No need to go further as rule doesn't exist. |
39 | 39 | // Keep |
40 | 40 | if (isset($rule['keep'])) { |
41 | - $keys = array_keys($object); |
|
42 | - for ($x = 0; $x < count($object); $x++) { |
|
41 | + $keys = array_keys($object); |
|
42 | + for ($x = 0; $x < count($object); $x++) { |
|
43 | 43 | if (!in_array($keys[$X], $rule['keep'])) { |
44 | - unset($object[$keys[$x]]); |
|
44 | + unset($object[$keys[$x]]); |
|
45 | + } |
|
45 | 46 | } |
46 | - } |
|
47 | 47 | } |
48 | 48 | // Unset |
49 | 49 | if (isset($rule['unset'])) { |
50 | - $this->unset_values($object, $rule); |
|
50 | + $this->unset_values($object, $rule); |
|
51 | 51 | } |
52 | 52 | // Replace |
53 | 53 | if (isset($rule['replace'])) { |
54 | - $this->replace_fields($object, $rule); |
|
54 | + $this->replace_fields($object, $rule); |
|
55 | 55 | } |
56 | 56 | // Bools |
57 | 57 | if (isset($rule['bools'])) { |
58 | - foreach($rule['bools'] as $boolKey) { |
|
58 | + foreach($rule['bools'] as $boolKey) { |
|
59 | 59 | $object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true'; |
60 | - } |
|
60 | + } |
|
61 | 61 | } |
62 | 62 | // Cast |
63 | 63 | if (isset($rule['cast'])) { |
64 | - $this->cast_fields($object, $rule); |
|
64 | + $this->cast_fields($object, $rule); |
|
65 | 65 | } |
66 | 66 | // Object Array |
67 | 67 | if (isset($rule['object_array'])) { |
68 | - foreach($rule['object_array'] as $field => $data) { |
|
68 | + foreach($rule['object_array'] as $field => $data) { |
|
69 | 69 | $ids = json_decode($object[$field]); |
70 | 70 | if (is_scalar($ids)) { |
71 | - // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
72 | - $this->ci->db->where($this->primaryKey, $ids); |
|
73 | - $query = $this->ci->db->get($data['table']); |
|
74 | - if ($query->num_rows() == 0) { |
|
71 | + // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
72 | + $this->ci->db->where($this->primaryKey, $ids); |
|
73 | + $query = $this->ci->db->get($data['table']); |
|
74 | + if ($query->num_rows() == 0) { |
|
75 | 75 | $object[$field] = json_encode (json_decode ("{}")); |
76 | 76 | continue; |
77 | - } |
|
78 | - $object[$field] = $query->result_array()[0]; |
|
79 | - if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
80 | - continue; |
|
77 | + } |
|
78 | + $object[$field] = $query->result_array()[0]; |
|
79 | + if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
80 | + continue; |
|
81 | 81 | } |
82 | 82 | $object[$field] = []; |
83 | 83 | if ($ids == null) return; |
84 | 84 | foreach($ids as $id) { |
85 | - $this->ci->db->where($this->primaryKey, $id); |
|
86 | - $query = $this->ci->db->get($data['table']); |
|
87 | - if ($query->num_rows() == 0) { |
|
85 | + $this->ci->db->where($this->primaryKey, $id); |
|
86 | + $query = $this->ci->db->get($data['table']); |
|
87 | + if ($query->num_rows() == 0) { |
|
88 | 88 | continue; |
89 | - } |
|
90 | - $object[$field][] = $query->result_array()[0]; |
|
91 | - // Recursion |
|
92 | - if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
89 | + } |
|
90 | + $object[$field][] = $query->result_array()[0]; |
|
91 | + // Recursion |
|
92 | + if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
93 | + } |
|
93 | 94 | } |
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - /** |
|
98 | - * [unset_values description] |
|
99 | - * @param array $object Object to Refactor. |
|
100 | - * @param array $rule Rule data, containing keys to unset in the given |
|
101 | - * associative array. |
|
102 | - */ |
|
103 | - private function unset_values(array &$object, &$rule):void { |
|
95 | + } |
|
96 | + } |
|
97 | + /** |
|
98 | + * [unset_values description] |
|
99 | + * @param array $object Object to Refactor. |
|
100 | + * @param array $rule Rule data, containing keys to unset in the given |
|
101 | + * associative array. |
|
102 | + */ |
|
103 | + private function unset_values(array &$object, &$rule):void { |
|
104 | 104 | foreach($rule['unset'] as $key) { |
105 | - unset($object[$key]); |
|
106 | - } |
|
107 | - } |
|
108 | - /** |
|
109 | - * [replace_fields description] |
|
110 | - * @param array $object [description] |
|
111 | - * @param [type] $rule [description] |
|
112 | - */ |
|
113 | - private function replace_fields(array &$object, &$rule):void { |
|
105 | + unset($object[$key]); |
|
106 | + } |
|
107 | + } |
|
108 | + /** |
|
109 | + * [replace_fields description] |
|
110 | + * @param array $object [description] |
|
111 | + * @param [type] $rule [description] |
|
112 | + */ |
|
113 | + private function replace_fields(array &$object, &$rule):void { |
|
114 | 114 | foreach ($rule['replace'] as $oldKey => $newKey) { |
115 | - $object[$newKey] = $object[$oldKey]; |
|
116 | - unset($object[$oldKey]); |
|
117 | - } |
|
118 | - } |
|
119 | - /** |
|
120 | - * [cast_fields description] |
|
121 | - * @param array $object [description] |
|
122 | - * @param [type] $rule [description] |
|
123 | - */ |
|
124 | - private function cast_fields(array &$object, &$rule):void { |
|
115 | + $object[$newKey] = $object[$oldKey]; |
|
116 | + unset($object[$oldKey]); |
|
117 | + } |
|
118 | + } |
|
119 | + /** |
|
120 | + * [cast_fields description] |
|
121 | + * @param array $object [description] |
|
122 | + * @param [type] $rule [description] |
|
123 | + */ |
|
124 | + private function cast_fields(array &$object, &$rule):void { |
|
125 | 125 | foreach ($rule['cast'] as $key => $type) { |
126 | - switch ($type) { |
|
126 | + switch ($type) { |
|
127 | 127 | case 'int': |
128 | 128 | $object[$key] = (int) $object[$key]; |
129 | - break; |
|
129 | + break; |
|
130 | 130 | case 'string': |
131 | 131 | $object[$key] = (string) $object[$key]; |
132 | - break; |
|
133 | - } |
|
132 | + break; |
|
133 | + } |
|
134 | + } |
|
134 | 135 | } |
135 | - } |
|
136 | 136 | } |
137 | 137 | ?> |
@@ -11,8 +11,8 @@ discard block |
||
11 | 11 | |
12 | 12 | private $primaryKey; |
13 | 13 | |
14 | - function __construct($params=null) { |
|
15 | - $this->ci =& get_instance(); |
|
14 | + function __construct($params = null) { |
|
15 | + $this->ci = & get_instance(); |
|
16 | 16 | $this->ci->load->config("refactor", false, true); |
17 | 17 | $this->init($params == null ? [] : $params); |
18 | 18 | } |
@@ -55,24 +55,24 @@ discard block |
||
55 | 55 | } |
56 | 56 | // Bools |
57 | 57 | if (isset($rule['bools'])) { |
58 | - foreach($rule['bools'] as $boolKey) { |
|
58 | + foreach ($rule['bools'] as $boolKey) { |
|
59 | 59 | $object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true'; |
60 | 60 | } |
61 | 61 | } |
62 | 62 | // Cast |
63 | - if (isset($rule['cast'])) { |
|
63 | + if (isset($rule['cast'])) { |
|
64 | 64 | $this->cast_fields($object, $rule); |
65 | 65 | } |
66 | 66 | // Object Array |
67 | 67 | if (isset($rule['object_array'])) { |
68 | - foreach($rule['object_array'] as $field => $data) { |
|
68 | + foreach ($rule['object_array'] as $field => $data) { |
|
69 | 69 | $ids = json_decode($object[$field]); |
70 | 70 | if (is_scalar($ids)) { |
71 | 71 | // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
72 | 72 | $this->ci->db->where($this->primaryKey, $ids); |
73 | 73 | $query = $this->ci->db->get($data['table']); |
74 | 74 | if ($query->num_rows() == 0) { |
75 | - $object[$field] = json_encode (json_decode ("{}")); |
|
75 | + $object[$field] = json_encode(json_decode("{}")); |
|
76 | 76 | continue; |
77 | 77 | } |
78 | 78 | $object[$field] = $query->result_array()[0]; |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | } |
82 | 82 | $object[$field] = []; |
83 | 83 | if ($ids == null) return; |
84 | - foreach($ids as $id) { |
|
84 | + foreach ($ids as $id) { |
|
85 | 85 | $this->ci->db->where($this->primaryKey, $id); |
86 | 86 | $query = $this->ci->db->get($data['table']); |
87 | 87 | if ($query->num_rows() == 0) { |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | * associative array. |
102 | 102 | */ |
103 | 103 | private function unset_values(array &$object, &$rule):void { |
104 | - foreach($rule['unset'] as $key) { |
|
104 | + foreach ($rule['unset'] as $key) { |
|
105 | 105 | unset($object[$key]); |
106 | 106 | } |
107 | 107 | } |
@@ -35,7 +35,10 @@ discard block |
||
35 | 35 | $rule = is_array($ruleName) ? $ruleName : null; |
36 | 36 | } |
37 | 37 | |
38 | - if ($rule == null) return; // No need to go further as rule doesn't exist. |
|
38 | + if ($rule == null) { |
|
39 | + return; |
|
40 | + } |
|
41 | + // No need to go further as rule doesn't exist. |
|
39 | 42 | // Keep |
40 | 43 | if (isset($rule['keep'])) { |
41 | 44 | $keys = array_keys($object); |
@@ -76,11 +79,15 @@ discard block |
||
76 | 79 | continue; |
77 | 80 | } |
78 | 81 | $object[$field] = $query->result_array()[0]; |
79 | - if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
82 | + if (isset($data['refactor'])) { |
|
83 | + $this->run($object[$field], $data['refactor']); |
|
84 | + } |
|
80 | 85 | continue; |
81 | 86 | } |
82 | 87 | $object[$field] = []; |
83 | - if ($ids == null) return; |
|
88 | + if ($ids == null) { |
|
89 | + return; |
|
90 | + } |
|
84 | 91 | foreach($ids as $id) { |
85 | 92 | $this->ci->db->where($this->primaryKey, $id); |
86 | 93 | $query = $this->ci->db->get($data['table']); |
@@ -89,7 +96,9 @@ discard block |
||
89 | 96 | } |
90 | 97 | $object[$field][] = $query->result_array()[0]; |
91 | 98 | // Recursion |
92 | - if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
99 | + if (isset($data['refactor'])) { |
|
100 | + $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
101 | + } |
|
93 | 102 | } |
94 | 103 | } |
95 | 104 | } |