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
|
|
|
'hash_hmac' => 'hash_hmac', |
22
|
|
|
'base64_encode' => 'base64_encode', |
23
|
|
|
'base64_decode' => 'base64_decode', |
24
|
|
|
'json_encode' => 'json_encode', |
25
|
|
|
'json_decode' => 'json_decode', |
26
|
|
|
'serialize' => 'serialize', |
27
|
|
|
'unserialize' => 'unserialize', |
28
|
|
|
'strtotime' => 'strtotime', |
29
|
|
|
'date' => 'date' |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Allowed transformation functions |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public $functions; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class constructor |
41
|
|
|
* |
42
|
|
|
* @param string $property Property key with the processing instruction |
43
|
|
|
*/ |
44
|
12 |
|
public function __construct($property) |
45
|
|
|
{ |
46
|
12 |
|
$this->property = $property; |
47
|
12 |
|
$this->functions = static::$defaultFunctions; |
48
|
12 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Apply processing to a single node |
52
|
|
|
* |
53
|
|
|
* @param Node $node |
54
|
|
|
*/ |
55
|
8 |
|
public function applyToNode(Node $node) |
56
|
|
|
{ |
57
|
8 |
|
$instruction = $node->getInstruction($this); |
58
|
8 |
|
$transformations = !is_array($instruction) ? [$instruction] : $instruction; |
59
|
|
|
|
60
|
8 |
|
if (isset($node->input)) { |
61
|
5 |
|
$input = $this->resolveNodes($node->input); |
62
|
5 |
|
} |
63
|
|
|
|
64
|
8 |
|
foreach ($transformations as $transformation) { |
65
|
8 |
|
if (is_string($transformation) && !isset($input)) { |
66
|
1 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
if (is_string($transformation)) { |
70
|
5 |
|
list($key, $args) = explode(':', $transformation) + [null, null]; |
71
|
7 |
|
} elseif (is_object($transformation) || is_array($transformation)) { |
72
|
2 |
|
$transformation = (object)$transformation; |
73
|
2 |
|
$key = isset($transformation->function) ? $transformation->function : null; |
74
|
2 |
|
$args = isset($transformation->args) ? $transformation->args : []; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
7 |
|
if (!isset($this->functions[$key])) { |
78
|
1 |
|
throw new \Exception("Unknown transformation '$key'"); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
if (isset($args)) { |
82
|
5 |
|
$args = $this->resolveNodes($args); |
83
|
5 |
|
} |
84
|
|
|
|
85
|
6 |
|
$fn = $this->functions[$key]; |
|
|
|
|
86
|
|
|
|
87
|
6 |
|
if (is_string($transformation)) { |
88
|
4 |
|
$result = isset($args) ? call_user_func($fn, $args, $input) : call_user_func($fn, $input); |
|
|
|
|
89
|
6 |
|
} elseif (is_object($transformation)) { |
90
|
2 |
|
$result = call_user_func_array($fn, $args); |
|
|
|
|
91
|
2 |
|
} |
92
|
7 |
|
} |
93
|
|
|
|
94
|
7 |
|
if (isset($result)) { |
95
|
6 |
|
$node->setResult($result); |
96
|
6 |
|
} |
97
|
7 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Resolve instructions of nodes by getting their results |
101
|
|
|
* |
102
|
|
|
* @param Node $node |
103
|
|
|
* |
104
|
|
|
* @return mixed $result |
105
|
|
|
*/ |
106
|
7 |
|
protected function resolveNodes($node) { |
107
|
7 |
|
if (!$node instanceof Node) { |
108
|
7 |
|
return $node; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $node->getResult(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: