@@ -3,117 +3,117 @@ |
||
| 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 | // Cast |
| 46 | 46 | if (isset($rule['cast'])) { |
| 47 | - $this->cast_fields($object, $rule); |
|
| 47 | + $this->cast_fields($object, $rule); |
|
| 48 | 48 | } |
| 49 | 49 | // Objects |
| 50 | 50 | if (isset($rule['objects'])) { |
| 51 | - foreach($rule['objects'] as $field => $data) { |
|
| 51 | + foreach($rule['objects'] as $field => $data) { |
|
| 52 | 52 | $ids = json_decode($object[$field]); |
| 53 | 53 | if (is_scalar($ids)) { |
| 54 | - // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
| 55 | - $this->ci->db->where($this->primaryKey, $ids); |
|
| 56 | - $query = $this->ci->db->get($data['table']); |
|
| 57 | - if ($query->num_rows() == 0) { |
|
| 54 | + // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
| 55 | + $this->ci->db->where($this->primaryKey, $ids); |
|
| 56 | + $query = $this->ci->db->get($data['table']); |
|
| 57 | + if ($query->num_rows() == 0) { |
|
| 58 | 58 | $object[$field] = json_encode (json_decode ("{}")); |
| 59 | 59 | continue; |
| 60 | - } |
|
| 61 | - $object[$field] = $query->result_array()[0]; |
|
| 62 | - if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 63 | - continue; |
|
| 60 | + } |
|
| 61 | + $object[$field] = $query->result_array()[0]; |
|
| 62 | + if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 63 | + continue; |
|
| 64 | 64 | } |
| 65 | 65 | $object[$field] = []; |
| 66 | 66 | foreach($ids as $id) { |
| 67 | - $this->ci->db->where($this->primaryKey, $id); |
|
| 68 | - $query = $this->ci->db->get($data['table']); |
|
| 69 | - if ($query->num_rows() == 0) { |
|
| 67 | + $this->ci->db->where($this->primaryKey, $id); |
|
| 68 | + $query = $this->ci->db->get($data['table']); |
|
| 69 | + if ($query->num_rows() == 0) { |
|
| 70 | 70 | continue; |
| 71 | - } |
|
| 72 | - $object[$field][] = $query->result_array()[0]; |
|
| 73 | - // Recursion |
|
| 74 | - if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 71 | + } |
|
| 72 | + $object[$field][] = $query->result_array()[0]; |
|
| 73 | + // Recursion |
|
| 74 | + if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 75 | + } |
|
| 75 | 76 | } |
| 76 | - } |
|
| 77 | 77 | } |
| 78 | - } |
|
| 79 | - /** |
|
| 80 | - * [unset_values description] |
|
| 81 | - * @param array $object Object to Refactor. |
|
| 82 | - * @param array $rule Rule data, containing keys to unset in the given |
|
| 83 | - * associative array. |
|
| 84 | - */ |
|
| 85 | - private function unset_values(array &$object, &$rule):void { |
|
| 78 | + } |
|
| 79 | + /** |
|
| 80 | + * [unset_values description] |
|
| 81 | + * @param array $object Object to Refactor. |
|
| 82 | + * @param array $rule Rule data, containing keys to unset in the given |
|
| 83 | + * associative array. |
|
| 84 | + */ |
|
| 85 | + private function unset_values(array &$object, &$rule):void { |
|
| 86 | 86 | foreach($rule['unset'] as $key) { |
| 87 | - unset($object[$key]); |
|
| 87 | + unset($object[$key]); |
|
| 88 | + } |
|
| 88 | 89 | } |
| 89 | - } |
|
| 90 | - /** |
|
| 91 | - * [replace_fields description] |
|
| 92 | - * @param array $object [description] |
|
| 93 | - * @param [type] $rule [description] |
|
| 94 | - */ |
|
| 95 | - private function replace_fields(array &$object, &$rule):void { |
|
| 90 | + /** |
|
| 91 | + * [replace_fields description] |
|
| 92 | + * @param array $object [description] |
|
| 93 | + * @param [type] $rule [description] |
|
| 94 | + */ |
|
| 95 | + private function replace_fields(array &$object, &$rule):void { |
|
| 96 | 96 | foreach ($rule['replace'] as $oldKey => $newKey) { |
| 97 | - $object[$newKey] = $object[$oldKey]; |
|
| 98 | - unset($object[$oldKey]); |
|
| 97 | + $object[$newKey] = $object[$oldKey]; |
|
| 98 | + unset($object[$oldKey]); |
|
| 99 | + } |
|
| 99 | 100 | } |
| 100 | - } |
|
| 101 | - /** |
|
| 102 | - * [cast_fields description] |
|
| 103 | - * @param array $object [description] |
|
| 104 | - * @param [type] $rule [description] |
|
| 105 | - */ |
|
| 106 | - private function cast_fields(array &$object, &$rule):void { |
|
| 101 | + /** |
|
| 102 | + * [cast_fields description] |
|
| 103 | + * @param array $object [description] |
|
| 104 | + * @param [type] $rule [description] |
|
| 105 | + */ |
|
| 106 | + private function cast_fields(array &$object, &$rule):void { |
|
| 107 | 107 | foreach ($rule['cast'] as $key => $type) { |
| 108 | - switch ($type) { |
|
| 108 | + switch ($type) { |
|
| 109 | 109 | case 'int': |
| 110 | 110 | $object[$key] = (int) $object[$key]; |
| 111 | - break; |
|
| 111 | + break; |
|
| 112 | 112 | case 'string': |
| 113 | 113 | $object[$key] = (string) $object[$key]; |
| 114 | - break; |
|
| 115 | - } |
|
| 114 | + break; |
|
| 115 | + } |
|
| 116 | + } |
|
| 116 | 117 | } |
| 117 | - } |
|
| 118 | 118 | } |
| 119 | 119 | ?> |
@@ -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 | } |
@@ -38,24 +38,24 @@ discard block |
||
| 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 | // Cast |
| 46 | - if (isset($rule['cast'])) { |
|
| 46 | + if (isset($rule['cast'])) { |
|
| 47 | 47 | $this->cast_fields($object, $rule); |
| 48 | 48 | } |
| 49 | 49 | // Objects |
| 50 | 50 | if (isset($rule['objects'])) { |
| 51 | - foreach($rule['objects'] as $field => $data) { |
|
| 51 | + foreach ($rule['objects'] as $field => $data) { |
|
| 52 | 52 | $ids = json_decode($object[$field]); |
| 53 | 53 | if (is_scalar($ids)) { |
| 54 | 54 | // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
| 55 | 55 | $this->ci->db->where($this->primaryKey, $ids); |
| 56 | 56 | $query = $this->ci->db->get($data['table']); |
| 57 | 57 | if ($query->num_rows() == 0) { |
| 58 | - $object[$field] = json_encode (json_decode ("{}")); |
|
| 58 | + $object[$field] = json_encode(json_decode("{}")); |
|
| 59 | 59 | continue; |
| 60 | 60 | } |
| 61 | 61 | $object[$field] = $query->result_array()[0]; |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | continue; |
| 64 | 64 | } |
| 65 | 65 | $object[$field] = []; |
| 66 | - foreach($ids as $id) { |
|
| 66 | + foreach ($ids as $id) { |
|
| 67 | 67 | $this->ci->db->where($this->primaryKey, $id); |
| 68 | 68 | $query = $this->ci->db->get($data['table']); |
| 69 | 69 | if ($query->num_rows() == 0) { |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * associative array. |
| 84 | 84 | */ |
| 85 | 85 | private function unset_values(array &$object, &$rule):void { |
| 86 | - foreach($rule['unset'] as $key) { |
|
| 86 | + foreach ($rule['unset'] as $key) { |
|
| 87 | 87 | unset($object[$key]); |
| 88 | 88 | } |
| 89 | 89 | } |