Skip to content

Commit

Permalink
Hardening + RandomInt + Persistence (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeofguenter authored Feb 5, 2018
1 parent f6820de commit ed6c9de
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 32 deletions.
20 changes: 20 additions & 0 deletions app/Transformer/Persistence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Transformer;

class Persistence
{
protected static $storage;

public static function set($key, $value)
{
static::$storage[$key] = $value;
}

public static function get($key)
{
if (isset(static::$storage[$key])) {
return static::$storage[$key];
}
}
}
25 changes: 24 additions & 1 deletion app/Transformer/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Rule
{
public $table;

public $column;

public $name;
Expand All @@ -12,11 +14,19 @@ class Rule

public $condition;

public function __construct($column, array $rule)
public $transformer;

public $persistentKey;

public function __construct($table, $column, array $rule)
{
$this->table = $table;

$this->column = $column;

$this->extractNameAndParam($rule);

$this->getTransformer();
}

protected function extractNameAndParam(array $rule)
Expand All @@ -36,8 +46,21 @@ protected function extractNameAndParam(array $rule)
unset($rule['Condition']);
}

if (isset($rule['PersistentKey'])) {
$this->persistentKey = $rule['PersistentKey'];
unset($rule['PersistentKey']);
}

$this->name = key($rule);
$this->param = current($rule);
}
}

protected function getTransformer()
{
$class_name = 'App\\Transformer\\Transformers\\' . $this->name;
$this->transformer = new $class_name;
$this->transformer->setPersistentKey($this->persistentKey);
$this->transformer->setParam($this->param);
}
}
11 changes: 7 additions & 4 deletions app/Transformer/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

class RuleSet implements Iterator
{
protected $table;

protected $rules = [];

protected $column = [];
protected $columns = [];

protected $pos;

public function __construct(array $rules)
public function __construct($table, array $rules)
{
$this->table = $table;
$this->rules = $rules;
$this->columns = array_keys($rules);
$this->pos = 0;
Expand All @@ -38,12 +41,12 @@ public function current()
$rules = [];

foreach($rule as $each) {
$rules[] = new Rule($column, $each);
$rules[] = new Rule($this->table, $column, $each);
}

return $rules;
} else {
return new Rule($column, $rule);
return new Rule($this->table, $column, $rule);
}
}

Expand Down
50 changes: 23 additions & 27 deletions app/Transformer/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

class Transform
{
protected $mappings = [];
protected $rule_sets = [];

protected $transformers = [];

public function __construct(array $mappings)
{
$this->mappings = $mappings;
foreach ($mappings as $table => $rules) {
$this->rule_sets[$table] = new RuleSet($table, $rules);
}
}

public function transform($row)
Expand All @@ -24,7 +26,7 @@ public function transform($row)

$table = $match[1];

if (!isset($this->mappings[$table])) {
if (!isset($this->rule_sets[$table])) {
return $row;
}

Expand All @@ -34,9 +36,7 @@ public function transform($row)
return $row;
}

$mappings = $this->mappings[$table];

$new_values = $this->map($insert->columns, $insert->values, $mappings);
$new_values = $this->map($table, $insert->columns, $insert->values);

return $this->build($table, $insert->columns, $new_values);
}
Expand All @@ -57,12 +57,14 @@ protected function build($table, array $columns, array $values)
);
}

protected function map(array $columns, array $values, array $mappings)
protected function map($table, array $columns, array $values)
{
$columns_n = array_flip($columns);
$ruleset = new RuleSet($mappings);

foreach ($ruleset as $rule) {
// iterate every rule against values (= value groups)
// -> that means every rule will only target a specific column of
// each value group
foreach ($this->rule_sets[$table] as $rule) {
$this->transformValues($rule, $columns_n, $values);
}

Expand All @@ -72,39 +74,33 @@ protected function map(array $columns, array $values, array $mappings)
protected function transformValues($rule, array $columns_n, array &$values)
{
foreach ($values as $n => $value_group) {
$transformer = null;
$apply_rule = null;

if (is_array($rule)) {
foreach ($rule as $each) {
if (!isset($columns_n[$each->column])) {
continue 2;
}

if ($each->condition === null || $each->condition->evaluate($values[$n][$columns_n[$each->column]])) {
$column_n = $columns_n[$each->column];
$transformer = $this->getTransformer($each);
$apply_rule = $each;
break;
}
}

if (!$transformer) {
if (!$apply_rule) {
continue;
}

} else {
$transformer = $this->getTransformer($rule);
$apply_rule = $rule;
if (!isset($columns_n[$rule->column])) {
continue;
}
$column_n = $columns_n[$rule->column];
}


$values[$n][$column_n] = $transformer->transform($columns_n, $value_group, $column_n);
}
}

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);
$values[$n][$column_n] = $apply_rule->transformer->transform($columns_n, $value_group, $column_n);
}

return $this->transformers[$rule->name];
}
}
23 changes: 23 additions & 0 deletions app/Transformer/Transformers/RandomInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Transformer\Transformers;

class RandomInt extends Transformer
{
public function transform(array $columns_n, array $values, $column)
{
return $this->replaceValue($values[$column], $this->randomNumber());
}

protected function randomNumber()
{
$min = (int) ('1' . str_repeat('0', $this->param - 1));
$max = (int) str_repeat('9', $this->param);

if (function_exists('random_int')) {
return random_int($min, $max);
} else {
return rand($min, $max);
}
}
}
21 changes: 21 additions & 0 deletions app/Transformer/Transformers/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,45 @@

namespace App\Transformer\Transformers;

use App\Transformer\Persistence;

abstract class Transformer implements TransformerInterface
{
protected $param;

protected $persistent_key;

public function setParam($param)
{
$this->param = $param;
}

public function setPersistentKey($persistent_key)
{
$this->persistent_key = $persistent_key;
}

protected function replaceValue($original, $new)
{
if ($original === 'NULL' || $original === "''") {
return $original;
}

if ($this->persistent_key) {
$full_persistent_key = sprintf('%s/%s', $this->persistent_key, $original);

$persistent_value = Persistence::get($full_persistent_key);
if ($persistent_value !== null) {
$new = $persistent_value;
} else {
Persistence::set($full_persistent_key, $new);
}
}

if ($original[0] === "'" && $original[-1] === "'") {
return "'{$new}'";
}

return $new;
}
}

0 comments on commit ed6c9de

Please sign in to comment.