MediaWiki REL1_37
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
159 public function setOK( $ok ) {
160 $this->ok = $ok;
161 return $this;
162 }
163
171 public function setResult( $ok, $value = null ) {
172 $this->ok = (bool)$ok;
173 $this->value = $value;
174 return $this;
175 }
176
193 private function addError( array $newError ) {
194 if ( $newError[ 'message' ] instanceof MessageSpecifier ) {
195 $isEqual = static function ( $existingError ) use ( $newError ) {
196 if ( $existingError['message'] instanceof MessageSpecifier ) {
197 // compare attributes of both MessageSpecifiers
198 return $newError['message'] == $existingError['message'];
199 } else {
200 return $newError['message']->getKey() === $existingError['message'] &&
201 $newError['message']->getParams() === $existingError['params'];
202 }
203 };
204 } else {
205 $isEqual = static function ( $existingError ) use ( $newError ) {
206 if ( $existingError['message'] instanceof MessageSpecifier ) {
207 return $newError['message'] === $existingError['message']->getKey() &&
208 $newError['params'] === $existingError['message']->getParams();
209 } else {
210 return $newError['message'] === $existingError['message'] &&
211 $newError['params'] === $existingError['params'];
212 }
213 };
214 }
215 foreach ( $this->errors as $index => $existingError ) {
216 if ( $isEqual( $existingError ) ) {
217 if ( $newError[ 'type' ] === 'error' && $existingError[ 'type' ] === 'warning' ) {
218 $this->errors[ $index ][ 'type' ] = 'error';
219 }
220 return $this;
221 }
222 }
223 $this->errors[] = $newError;
224 return $this;
225 }
226
234 public function warning( $message, ...$parameters ) {
235 return $this->addError( [
236 'type' => 'warning',
237 'message' => $message,
238 'params' => $parameters
239 ] );
240 }
241
250 public function error( $message, ...$parameters ) {
251 return $this->addError( [
252 'type' => 'error',
253 'message' => $message,
254 'params' => $parameters
255 ] );
256 }
257
266 public function fatal( $message, ...$parameters ) {
267 $this->ok = false;
268 return $this->error( $message, ...$parameters );
269 }
270
278 public function merge( $other, $overwriteValue = false ) {
279 foreach ( $other->errors as $error ) {
280 $this->addError( $error );
281 }
282 $this->ok = $this->ok && $other->ok;
283 if ( $overwriteValue ) {
284 $this->value = $other->value;
285 }
286 $this->successCount += $other->successCount;
287 $this->failCount += $other->failCount;
288 return $this;
289 }
290
301 public function getErrorsByType( $type ) {
302 $result = [];
303 foreach ( $this->errors as $error ) {
304 if ( $error['type'] === $type ) {
305 $result[] = $error;
306 }
307 }
308
309 return $result;
310 }
311
319 public function hasMessage( $message ) {
320 if ( $message instanceof MessageSpecifier ) {
321 $message = $message->getKey();
322 }
323 foreach ( $this->errors as $error ) {
324 if ( $error['message'] instanceof MessageSpecifier
325 && $error['message']->getKey() === $message
326 ) {
327 return true;
328 } elseif ( $error['message'] === $message ) {
329 return true;
330 }
331 }
332
333 return false;
334 }
335
347 public function replaceMessage( $source, $dest ) {
348 $replaced = false;
349
350 foreach ( $this->errors as $index => $error ) {
351 if ( $error['message'] === $source ) {
352 $this->errors[$index]['message'] = $dest;
353 $replaced = true;
354 }
355 }
356
357 return $replaced;
358 }
359
363 public function __toString() {
364 $status = $this->isOK() ? "OK" : "Error";
365 if ( count( $this->errors ) ) {
366 $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
367 } else {
368 $errorcount = "no errors detected";
369 }
370 if ( isset( $this->value ) ) {
371 $valstr = gettype( $this->value ) . " value set";
372 if ( is_object( $this->value ) ) {
373 $valstr .= "\"" . get_class( $this->value ) . "\" instance";
374 }
375 } else {
376 $valstr = "no value set";
377 }
378 $out = sprintf( "<%s, %s, %s>",
379 $status,
380 $errorcount,
381 $valstr
382 );
383 if ( count( $this->errors ) > 0 ) {
384 $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
385 $i = 1;
386 $out .= "\n";
387 $out .= $hdr;
388 foreach ( $this->errors as $error ) {
389 if ( $error['message'] instanceof MessageSpecifier ) {
390 $key = $error['message']->getKey();
391 $params = $error['message']->getParams();
392 } elseif ( $error['params'] ) {
393 $key = $error['message'];
394 $params = $error['params'];
395 } else {
396 $key = $error['message'];
397 $params = [];
398 }
399
400 $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
401 $i,
402 $key,
403 $this->flattenParams( $params )
404 );
405 $i++;
406 }
407 $out .= $hdr;
408 }
409
410 return $out;
411 }
412
417 private function flattenParams( array $params ): string {
418 $ret = [];
419 foreach ( $params as $p ) {
420 if ( is_array( $p ) ) {
421 $ret[] = '[ ' . self::flattenParams( $p ) . ' ]';
422 } elseif ( $p instanceof MessageSpecifier ) {
423 $ret[] = '{ ' . $p->getKey() . ': ' . self::flattenParams( $p->getParams() ) . ' }';
424 } else {
425 $ret[] = (string)$p;
426 }
427 }
428 return implode( ' ', $ret );
429 }
430
439 protected function getStatusArray( $type = false ) {
440 $result = [];
441
442 foreach ( $this->getErrors() as $error ) {
443 if ( $type === false || $error['type'] === $type ) {
444 if ( $error['message'] instanceof MessageSpecifier ) {
445 $result[] = array_merge(
446 [ $error['message']->getKey() ],
447 $error['message']->getParams()
448 );
449 } elseif ( $error['params'] ) {
450 $result[] = array_merge( [ $error['message'] ], $error['params'] );
451 } else {
452 $result[] = [ $error['message'] ];
453 }
454 }
455 }
456
457 return $result;
458 }
459}
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.
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
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.
addError(array $newError)
Add a new error to the error array ($this->errors) if that error is not already in the error array.
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.
$source