Passed
Push — master ( a9e4c3...f94168 )
by Steffen
02:00
created

DecisionTableBuilder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 233
rs 10
c 1
b 0
f 0
wmc 23

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getHitPolicy() 0 3 1
A addRule() 0 4 1
A setCollectOperator() 0 4 1
A getOutputs() 0 3 1
A getInstance() 0 3 1
A setInputs() 0 4 1
A addInput() 0 4 1
A build() 0 15 4
A setOutputs() 0 4 1
A getDefinitionKey() 0 3 1
A getCollectOperator() 0 3 1
A getRules() 0 3 1
A setRules() 0 4 1
A getInputs() 0 3 1
A setName() 0 4 1
A getName() 0 3 1
A __construct() 0 1 1
A addOutput() 0 4 1
A setHitPolicy() 0 4 1
A setDefinitionKey() 0 4 1
1
<?php
2
3
namespace SteffenBrand\DmnDecisionTables;
4
5
use SteffenBrand\DmnDecisionTables\Exception\DmnValidationException;
6
use SteffenBrand\DmnDecisionTables\Model\DecisionTable;
7
use SteffenBrand\DmnDecisionTables\Model\Input;
8
use SteffenBrand\DmnDecisionTables\Model\Output;
9
use SteffenBrand\DmnDecisionTables\Model\Rule;
10
use SteffenBrand\DmnDecisionTables\Validator\DecisionTableValidator;
11
12
/**
13
 * Class DecisionTableBuilder
14
 * @package SteffenBrand\DmnDecisionTables
15
 */
16
class DecisionTableBuilder implements DecisionTableBuilderInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @var string
25
     */
26
    private $definitionKey;
27
28
    /**
29
     * @var string
30
     */
31
    private $hitPolicy;
32
33
    /**
34
     * @var string
35
     */
36
    private $collectOperator;
37
38
    /**
39
     * @var Input[]
40
     */
41
    private $inputs;
42
43
    /**
44
     * @var Output[]
45
     */
46
    private $outputs;
47
48
    /**
49
     * @var Rule[]
50
     */
51
    private $rules;
52
53
    /**
54
     * DecisionTableBuilder constructor.
55
     */
56
    public function __construct() {}
57
58
    /**
59
     * Builds a DecisionTable instance.
60
     *
61
     * @param bool $validation
62
     * @param DecisionTableValidatorInterface
63
     * @return DecisionTable
64
     * @throws DmnValidationException
65
     */
66
    public function build($validation = true, $decisionTableValidator = null)
67
    {
68
        if (true === $validation) {
69
            $validator = $decisionTableValidator;
70
            if (null === $decisionTableValidator) {
71
                $validator = new DecisionTableValidator($this);
72
            }
73
74
            $validator->validate();
75
            if (false === $validator->isValid()) {
76
                throw new DmnValidationException($validator->getErrors());
77
            }
78
        }
79
80
        return new DecisionTable($this);
81
    }
82
83
    /**
84
     * Returns a DecisionTableBuilder instance.
85
     *
86
     * @return DecisionTableBuilder
87
     */
88
    public static function getInstance()
89
    {
90
        return new DecisionTableBuilder();
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getName()
97
    {
98
        return $this->name;
99
    }
100
101
    /**
102
     * @param string $name
103
     * @return DecisionTableBuilderInterface
104
     */
105
    public function setName($name)
106
    {
107
        $this->name = $name;
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getDefinitionKey()
115
    {
116
        return $this->definitionKey;
117
    }
118
119
    /**
120
     * @param string $definitionKey
121
     * @return DecisionTableBuilderInterface
122
     */
123
    public function setDefinitionKey($definitionKey)
124
    {
125
        $this->definitionKey = $definitionKey;
126
        return $this;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getHitPolicy()
133
    {
134
        return $this->hitPolicy;
135
    }
136
137
    /**
138
     * @param $hitPolicy
139
     * @return DecisionTableBuilderInterface
140
     */
141
    public function setHitPolicy($hitPolicy)
142
    {
143
        $this->hitPolicy = $hitPolicy;
144
        return $this;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getCollectOperator()
151
    {
152
        return $this->collectOperator;
153
    }
154
155
    /**
156
     * @param $collectOperator
157
     * @return DecisionTableBuilderInterface
158
     */
159
    public function setCollectOperator($collectOperator)
160
    {
161
        $this->collectOperator = $collectOperator;
162
        return $this;
163
    }
164
165
    /**
166
     * @return Input[]
167
     */
168
    public function getInputs()
169
    {
170
        return $this->inputs;
171
    }
172
173
    /**
174
     * @param Input[] $inputs
175
     * @return DecisionTableBuilderInterface
176
     */
177
    public function setInputs($inputs)
178
    {
179
        $this->inputs = $inputs;
180
        return $this;
181
    }
182
183
    /**
184
     * @param Input $input
185
     * @return DecisionTableBuilderInterface
186
     */
187
    public function addInput($input)
188
    {
189
        $this->inputs[] = $input;
190
        return $this;
191
    }
192
193
    /**
194
     * @return Output[]
195
     */
196
    public function getOutputs()
197
    {
198
        return $this->outputs;
199
    }
200
201
    /**
202
     * @param Output[] $outputs
203
     * @return DecisionTableBuilderInterface
204
     */
205
    public function setOutputs($outputs)
206
    {
207
        $this->outputs = $outputs;
208
        return $this;
209
    }
210
211
    /**
212
     * @param Output $output
213
     * @return DecisionTableBuilderInterface
214
     */
215
    public function addOutput($output)
216
    {
217
        $this->outputs[] = $output;
218
        return $this;
219
    }
220
221
    /**
222
     * @return Rule[]
223
     */
224
    public function getRules()
225
    {
226
        return $this->rules;
227
    }
228
229
    /**
230
     * @param Rule[] $rules
231
     * @return DecisionTableBuilderInterface
232
     */
233
    public function setRules($rules)
234
    {
235
        $this->rules = $rules;
236
        return $this;
237
    }
238
239
    /**
240
     * @param array $inputEntries
241
     * @param array $outputEntries
242
     * @param string|null $description
243
     * @return DecisionTableBuilderInterface
244
     */
245
    public function addRule($inputEntries, $outputEntries, $description = null)
246
    {
247
        $this->rules[] = new Rule($inputEntries, $outputEntries, $description);
248
        return $this;
249
    }
250
}