diff --git a/README.md b/README.md index 7744743..d339823 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ $ curl -#fL "$(curl -s https://api.github.com/repos/NINEJKH/myformer/releases/la ## Example -create structure dump +### create structure dump ```bash $ mysqldump \ @@ -22,7 +22,7 @@ $ mysqldump \ > database_structure.sql ``` -create data dump +### create data dump ```bash $ mysqldump \ @@ -36,24 +36,24 @@ $ mysqldump \ > database_data.sql ``` -create config +### create config ```bash $ cat <<'EOF' > .myform.json { "table_name": [{ "columnA": { - "Tel": [] + "Tel": null } }, { "columnB": { - "Email": [] + "Email": "qa+%s@company.com" } }, { "columnC": { - "Replace": ["foo", "bar"] + "Set": "static content" } } ] @@ -61,8 +61,34 @@ $ cat <<'EOF' > .myform.json EOF ``` -anonymise data +### anonymise data ```bash -$ myformer +$ myformer transform *_data.sql ``` + +This will create a file with the same name + postfixed with "+transformed". + +## Rules + +### Email + +Anonymise Email addresses. + +Accepts a string parameter, `%s` will be replaced with the first 16 chars +of the md5 hash of the original value. + +### Ref + +Set column value from another column value. + +Accepts a string parameter, which will be used as column name of the +source value. + +### Set + +Statically set the value of the string parameter + +### Tel + +Random telphone-like number (999 + 10-digits) diff --git a/app/Transformer/Condition.php b/app/Transformer/Condition.php new file mode 100644 index 0000000..92add03 --- /dev/null +++ b/app/Transformer/Condition.php @@ -0,0 +1,13 @@ +param = $param; + } +} diff --git a/app/Transformer/ConditionInterface.php b/app/Transformer/ConditionInterface.php new file mode 100644 index 0000000..f66fa55 --- /dev/null +++ b/app/Transformer/ConditionInterface.php @@ -0,0 +1,8 @@ +param); + } +} diff --git a/app/Transformer/Rule.php b/app/Transformer/Rule.php new file mode 100644 index 0000000..df04deb --- /dev/null +++ b/app/Transformer/Rule.php @@ -0,0 +1,43 @@ +column = $column; + + $this->extractNameAndParam($rule); + } + + protected function extractNameAndParam(array $rule) + { + if (count($rule) === 1) { + $this->name = key($rule); + $this->param = current($rule); + } else { + if (isset($rule['Condition'])) { + $condition_class = 'App\\Transformer\\Conditions\\' . key($rule['Condition']); + $this->condition = new $condition_class(current($rule['Condition'])); + //$this->condition = [ + // 'name' => key($rule['Condition']), + // 'param' => current($rule['Condition']), + //]; + + unset($rule['Condition']); + } + + $this->name = key($rule); + $this->param = current($rule); + } + } +} diff --git a/app/Transformer/RuleSet.php b/app/Transformer/RuleSet.php new file mode 100644 index 0000000..ccf5f26 --- /dev/null +++ b/app/Transformer/RuleSet.php @@ -0,0 +1,59 @@ +rules = $rules; + $this->columns = array_keys($rules); + $this->pos = 0; + } + + public function next() + { + ++$this->pos; + } + + public function valid() + { + return isset($this->columns[$this->pos]); + } + + public function current() + { + $column = $this->columns[$this->pos]; + $rule = $this->rules[$column]; + + if (isset($rule[0])) { + $rules = []; + + foreach($rule as $each) { + $rules[] = new Rule($column, $each); + } + + return $rules; + } else { + return new Rule($column, $rule); + } + } + + public function rewind() + { + $this->pos = 0; + } + + public function key() + { + return $this->pos; + } +} diff --git a/app/Transformer/Transform.php b/app/Transformer/Transform.php index dcd9dba..9c6018b 100644 --- a/app/Transformer/Transform.php +++ b/app/Transformer/Transform.php @@ -60,23 +60,51 @@ protected function build($table, array $columns, array $values) protected function map(array $columns, array $values, array $mappings) { $columns_n = array_flip($columns); + $ruleset = new RuleSet($mappings); - foreach ($mappings as $column => $rule) { - $rule_name = key($rule); + foreach ($ruleset as $rule) { + $this->transformValues($rule, $columns_n, $values); + } - if (!isset($this->transformers[$rule_name])) { - $class_name = 'App\\Transformer\\Transformers\\' . $rule_name; - $this->transformers[$rule_name] = new $class_name; - $this->transformers[$rule_name]->setParam(current($rule)); - } + return $values; + } - foreach ($values as $n => $value_group) { - $column_n = $columns_n[$column]; - $values[$n][$column_n] = $this->transformers[$rule_name]->transform($columns_n, $value_group, $column_n); + protected function transformValues($rule, array $columns_n, array &$values) + { + foreach ($values as $n => $value_group) { + $transformer = null; + + if (is_array($rule)) { + foreach ($rule as $each) { + if ($each->condition === null || $each->condition->evaluate($values[$n][$columns_n[$each->column]])) { + $column_n = $columns_n[$each->column]; + $transformer = $this->getTransformer($each); + break; + } + } + + if (!$transformer) { + continue; + } + + } else { + $transformer = $this->getTransformer($rule); + $column_n = $columns_n[$rule->column]; } + + $values[$n][$column_n] = $transformer->transform($columns_n, $value_group, $column_n); } + } - return $values; + protected function getTransformer(Rule $rule) + { + if (!isset($this->transformers[$rule->name])) { + $class_name = 'App\\Transformer\\Transformers\\' . $rule->name; + $this->transformers[$rule->name] = new $class_name; + $this->transformers[$rule->name]->setParam($rule->param); + } + + return $this->transformers[$rule->name]; } }