Skip to content

Commit

Permalink
Merge pull request #16 from legalthings/transform-default-functions
Browse files Browse the repository at this point in the history
Ability to set default functions for transform processor
  • Loading branch information
svenstm authored Jul 14, 2016
2 parents 768131f + f68e9c8 commit 9732915
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/DataEnricher/Processor/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,37 @@ class Transform implements Processor
use Processor\Implementation;

/**
* Allowed transformation functions
* @var type
* Default transformation functions
* @var array
*/
public $allowed = [
'hash',
'base64_encode',
'base64_decode',
'json_encode',
'json_decode',
'serialize',
'unserialize'
public static $defaultFunctions = [
'hash' => 'hash',
'base64_encode' => 'base64_encode',
'base64_decode' => 'base64_decode',
'json_encode' => 'json_encode',
'json_decode' => 'json_decode',
'serialize' => 'serialize',
'unserialize' => 'unserialize'
];

/**
* Allowed transformation functions
* @var array
*/
public $functions;


/**
* Class constructor
*
* @param string $property Property key with the processing instruction
*/
public function __construct($property)
{
$this->property = $property;
$this->functions = static::$defaultFunctions;
}

/**
* Apply processing to a single node
*
Expand All @@ -46,14 +64,15 @@ public function applyToNode(Node $node)
}

foreach ($transformations as $transformation) {
list($fn, $arg) = explode(':', $transformation) + [null];
list($key, $arg) = explode(':', $transformation) + [null];

if (!in_array($fn, $this->allowed)) {
if (!isset($this->functions[$key])) {
trigger_error("Unknown transformation '$transformation'", E_USER_WARNING);
continue;
}

$value = isset($arg) ? $fn($arg, $value) : $fn($value);
$fn = $this->functions[$key];
$value = isset($arg) ? call_user_func($fn, $arg, $value) : call_user_func($fn, $value);
}

$node->setResult($value);
Expand Down

0 comments on commit 9732915

Please sign in to comment.