@@ -3,141 +3,141 @@ |
||
| 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 | // Inflate |
| 67 | 67 | if (isset($rule['inflate'])) { |
| 68 | - foreach($rule['inflate'] as $field => $data) { |
|
| 68 | + foreach($rule['inflate'] as $field => $data) { |
|
| 69 | 69 | |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | } |
| 72 | 72 | // Inflate Array |
| 73 | 73 | if (isset($rule['inflate_array'])) { |
| 74 | - foreach($rule['inflate_array'] as $field => $data) { |
|
| 74 | + foreach($rule['inflate_array'] as $field => $data) { |
|
| 75 | 75 | $ids = json_decode($object[$field]); |
| 76 | 76 | if (is_scalar($ids)) { |
| 77 | - // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
| 78 | - $this->ci->db->where($this->primaryKey, $ids); |
|
| 79 | - $query = $this->ci->db->get($data['table']); |
|
| 80 | - if ($query->num_rows() == 0) { |
|
| 77 | + // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
|
| 78 | + $this->ci->db->where($this->primaryKey, $ids); |
|
| 79 | + $query = $this->ci->db->get($data['table']); |
|
| 80 | + if ($query->num_rows() == 0) { |
|
| 81 | 81 | $object[$field] = json_encode (json_decode ("{}")); |
| 82 | 82 | continue; |
| 83 | - } |
|
| 84 | - $object[$field] = $query->result_array()[0]; |
|
| 85 | - if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 86 | - continue; |
|
| 83 | + } |
|
| 84 | + $object[$field] = $query->result_array()[0]; |
|
| 85 | + if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']); |
|
| 86 | + continue; |
|
| 87 | 87 | } |
| 88 | 88 | $object[$field] = []; |
| 89 | 89 | if ($ids == null) return; |
| 90 | 90 | foreach($ids as $id) { |
| 91 | - $this->ci->db->where($this->primaryKey, $id); |
|
| 92 | - $query = $this->ci->db->get($data['table']); |
|
| 93 | - if ($query->num_rows() == 0) { |
|
| 91 | + $this->ci->db->where($this->primaryKey, $id); |
|
| 92 | + $query = $this->ci->db->get($data['table']); |
|
| 93 | + if ($query->num_rows() == 0) { |
|
| 94 | 94 | continue; |
| 95 | - } |
|
| 96 | - $object[$field][] = $query->result_array()[0]; |
|
| 97 | - // Recursion |
|
| 98 | - if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 95 | + } |
|
| 96 | + $object[$field][] = $query->result_array()[0]; |
|
| 97 | + // Recursion |
|
| 98 | + if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']); |
|
| 99 | 99 | } |
| 100 | - } |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - /** |
|
| 104 | - * [unset_values description] |
|
| 105 | - * @param array $object Object to Refactor. |
|
| 106 | - * @param array $rule Rule data, containing keys to unset in the given |
|
| 107 | - * associative array. |
|
| 108 | - */ |
|
| 109 | - private function unset_values(array &$object, &$rule):void { |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + /** |
|
| 104 | + * [unset_values description] |
|
| 105 | + * @param array $object Object to Refactor. |
|
| 106 | + * @param array $rule Rule data, containing keys to unset in the given |
|
| 107 | + * associative array. |
|
| 108 | + */ |
|
| 109 | + private function unset_values(array &$object, &$rule):void { |
|
| 110 | 110 | foreach($rule['unset'] as $key) { |
| 111 | - unset($object[$key]); |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - /** |
|
| 115 | - * [replace_fields description] |
|
| 116 | - * @param array $object [description] |
|
| 117 | - * @param [type] $rule [description] |
|
| 118 | - */ |
|
| 119 | - private function replace_fields(array &$object, &$rule):void { |
|
| 111 | + unset($object[$key]); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + /** |
|
| 115 | + * [replace_fields description] |
|
| 116 | + * @param array $object [description] |
|
| 117 | + * @param [type] $rule [description] |
|
| 118 | + */ |
|
| 119 | + private function replace_fields(array &$object, &$rule):void { |
|
| 120 | 120 | foreach ($rule['replace'] as $oldKey => $newKey) { |
| 121 | - $object[$newKey] = $object[$oldKey]; |
|
| 122 | - unset($object[$oldKey]); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - /** |
|
| 126 | - * [cast_fields description] |
|
| 127 | - * @param array $object [description] |
|
| 128 | - * @param [type] $rule [description] |
|
| 129 | - */ |
|
| 130 | - private function cast_fields(array &$object, &$rule):void { |
|
| 121 | + $object[$newKey] = $object[$oldKey]; |
|
| 122 | + unset($object[$oldKey]); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + /** |
|
| 126 | + * [cast_fields description] |
|
| 127 | + * @param array $object [description] |
|
| 128 | + * @param [type] $rule [description] |
|
| 129 | + */ |
|
| 130 | + private function cast_fields(array &$object, &$rule):void { |
|
| 131 | 131 | foreach ($rule['cast'] as $key => $type) { |
| 132 | - switch ($type) { |
|
| 132 | + switch ($type) { |
|
| 133 | 133 | case 'int': |
| 134 | 134 | $object[$key] = (int) $object[$key]; |
| 135 | - break; |
|
| 135 | + break; |
|
| 136 | 136 | case 'string': |
| 137 | 137 | $object[$key] = (string) $object[$key]; |
| 138 | - break; |
|
| 139 | - } |
|
| 138 | + break; |
|
| 139 | + } |
|
| 140 | + } |
|
| 140 | 141 | } |
| 141 | - } |
|
| 142 | 142 | } |
| 143 | 143 | ?> |
@@ -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,30 +55,30 @@ 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 | // Inflate |
| 67 | 67 | if (isset($rule['inflate'])) { |
| 68 | - foreach($rule['inflate'] as $field => $data) { |
|
| 68 | + foreach ($rule['inflate'] as $field => $data) { |
|
| 69 | 69 | |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | // Inflate Array |
| 73 | 73 | if (isset($rule['inflate_array'])) { |
| 74 | - foreach($rule['inflate_array'] as $field => $data) { |
|
| 74 | + foreach ($rule['inflate_array'] as $field => $data) { |
|
| 75 | 75 | $ids = json_decode($object[$field]); |
| 76 | 76 | if (is_scalar($ids)) { |
| 77 | 77 | // JSON Array wasn't supplied. Let's treat it as a scaler ID. |
| 78 | 78 | $this->ci->db->where($this->primaryKey, $ids); |
| 79 | 79 | $query = $this->ci->db->get($data['table']); |
| 80 | 80 | if ($query->num_rows() == 0) { |
| 81 | - $object[$field] = json_encode (json_decode ("{}")); |
|
| 81 | + $object[$field] = json_encode(json_decode("{}")); |
|
| 82 | 82 | continue; |
| 83 | 83 | } |
| 84 | 84 | $object[$field] = $query->result_array()[0]; |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | } |
| 88 | 88 | $object[$field] = []; |
| 89 | 89 | if ($ids == null) return; |
| 90 | - foreach($ids as $id) { |
|
| 90 | + foreach ($ids as $id) { |
|
| 91 | 91 | $this->ci->db->where($this->primaryKey, $id); |
| 92 | 92 | $query = $this->ci->db->get($data['table']); |
| 93 | 93 | if ($query->num_rows() == 0) { |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | * associative array. |
| 108 | 108 | */ |
| 109 | 109 | private function unset_values(array &$object, &$rule):void { |
| 110 | - foreach($rule['unset'] as $key) { |
|
| 110 | + foreach ($rule['unset'] as $key) { |
|
| 111 | 111 | unset($object[$key]); |
| 112 | 112 | } |
| 113 | 113 | } |