@@ -3,96 +3,96 @@ |
||
| 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 $object [description] |
|
| 26 | - * @param string $ruleName [description] |
|
| 27 | - */ |
|
| 28 | - function run(array &$object, string $ruleName):void { |
|
| 22 | + } |
|
| 23 | + /** |
|
| 24 | + * [run description] |
|
| 25 | + * @param array $object [description] |
|
| 26 | + * @param string $ruleName [description] |
|
| 27 | + */ |
|
| 28 | + function run(array &$object, string $ruleName):void { |
|
| 29 | 29 | $rule = $this->ci->config->item("refactor_$ruleName"); |
| 30 | 30 | if ($rule == null) return; // No need to go further as rule doesn't exist. |
| 31 | 31 | // Unset |
| 32 | 32 | if (isset($rule['unset'])) { |
| 33 | - $this->unset_values($object, $rule); |
|
| 33 | + $this->unset_values($object, $rule); |
|
| 34 | 34 | } |
| 35 | 35 | // Replace |
| 36 | 36 | if (isset($rule['replace'])) { |
| 37 | - $this->replace_fields($object, $rule); |
|
| 37 | + $this->replace_fields($object, $rule); |
|
| 38 | 38 | } |
| 39 | 39 | // Bools |
| 40 | 40 | if (isset($rule['bools'])) { |
| 41 | - foreach($rule['bools'] as $boolKey) { |
|
| 41 | + foreach($rule['bools'] as $boolKey) { |
|
| 42 | 42 | $object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true'; |
| 43 | - } |
|
| 43 | + } |
|
| 44 | 44 | } |
| 45 | 45 | // Objects |
| 46 | 46 | if (isset($rule['objects'])) { |
| 47 | - foreach($rule['objects'] as $field => $data) { |
|
| 47 | + foreach($rule['objects'] as $field => $data) { |
|
| 48 | 48 | $ids = json_decode($object[$field]); |
| 49 | 49 | if (is_scalar($ids)) { |
| 50 | - // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
| 51 | - $this->ci->db->where($this->primaryKey, $ids); |
|
| 52 | - $query = $this->ci->db->get($data['table']); |
|
| 53 | - if ($query->num_rows() == 0) { |
|
| 50 | + // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
| 51 | + $this->ci->db->where($this->primaryKey, $ids); |
|
| 52 | + $query = $this->ci->db->get($data['table']); |
|
| 53 | + if ($query->num_rows() == 0) { |
|
| 54 | 54 | $object[$field] = json_encode (json_decode ("{}")); |
| 55 | 55 | continue; |
| 56 | - } |
|
| 57 | - $object[$field] = $query->result_array()[0]; |
|
| 58 | - if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 59 | - continue; |
|
| 56 | + } |
|
| 57 | + $object[$field] = $query->result_array()[0]; |
|
| 58 | + if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 59 | + continue; |
|
| 60 | 60 | } |
| 61 | 61 | $object[$field] = []; |
| 62 | 62 | foreach($ids as $id) { |
| 63 | - $this->ci->db->where($this->primaryKey, $id); |
|
| 64 | - $query = $this->ci->db->get($data['table']); |
|
| 65 | - if ($query->num_rows() == 0) { |
|
| 63 | + $this->ci->db->where($this->primaryKey, $id); |
|
| 64 | + $query = $this->ci->db->get($data['table']); |
|
| 65 | + if ($query->num_rows() == 0) { |
|
| 66 | 66 | continue; |
| 67 | - } |
|
| 68 | - $object[$field][] = $query->result_array()[0]; |
|
| 69 | - // Recursion |
|
| 70 | - if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 67 | + } |
|
| 68 | + $object[$field][] = $query->result_array()[0]; |
|
| 69 | + // Recursion |
|
| 70 | + if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 71 | 71 | } |
| 72 | - } |
|
| 72 | + } |
|
| 73 | + } |
|
| 73 | 74 | } |
| 74 | - } |
|
| 75 | - /** |
|
| 76 | - * [unset_values description] |
|
| 77 | - * @param array $object Object to Refactor. |
|
| 78 | - * @param array $rule Rule data, containing keys to unset in the given |
|
| 79 | - * associative array. |
|
| 80 | - */ |
|
| 81 | - private function unset_values(array &$object, &$rule):void { |
|
| 75 | + /** |
|
| 76 | + * [unset_values description] |
|
| 77 | + * @param array $object Object to Refactor. |
|
| 78 | + * @param array $rule Rule data, containing keys to unset in the given |
|
| 79 | + * associative array. |
|
| 80 | + */ |
|
| 81 | + private function unset_values(array &$object, &$rule):void { |
|
| 82 | 82 | foreach($rule['unset'] as $key) { |
| 83 | - unset($object[$key]); |
|
| 83 | + unset($object[$key]); |
|
| 84 | 84 | } |
| 85 | - } |
|
| 86 | - /** |
|
| 87 | - * [replace_fields description] |
|
| 88 | - * @param array $object [description] |
|
| 89 | - * @param [type] $rule [description] |
|
| 90 | - */ |
|
| 91 | - private function replace_fields(array &$object, &$rule):void { |
|
| 85 | + } |
|
| 86 | + /** |
|
| 87 | + * [replace_fields description] |
|
| 88 | + * @param array $object [description] |
|
| 89 | + * @param [type] $rule [description] |
|
| 90 | + */ |
|
| 91 | + private function replace_fields(array &$object, &$rule):void { |
|
| 92 | 92 | foreach ($rule['replace'] as $oldKey => $newKey) { |
| 93 | - $object[$newKey] = $object[$oldKey]; |
|
| 94 | - unset($object[$oldKey]); |
|
| 93 | + $object[$newKey] = $object[$oldKey]; |
|
| 94 | + unset($object[$oldKey]); |
|
| 95 | + } |
|
| 95 | 96 | } |
| 96 | - } |
|
| 97 | 97 | } |
| 98 | 98 | ?> |
@@ -27,7 +27,10 @@ discard block |
||
| 27 | 27 | */ |
| 28 | 28 | function run(array &$object, string $ruleName):void { |
| 29 | 29 | $rule = $this->ci->config->item("refactor_$ruleName"); |
| 30 | - if ($rule == null) return; // No need to go further as rule doesn't exist. |
|
| 30 | + if ($rule == null) { |
|
| 31 | + return; |
|
| 32 | + } |
|
| 33 | + // No need to go further as rule doesn't exist. |
|
| 31 | 34 | // Unset |
| 32 | 35 | if (isset($rule['unset'])) { |
| 33 | 36 | $this->unset_values($object, $rule); |
@@ -55,7 +58,9 @@ discard block |
||
| 55 | 58 | continue; |
| 56 | 59 | } |
| 57 | 60 | $object[$field] = $query->result_array()[0]; |
| 58 | - if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 61 | + if (isset($data['refactor'])) { |
|
| 62 | + $this->run($object[$field], $data['refactor']); |
|
| 63 | + } |
|
| 59 | 64 | continue; |
| 60 | 65 | } |
| 61 | 66 | $object[$field] = []; |
@@ -67,7 +72,9 @@ discard block |
||
| 67 | 72 | } |
| 68 | 73 | $object[$field][] = $query->result_array()[0]; |
| 69 | 74 | // Recursion |
| 70 | - if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 75 | + if (isset($data['refactor'])) { |
|
| 76 | + $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 77 | + } |
|
| 71 | 78 | } |
| 72 | 79 | } |
| 73 | 80 | } |