1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LegalThings\DataEnricher; |
4
|
|
|
|
5
|
|
|
use LegalThings\DataEnricher\Processor; |
6
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* An object with processing instructions |
10
|
|
|
*/ |
11
|
|
|
class Node extends \stdClass |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The processed data |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $i_result; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class constructor |
21
|
|
|
* |
22
|
|
|
* @param \stdClass $data |
23
|
|
|
*/ |
24
|
|
|
public function __construct(\stdClass $data) |
25
|
|
|
{ |
26
|
|
|
$this->i_result = $data; |
27
|
|
|
|
28
|
|
|
foreach ($data as $key => $value) { |
|
|
|
|
29
|
|
|
if ($key === 'i_result') { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->$key = $value; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Replace nodes with their results |
40
|
|
|
* |
41
|
|
|
* @param array|object $target |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
protected function applyNodeResults(&$target) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if (!is_array($target) && !is_object($target)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
foreach ($target as &$value) { |
50
|
|
|
if ($value instanceof self) { |
51
|
|
|
$value = $value->getResult(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->applyNodeResults($value); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the processed result |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function getResult() |
64
|
|
|
{ |
65
|
|
|
if ($this->i_result instanceof PromiseInterface) { |
66
|
|
|
$result = $this->i_result->wait(); |
67
|
|
|
} else { |
68
|
|
|
$result = $this->i_result; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->applyNodeResults($result); |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the result after processing |
77
|
|
|
* |
78
|
|
|
* @param mixed $result |
79
|
|
|
*/ |
80
|
|
|
public function setResult($result) |
81
|
|
|
{ |
82
|
|
|
$this->i_result = $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test if the node has an instruction for a processor |
88
|
|
|
* |
89
|
|
|
* @param Processor $processor |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
|
|
public function hasInstruction(Processor $processor) |
93
|
|
|
{ |
94
|
|
|
$prop = $processor->getProperty(); |
95
|
|
|
return isset($this->$prop); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get an instruction for a processor |
100
|
|
|
* |
101
|
|
|
* @param Processor $processor |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function getInstruction(Processor $processor) |
105
|
|
|
{ |
106
|
|
|
$prop = $processor->getProperty(); |
107
|
|
|
|
108
|
|
|
if (!isset($this->$prop)) { |
109
|
|
|
throw new \LogicException("Node doesn't have instruction property '$prop'"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$value = $this->$prop; |
113
|
|
|
|
114
|
|
|
if ($value instanceof self) { |
115
|
|
|
$value = $value->getResult(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Apply processing to this node |
123
|
|
|
* |
124
|
|
|
* @param Processor $processor |
125
|
|
|
*/ |
126
|
|
|
public function apply(Processor $processor) |
127
|
|
|
{ |
128
|
|
|
if (!$this->hasInstruction($processor)) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($this->i_result instanceof PromiseInterface) { |
133
|
|
|
$this->i_result->then(function() use ($processor) { |
|
|
|
|
134
|
|
|
$processor->applyToNode($this); |
135
|
|
|
}); |
136
|
|
|
} else { |
137
|
|
|
$processor->applyToNode($this); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|