25
25
26
26
namespace LibreNMS ;
27
27
28
+ use LibreNMS \Util \Html ;
29
+
28
30
class ValidationResult
29
31
{
30
- const FAILURE = 0 ;
31
- const WARNING = 1 ;
32
- const SUCCESS = 2 ;
33
- const INFO = 3 ;
32
+ public const FAILURE = 0 ;
33
+ public const WARNING = 1 ;
34
+ public const SUCCESS = 2 ;
35
+ public const INFO = 3 ;
34
36
37
+ /** @var string */
35
38
private $ message ;
39
+ /** @var int */
36
40
private $ status ;
41
+ /** @var string */
37
42
private $ list_description = '' ;
43
+ /** @var array */
38
44
private $ list ;
45
+ /** @var string|null */
39
46
private $ fix ;
40
47
41
48
/**
42
49
* ValidationResult constructor.
43
50
*
44
51
* @param string $message The message to describe this result
45
52
* @param int $status The status of this result FAILURE, WARNING, or SUCCESS
46
- * @param string $fix a suggested fix to highlight for the user
53
+ * @param string|null $fix a suggested fix to highlight for the user
47
54
*/
48
- public function __construct ($ message , $ status , $ fix = null )
55
+ public function __construct (string $ message , int $ status , string $ fix = null )
49
56
{
50
57
$ this ->message = $ message ;
51
58
$ this ->status = $ status ;
@@ -56,10 +63,10 @@ public function __construct($message, $status, $fix = null)
56
63
* Create a new ok Validation result
57
64
*
58
65
* @param string $message The message to describe this result
59
- * @param string $fix a suggested fix to highlight for the user
66
+ * @param string|null $fix a suggested fix to highlight for the user
60
67
* @return ValidationResult
61
68
*/
62
- public static function ok ($ message , $ fix = null )
69
+ public static function ok (string $ message , string $ fix = null ): ValidationResult
63
70
{
64
71
return new self ($ message , self ::SUCCESS , $ fix );
65
72
}
@@ -68,10 +75,10 @@ public static function ok($message, $fix = null)
68
75
* Create a new warning Validation result
69
76
*
70
77
* @param string $message The message to describe this result
71
- * @param string $fix a suggested fix to highlight for the user
78
+ * @param string|null $fix a suggested fix to highlight for the user
72
79
* @return ValidationResult
73
80
*/
74
- public static function warn ($ message , $ fix = null )
81
+ public static function warn (string $ message , string $ fix = null ): ValidationResult
75
82
{
76
83
return new self ($ message , self ::WARNING , $ fix );
77
84
}
@@ -82,7 +89,7 @@ public static function warn($message, $fix = null)
82
89
* @param string $message The message to describe this result
83
90
* @return ValidationResult
84
91
*/
85
- public static function info ($ message )
92
+ public static function info (string $ message ): ValidationResult
86
93
{
87
94
return new self ($ message , self ::INFO );
88
95
}
@@ -91,10 +98,10 @@ public static function info($message)
91
98
* Create a new failure Validation result
92
99
*
93
100
* @param string $message The message to describe this result
94
- * @param string $fix a suggested fix to highlight for the user
101
+ * @param string|null $fix a suggested fix to highlight for the user
95
102
* @return ValidationResult
96
103
*/
97
- public static function fail ($ message , $ fix = null )
104
+ public static function fail (string $ message , string $ fix = null ): ValidationResult
98
105
{
99
106
return new self ($ message , self ::FAILURE , $ fix );
100
107
}
@@ -105,27 +112,27 @@ public static function fail($message, $fix = null)
105
112
*
106
113
* @return int
107
114
*/
108
- public function getStatus ()
115
+ public function getStatus (): int
109
116
{
110
117
return $ this ->status ;
111
118
}
112
119
113
- public function getMessage ()
120
+ public function getMessage (): string
114
121
{
115
122
return $ this ->message ;
116
123
}
117
124
118
- public function hasList ()
125
+ public function hasList (): bool
119
126
{
120
127
return ! empty ($ this ->list );
121
128
}
122
129
123
- public function getList ()
130
+ public function getList (): ? array
124
131
{
125
132
return $ this ->list ;
126
133
}
127
134
128
- public function setList ($ description , array $ list )
135
+ public function setList (string $ description , array $ list ): ValidationResult
129
136
{
130
137
if (is_array (current ($ list ))) {
131
138
$ list = array_map (function ($ item ) {
@@ -139,11 +146,14 @@ public function setList($description, array $list)
139
146
return $ this ;
140
147
}
141
148
142
- public function hasFix ()
149
+ public function hasFix (): bool
143
150
{
144
151
return ! empty ($ this ->fix );
145
152
}
146
153
154
+ /**
155
+ * @return string|array|null
156
+ */
147
157
public function getFix ()
148
158
{
149
159
return $ this ->fix ;
@@ -156,7 +166,7 @@ public function getFix()
156
166
* @param string|array $fix
157
167
* @return ValidationResult $this
158
168
*/
159
- public function setFix ($ fix )
169
+ public function setFix ($ fix ): ValidationResult
160
170
{
161
171
$ this ->fix = $ fix ;
162
172
@@ -166,7 +176,7 @@ public function setFix($fix)
166
176
/**
167
177
* Print out this result to the console. Formatted nicely and with color.
168
178
*/
169
- public function consolePrint ()
179
+ public function consolePrint (): void
170
180
{
171
181
c_echo (str_pad ('[ ' . $ this ->getStatusText ($ this ->status ) . '] ' , 12 ) . $ this ->message . PHP_EOL );
172
182
@@ -188,7 +198,7 @@ public function consolePrint()
188
198
*
189
199
* @return string
190
200
*/
191
- public static function getStatusText ($ status )
201
+ public static function getStatusText (int $ status ): string
192
202
{
193
203
$ table = [
194
204
self ::SUCCESS => '%gOK%n ' ,
@@ -200,19 +210,35 @@ public static function getStatusText($status)
200
210
return $ table [$ status ] ?? 'Unknown ' ;
201
211
}
202
212
203
- public function getListDescription ()
213
+ public function getListDescription (): string
204
214
{
205
215
return $ this ->list_description ;
206
216
}
207
217
218
+ public function toArray (): array
219
+ {
220
+ $ resultStatus = $ this ->getStatus ();
221
+ $ resultFix = $ this ->getFix ();
222
+ $ resultList = $ this ->getList ();
223
+
224
+ return [
225
+ 'status ' => $ resultStatus ,
226
+ 'statusText ' => $ this ->getStatusText ($ resultStatus ),
227
+ 'message ' => $ this ->getMessage (),
228
+ 'fix ' => is_array ($ resultFix ) ? $ resultFix : ($ resultList ? [Html::linkify ($ resultFix )] : []),
229
+ 'listDescription ' => $ this ->getListDescription (),
230
+ 'list ' => is_array ($ resultList ) ? array_values ($ resultList ) : [],
231
+ ];
232
+ }
233
+
208
234
/**
209
235
* Print a list of items up to a max amount
210
236
* If over that number, a line will print the total items
211
237
*
212
238
* @param string $format format as consumed by printf()
213
239
* @param int $max the max amount of items to print, default 15
214
240
*/
215
- private function printList ($ format = "\t %s \n" , $ max = 15 )
241
+ private function printList (string $ format = "\t %s \n" , int $ max = 15 ): void
216
242
{
217
243
foreach (array_slice ($ this ->list , 0 , $ max ) as $ item ) {
218
244
printf ($ format , $ item );
0 commit comments