Skip to content

Commit

Permalink
support conditions (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeofguenter authored Jan 29, 2018
1 parent 3675c94 commit f6820de
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 19 deletions.
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -22,7 +22,7 @@ $ mysqldump \
> database_structure.sql
```

create data dump
### create data dump

```bash
$ mysqldump \
Expand All @@ -36,33 +36,59 @@ $ mysqldump \
> database_data.sql
```

create config
### create config

```bash
$ cat <<'EOF' > .myform.json
{
"table_name": [{
"columnA": {
"Tel": []
"Tel": null
}
},
{
"columnB": {
"Email": []
"Email": "qa+%[email protected]"
}
},
{
"columnC": {
"Replace": ["foo", "bar"]
"Set": "static content"
}
}
]
}
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)
13 changes: 13 additions & 0 deletions app/Transformer/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Transformer;

abstract class Condition implements ConditionInterface
{
protected $param;

public function __construct($param)
{
$this->param = $param;
}
}
8 changes: 8 additions & 0 deletions app/Transformer/ConditionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Transformer;

interface ConditionInterface
{
public function evaluate($against);
}
13 changes: 13 additions & 0 deletions app/Transformer/Conditions/Contains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Transformer\Conditions;

use App\Transformer\Condition;

class Contains extends Condition
{
public function evaluate($against)
{
return (bool) stripos($against, $this->param);
}
}
43 changes: 43 additions & 0 deletions app/Transformer/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Transformer;

class Rule
{
public $column;

public $name;

public $param;

public $condition;

public function __construct($column, array $rule)
{
$this->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);
}
}
}
59 changes: 59 additions & 0 deletions app/Transformer/RuleSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Transformer;

use Iterator;

class RuleSet implements Iterator
{
protected $rules = [];

protected $column = [];

protected $pos;

public function __construct(array $rules)
{
$this->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;
}
}
50 changes: 39 additions & 11 deletions app/Transformer/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

0 comments on commit f6820de

Please sign in to comment.