From f68e9c85901c788dece107f52cf4e79c6ac62a57 Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Thu, 14 Jul 2016 16:15:50 +0200 Subject: [PATCH] Ability to set default functions for transform processor Set `Processor\Transform::$defaultFunctions` to configure (custom) allowed functions --- src/DataEnricher/Processor/Transform.php | 45 +++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/DataEnricher/Processor/Transform.php b/src/DataEnricher/Processor/Transform.php index 5059946..397ad49 100644 --- a/src/DataEnricher/Processor/Transform.php +++ b/src/DataEnricher/Processor/Transform.php @@ -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 * @@ -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);