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