|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LegalThings; |
|
4
|
|
|
|
|
5
|
|
|
use LegalThings\DataEnricher\Node; |
|
6
|
|
|
use LegalThings\DataEnricher\Processor; |
|
7
|
|
|
use Jasny\DotKey; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Enrich objects by processing special properties. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DataEnricher |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Default processors |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
public static $defaultProcessors = [ |
|
19
|
|
|
'<ifset>' => Processor\IfSet::class, |
|
20
|
|
|
'<ref>' => Processor\Reference::class, |
|
21
|
|
|
'<eval>' => Processor\Evaluate::class, |
|
22
|
|
|
'<switch>' => Processor\SwitchChoose::class, |
|
23
|
|
|
'<merge>' => Processor\Merge::class, |
|
24
|
|
|
'<tpl>' => Processor\Mustache::class, |
|
25
|
|
|
'<src>' => Processor\Http::class, |
|
26
|
|
|
'<jmespath>' => Processor\JmesPath::class, |
|
27
|
|
|
'<apply>' => Processor\JmesPath::class, |
|
28
|
|
|
'<transformation>' => Processor\Transform::class, |
|
29
|
|
|
'<math>' => Processor\Math::class, |
|
30
|
|
|
'<sum>' => Processor\Sum::class, |
|
31
|
|
|
'<enrich>' => Processor\Enrich::class, |
|
32
|
|
|
'<dateformat>' => Processor\DateFormat::class, |
|
33
|
|
|
'<numberformat>' => Processor\NumberFormat::class, |
|
34
|
|
|
'<equal>' => Processor\Equal::class, |
|
35
|
|
|
'<match>' => Processor\Match::class, |
|
36
|
|
|
'<replace>' => Processor\Replace::class, |
|
37
|
|
|
'<if>' => Processor\IfElse::class, |
|
38
|
|
|
'<join>' => Processor\Join::class, |
|
39
|
|
|
'<encode>' => Processor\Encode::class, |
|
40
|
|
|
'<decode>' => Processor\Decode::class, |
|
41
|
|
|
'<serialize>' => Processor\Serialize::class, |
|
42
|
|
|
'<unserialize>' => Processor\Unserialize::class, |
|
43
|
|
|
'<hash>' => Processor\Hash::class, |
|
44
|
|
|
|
|
45
|
|
|
// Deprecated |
|
46
|
|
|
'_ref' => Processor\Reference::class, |
|
47
|
|
|
'_switch' => Processor\SwitchChoose::class, |
|
48
|
|
|
'_src' => Processor\Http::class, |
|
49
|
|
|
'_merge' => Processor\Merge::class, |
|
50
|
|
|
'_jmespath' => Processor\JmesPath::class, |
|
51
|
|
|
'_tpl' => Processor\Mustache::class, |
|
52
|
|
|
'_transformation' => Processor\Transform::class |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Processors, applied in specified order. |
|
58
|
|
|
* |
|
59
|
|
|
* @var DataEnricher\Processor[] |
|
60
|
|
|
*/ |
|
61
|
|
|
public $processors; |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Class constructor |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function __construct() |
|
68
|
|
|
{ |
|
69
|
5 |
|
foreach (static::$defaultProcessors as $property => $processor) { |
|
70
|
5 |
|
if (is_string($processor)) { |
|
71
|
5 |
|
$processor = new $processor($property); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
$this->processors[] = $processor; |
|
75
|
|
|
} |
|
76
|
5 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create processors |
|
80
|
|
|
* |
|
81
|
|
|
* @param object $source Data source |
|
82
|
|
|
* @param array|object $target Target or dot key path |
|
83
|
|
|
* @return Processor[] |
|
84
|
|
|
*/ |
|
85
|
2 |
|
protected function getProcessorsFor($source, $target) |
|
86
|
|
|
{ |
|
87
|
2 |
|
$processors = []; |
|
88
|
|
|
|
|
89
|
2 |
|
foreach ($this->processors as $processor) { |
|
90
|
2 |
|
$processors[] = $processor->withSourceAndTarget($source, $target); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
return $processors; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Apply processing instructions |
|
99
|
|
|
* |
|
100
|
|
|
* @param array|object|string $target Target or dot key path |
|
101
|
|
|
* @param object $source Data source |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function applyTo($target, $source = null) |
|
104
|
|
|
{ |
|
105
|
4 |
|
if (!isset($source)) { |
|
106
|
3 |
|
$source = $target; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
4 |
|
if (!is_object($source)) { |
|
110
|
2 |
|
throw new \Exception("Data enricher on works on an object, not on a " . gettype($source)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
if (is_string($target)) { |
|
114
|
|
|
$target = DotKey::on($source)->get($target); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
$nodes = $this->findNodes($target); |
|
118
|
2 |
|
$processors = $this->getProcessorsFor($source, $target); |
|
119
|
|
|
|
|
120
|
2 |
|
foreach ($nodes as $node) { |
|
121
|
2 |
|
foreach ($processors as $processor) { |
|
122
|
2 |
|
$node->apply($processor); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
$this->applyNodeResults($target); |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Find nodes that have processing instructions |
|
131
|
|
|
* |
|
132
|
|
|
* @param array|object $target |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function findNodes(&$target) |
|
136
|
|
|
{ |
|
137
|
2 |
|
$nodes = []; |
|
138
|
|
|
|
|
139
|
2 |
|
foreach ($target as $key => &$value) { |
|
140
|
2 |
|
if (is_array($value) || (is_object($value) && !$value instanceof Node)) { |
|
141
|
2 |
|
$nodes = array_merge($nodes, $this->findNodes($value)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) { |
|
145
|
2 |
|
$value = new Node($value); |
|
146
|
2 |
|
$nodes[] = $value; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
return $nodes; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Check if object has at leas one process property |
|
155
|
|
|
* |
|
156
|
|
|
* @param \stdClass $value |
|
157
|
|
|
* @param Processor[] $processors |
|
|
|
|
|
|
158
|
|
|
* @return boolean |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function hasProcessorProperty($value) |
|
161
|
|
|
{ |
|
162
|
2 |
|
$processorProps = array_map(function ($processor) { |
|
163
|
2 |
|
return $processor->getProperty(); |
|
164
|
2 |
|
}, $this->processors); |
|
165
|
|
|
|
|
166
|
2 |
|
$valueProps = array_keys(get_object_vars($value)); |
|
167
|
2 |
|
return count(array_intersect($valueProps, $processorProps)) > 0; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Replace nodes with their results |
|
172
|
|
|
* |
|
173
|
|
|
* @param array|object $target |
|
174
|
|
|
*/ |
|
175
|
2 |
View Code Duplication |
protected function applyNodeResults(&$target) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
2 |
|
foreach ($target as &$value) { |
|
178
|
2 |
|
if ($value instanceof Node) { |
|
179
|
2 |
|
$value = $value->getResult(); |
|
180
|
2 |
|
} elseif (is_array($value) || is_object($value)) { |
|
181
|
2 |
|
$this->applyNodeResults($value); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
2 |
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.