1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SteffenBrand\DmnDecisionTables\Model; |
4
|
|
|
|
5
|
|
|
use SteffenBrand\DmnDecisionTables\Constant\HitPolicy; |
6
|
|
|
use SteffenBrand\DmnDecisionTables\DecisionTableBuilder; |
7
|
|
|
use SteffenBrand\DmnDecisionTables\Exception\DmnConversionException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DecisionTable |
11
|
|
|
* @package SteffenBrand\DmnDecisionTables\Model |
12
|
|
|
*/ |
13
|
|
|
class DecisionTable implements DmnConvertibleInterface |
14
|
|
|
{ |
15
|
|
|
use ArrayToDmnTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $definitionKey; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $hitPolicy; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $collectOperator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Input[] |
39
|
|
|
*/ |
40
|
|
|
private $inputs; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Output[] |
44
|
|
|
*/ |
45
|
|
|
private $outputs; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Rule[] |
49
|
|
|
*/ |
50
|
|
|
private $rules; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* DecisionTable constructor. |
54
|
|
|
* @param DecisionTableBuilder $builder |
55
|
|
|
*/ |
56
|
|
|
public function __construct($builder) |
57
|
|
|
{ |
58
|
|
|
$this->name = $builder->getName(); |
59
|
|
|
$this->definitionKey = $builder->getDefinitionKey(); |
60
|
|
|
$this->hitPolicy = $builder->getHitPolicy(); |
61
|
|
|
$this->collectOperator = $builder->getCollectOperator(); |
62
|
|
|
$this->inputs = $builder->getInputs(); |
63
|
|
|
$this->outputs = $builder->getOutputs(); |
64
|
|
|
$this->rules = $builder->getRules(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns an XML representation of the decision table. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* @throws DmnConversionException |
72
|
|
|
*/ |
73
|
|
|
public function toDMN() |
74
|
|
|
{ |
75
|
|
|
$dom = $this->getDomDocument(); |
76
|
|
|
|
77
|
|
|
return $dom->saveXML(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Saves decision table as .DMN-file to filesystem. |
82
|
|
|
* |
83
|
|
|
* @param $fileNameAndPath |
84
|
|
|
* @return int |
85
|
|
|
* @throws DmnConversionException |
86
|
|
|
*/ |
87
|
|
|
public function saveFile($fileNameAndPath) |
88
|
|
|
{ |
89
|
|
|
$dom = $this->getDomDocument(); |
90
|
|
|
|
91
|
|
|
return $dom->save($fileNameAndPath); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a DOMDocument representation of the decision table. |
96
|
|
|
* |
97
|
|
|
* @return \DOMDocument |
98
|
|
|
* @throws DmnConversionException |
99
|
|
|
*/ |
100
|
|
|
private function getDomDocument() |
101
|
|
|
{ |
102
|
|
|
libxml_use_internal_errors(true); |
103
|
|
|
|
104
|
|
|
$dom = new \DOMDocument(); |
105
|
|
|
$dom->preserveWhiteSpace = false; |
106
|
|
|
$dom->formatOutput = true; |
107
|
|
|
|
108
|
|
|
$dom->loadXML( |
109
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>' . |
110
|
|
|
'<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn11.xsd" id="definitions" name="definitions" namespace="http://camunda.org/schema/1.0/dmn">' . |
111
|
|
|
'<decision id="' . $this->definitionKey . '" name="' . $this->name . '">' . |
112
|
|
|
'<decisionTable id="' . uniqid('decisionTable') . '" ' . $this->getHitPolicy() . '>' . |
113
|
|
|
$this->getDmnFromArray($this->inputs) . |
114
|
|
|
$this->getDmnFromArray($this->outputs) . |
115
|
|
|
$this->getDmnFromArray($this->rules) . |
116
|
|
|
'</decisionTable>' . |
117
|
|
|
'</decision>' . |
118
|
|
|
'</definitions>' |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
if (empty($errors = libxml_get_errors()) === false) { |
122
|
|
|
libxml_clear_errors(); |
123
|
|
|
throw new DmnConversionException($errors); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $dom; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns a hit policy xml string and adds aggregation in case of collect hit policy. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
private function getHitPolicy() |
135
|
|
|
{ |
136
|
|
|
$xml = 'hitPolicy="' . $this->hitPolicy . '"'; |
137
|
|
|
|
138
|
|
|
if (HitPolicy::COLLECT_POLICY === $this->hitPolicy) { |
139
|
|
|
$xml .= ' aggregation="' . $this->collectOperator . '"'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $xml; |
143
|
|
|
} |
144
|
|
|
} |