1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LegalThings\DataEnricher\Processor; |
4
|
|
|
|
5
|
|
|
use LegalThings\DataEnricher\Node; |
6
|
|
|
use LegalThings\DataEnricher\Processor; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Transform processor, apply transformation functions on data |
10
|
|
|
*/ |
11
|
|
|
class Transform implements Processor |
12
|
|
|
{ |
13
|
|
|
use Processor\Implementation; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default transformation functions |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public static $defaultFunctions = [ |
20
|
|
|
'hash' => 'hash', |
21
|
|
|
'base64_encode' => 'base64_encode', |
22
|
|
|
'base64_decode' => 'base64_decode', |
23
|
|
|
'json_encode' => 'json_encode', |
24
|
|
|
'json_decode' => 'json_decode', |
25
|
|
|
'serialize' => 'serialize', |
26
|
|
|
'unserialize' => 'unserialize', |
27
|
|
|
'strtotime' => 'strtotime', |
28
|
|
|
'date' => 'date' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Allowed transformation functions |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public $functions; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class constructor |
40
|
|
|
* |
41
|
|
|
* @param string $property Property key with the processing instruction |
42
|
|
|
*/ |
43
|
6 |
|
public function __construct($property) |
44
|
|
|
{ |
45
|
6 |
|
$this->property = $property; |
46
|
6 |
|
$this->functions = static::$defaultFunctions; |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Apply processing to a single node |
51
|
|
|
* |
52
|
|
|
* @param Node $node |
53
|
|
|
*/ |
54
|
3 |
|
public function applyToNode(Node $node) |
55
|
|
|
{ |
56
|
3 |
|
$transformations = (array)$node->getInstruction($this); |
57
|
|
|
|
58
|
3 |
|
if (!isset($node->input)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
$value = $node->input; |
63
|
|
|
|
64
|
3 |
|
if ($value instanceof Node) { |
65
|
|
|
$value = $value->getResult(); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
foreach ($transformations as $transformation) { |
69
|
3 |
|
list($key, $arg) = explode(':', $transformation) + [null, null]; |
70
|
|
|
|
71
|
3 |
|
if (!isset($this->functions[$key])) { |
72
|
|
|
trigger_error("Unknown transformation '$transformation'", E_USER_WARNING); |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$fn = $this->functions[$key]; |
77
|
3 |
|
$value = isset($arg) ? call_user_func($fn, $arg, $value) : call_user_func($fn, $value); |
78
|
3 |
|
} |
79
|
|
|
|
80
|
3 |
|
$node->setResult($value); |
81
|
3 |
|
} |
82
|
|
|
} |
83
|
|
|
|