1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus\Models; |
4
|
|
|
|
5
|
|
|
class Cerberus |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private $references = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $score = 0; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $result = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* DetectModel constructor. |
24
|
|
|
* @param array $references |
25
|
|
|
* @param int $score |
26
|
|
|
* @param array $result |
27
|
|
|
*/ |
28
|
|
|
function __construct($references = [], $score = 0, $result = []) |
29
|
|
|
{ |
30
|
|
|
$this->addReferences($references); |
31
|
|
|
$this->score = $score; |
32
|
|
|
$this->result = $result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $references |
38
|
|
|
*/ |
39
|
|
|
private function addReferences($references) |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
foreach ($references as $reference) { |
43
|
|
|
|
44
|
|
|
$this->references[] = new References( |
45
|
|
|
isset($reference['handler']) ? $reference['handler'] : null, |
46
|
|
|
isset($reference['input']) ? $reference['input'] : null, |
47
|
|
|
isset($reference['output']) ? $reference['output'] : null, |
48
|
|
|
isset($reference['name']) ? $reference['name'] : null |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function toArray() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
'results' => [ |
61
|
|
|
'decoded' => $this->decodedResult(), |
62
|
|
|
'string' => $this->getResult(), |
63
|
|
|
], |
64
|
|
|
'references' => $this->getReferences(), |
65
|
|
|
'score' => $this->getScore() |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return false|string |
71
|
|
|
*/ |
72
|
|
|
public function getJsonLogs() |
73
|
|
|
{ |
74
|
|
|
return json_encode($this->getArrayLogs()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getArrayLogs() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
'score'=>$this->getScore(), |
84
|
|
|
'result'=>$this->result, |
85
|
|
|
'inputs'=>$this->getInputs(), |
86
|
|
|
'outputs'=>$this->getOutputs(), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function decodedResult() |
94
|
|
|
{ |
95
|
|
|
return $this->result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return false|string |
100
|
|
|
*/ |
101
|
|
|
public function getResult() |
102
|
|
|
{ |
103
|
|
|
return json_encode($this->result); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getReferences() |
110
|
|
|
{ |
111
|
|
|
return $this->references; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return integer |
116
|
|
|
*/ |
117
|
|
|
public function getScore() |
118
|
|
|
{ |
119
|
|
|
return $this->score; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getInputs() |
126
|
|
|
{ |
127
|
|
|
$inputs = []; |
128
|
|
|
foreach ($this->references as $key => $reference) { |
129
|
|
|
$inputs[$key] = $reference->getInput(); |
130
|
|
|
} |
131
|
|
|
return $inputs; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function getOutputs() |
138
|
|
|
{ |
139
|
|
|
$outputs = []; |
140
|
|
|
foreach ($this->references as $key => $reference) { |
141
|
|
|
$outputs[$key] = $reference->getOutput(); |
142
|
|
|
} |
143
|
|
|
return $outputs; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function hasScore() |
150
|
|
|
{ |
151
|
|
|
return ($this->getScore() > 0) ? true : false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |