MediaWiki REL1_35
StatusValue.php
Go to the documentation of this file.
1<?php
44
46 protected $ok = true;
47
49 protected $errors = [];
50
52 public $value;
53
55 public $success = [];
56
58 public $successCount = 0;
59
61 public $failCount = 0;
62
70 public static function newFatal( $message, ...$parameters ) {
71 $result = new static();
72 $result->fatal( $message, ...$parameters );
73 return $result;
74 }
75
82 public static function newGood( $value = null ) {
83 $result = new static();
84 $result->value = $value;
85 return $result;
86 }
87
99 public function splitByErrorType() {
100 $errorsOnlyStatusValue = static::newGood();
101 $warningsOnlyStatusValue = static::newGood();
102 $warningsOnlyStatusValue->setResult( true, $this->getValue() );
103 $errorsOnlyStatusValue->setResult( $this->isOK(), $this->getValue() );
104
105 foreach ( $this->errors as $item ) {
106 if ( $item['type'] === 'warning' ) {
107 $warningsOnlyStatusValue->errors[] = $item;
108 } else {
109 $errorsOnlyStatusValue->errors[] = $item;
110 }
111 }
112
113 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
114 }
115
122 public function isGood() {
123 return $this->ok && !$this->errors;
124 }
125
131 public function isOK() {
132 return $this->ok;
133 }
134
138 public function getValue() {
139 return $this->value;
140 }
141
149 public function getErrors() {
150 return $this->errors;
151 }
152
158 public function setOK( $ok ) {
159 $this->ok = $ok;
160 }
161
168 public function setResult( $ok, $value = null ) {
169 $this->ok = (bool)$ok;
170 $this->value = $value;
171 }
172
179 public function warning( $message, ...$parameters ) {
180 $this->errors[] = [
181 'type' => 'warning',
182 'message' => $message,
183 'params' => $parameters
184 ];
185 }
186
194 public function error( $message, ...$parameters ) {
195 $this->errors[] = [
196 'type' => 'error',
197 'message' => $message,
198 'params' => $parameters
199 ];
200 }
201
209 public function fatal( $message, ...$parameters ) {
210 $this->errors[] = [
211 'type' => 'error',
212 'message' => $message,
213 'params' => $parameters
214 ];
215 $this->ok = false;
216 }
217
224 public function merge( $other, $overwriteValue = false ) {
225 $this->errors = array_merge( $this->errors, $other->errors );
226 $this->ok = $this->ok && $other->ok;
227 if ( $overwriteValue ) {
228 $this->value = $other->value;
229 }
230 $this->successCount += $other->successCount;
231 $this->failCount += $other->failCount;
232 }
233
244 public function getErrorsByType( $type ) {
245 $result = [];
246 foreach ( $this->errors as $error ) {
247 if ( $error['type'] === $type ) {
248 $result[] = $error;
249 }
250 }
251
252 return $result;
253 }
254
262 public function hasMessage( $message ) {
263 if ( $message instanceof MessageSpecifier ) {
264 $message = $message->getKey();
265 }
266 foreach ( $this->errors as $error ) {
267 if ( $error['message'] instanceof MessageSpecifier
268 && $error['message']->getKey() === $message
269 ) {
270 return true;
271 } elseif ( $error['message'] === $message ) {
272 return true;
273 }
274 }
275
276 return false;
277 }
278
290 public function replaceMessage( $source, $dest ) {
291 $replaced = false;
292
293 foreach ( $this->errors as $index => $error ) {
294 if ( $error['message'] === $source ) {
295 $this->errors[$index]['message'] = $dest;
296 $replaced = true;
297 }
298 }
299
300 return $replaced;
301 }
302
306 public function __toString() {
307 $status = $this->isOK() ? "OK" : "Error";
308 if ( count( $this->errors ) ) {
309 $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
310 } else {
311 $errorcount = "no errors detected";
312 }
313 if ( isset( $this->value ) ) {
314 $valstr = gettype( $this->value ) . " value set";
315 if ( is_object( $this->value ) ) {
316 $valstr .= "\"" . get_class( $this->value ) . "\" instance";
317 }
318 } else {
319 $valstr = "no value set";
320 }
321 $out = sprintf( "<%s, %s, %s>",
322 $status,
323 $errorcount,
324 $valstr
325 );
326 if ( count( $this->errors ) > 0 ) {
327 $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
328 $i = 1;
329 $out .= "\n";
330 $out .= $hdr;
331 foreach ( $this->errors as $error ) {
332 if ( $error['message'] instanceof MessageSpecifier ) {
333 $key = $error['message']->getKey();
334 $params = $error['message']->getParams();
335 } elseif ( $error['params'] ) {
336 $key = $error['message'];
337 $params = $error['params'];
338 } else {
339 $key = $error['message'];
340 $params = [];
341 }
342
343 $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
344 $i,
345 $key,
346 self::flattenParams( $params )
347 );
348 $i += 1;
349 }
350 $out .= $hdr;
351 }
352
353 return $out;
354 }
355
360 private function flattenParams( array $params ) : string {
361 $ret = [];
362 foreach ( $params as $p ) {
363 if ( is_array( $p ) ) {
364 $ret[] = '[ ' . self::flattenParams( $p ) . ' ]';
365 } elseif ( $p instanceof MessageSpecifier ) {
366 $ret[] = '{ ' . $p->getKey() . ': ' . self::flattenParams( $p->getParams() ) . ' }';
367 } else {
368 $ret[] = (string)$p;
369 }
370 }
371 return implode( ' ', $ret );
372 }
373}
Generic operation result class Has warning/error list, boolean status and arbitrary value.
hasMessage( $message)
Returns true if the specified message is present as a warning or error.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
int $failCount
Counter for batch operations.
array[] $errors
getErrors()
Get the list of errors.
replaceMessage( $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
splitByErrorType()
Splits this StatusValue object into two new StatusValue objects, one which contains only the error me...
setOK( $ok)
Change operation status.
isOK()
Returns whether the operation completed.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
setResult( $ok, $value=null)
Change operation result.
merge( $other, $overwriteValue=false)
Merge another status object into this one.
error( $message,... $parameters)
Add an error, do not set fatal flag This can be used for non-fatal errors.
getErrorsByType( $type)
Returns a list of status messages of the given type.
warning( $message,... $parameters)
Add a new warning.
isGood()
Returns whether the operation completed and didn't have any error or warnings.
static newGood( $value=null)
Factory function for good results.
flattenParams(array $params)
int $successCount
Counter for batch operations.
Stable for implementing.
$source