23use Wikimedia\Assert\Assert;
63 protected $errors = [];
72 public $successCount = 0;
75 public $failCount = 0;
87 public static function newFatal( $message, ...$parameters ) {
88 $result =
new static();
89 $result->fatal( $message, ...$parameters );
99 public static function newGood( $value =
null ) {
100 $result =
new static();
101 $result->value = $value;
117 $errorsOnlyStatusValue = static::newGood();
118 $warningsOnlyStatusValue = static::newGood();
119 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
120 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
122 foreach ( $this->errors as $item ) {
123 if ( $item[
'type'] ===
'warning' ) {
124 $warningsOnlyStatusValue->errors[] = $item;
126 $errorsOnlyStatusValue->errors[] = $item;
130 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
140 return $this->ok && !$this->errors;
169 return $this->errors;
191 $this->ok = (bool)$ok;
192 $this->value = $value;
213 private function addError( array $newError ) {
215 $isEqual =
static function ( $key,
$params ) use ( $newError ) {
218 return $newError[
'message'] == $key;
220 return $newError[
'message']->getKey() === $key &&
221 $newError[
'message']->getParams() ===
$params;
225 $isEqual =
static function ( $key,
$params ) use ( $newError ) {
228 $key = $key->getKey();
230 return $newError[
'message'] === $key && $newError[
'params'] ===
$params;
233 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' =>
$params ] ) {
234 if ( $isEqual( $key,
$params ) ) {
235 if ( $type ===
'warning' && $newError[
'type'] ===
'error' ) {
241 $this->errors[] = $newError;
252 public function warning( $message, ...$parameters ) {
253 $message = $this->normalizeMessage( $message );
255 return $this->addError( [
257 'message' => $message,
258 'params' => $parameters
270 public function error( $message, ...$parameters ) {
271 $message = $this->normalizeMessage( $message );
273 return $this->addError( [
275 'message' => $message,
276 'params' => $parameters
288 public function fatal( $message, ...$parameters ) {
290 return $this->
error( $message, ...$parameters );
300 public function merge( $other, $overwriteValue =
false ) {
301 if ( $this->statusData !==
null && $other->statusData !==
null ) {
302 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
304 $this->statusData ??= $other->statusData;
307 foreach ( $other->errors as $error ) {
308 $this->addError( $error );
310 $this->ok = $this->ok && $other->ok;
311 if ( $overwriteValue ) {
312 $this->value = $other->value;
314 $this->successCount += $other->successCount;
315 $this->failCount += $other->failCount;
334 foreach ( $this->errors as $error ) {
335 if ( $error[
'type'] === $type ) {
352 Assert::parameter( $type === null || $type ===
'warning' || $type ===
'error',
353 '$type',
"must be null, 'warning', or 'error'" );
355 foreach ( $this->errors as $error ) {
356 if ( $type ===
null || $error[
'type'] === $type ) {
357 [
'message' => $key,
'params' =>
$params ] = $error;
379 $message = $message->getKey();
382 foreach ( $this->errors as [
'message' => $key ] ) {
402 foreach ( $messages as $message ) {
404 $message = $message->getKey();
406 $exceptedKeys[] = $message;
409 foreach ( $this->errors as [
'message' => $key ] ) {
411 $key = $key->getKey();
413 if ( !in_array( $key, $exceptedKeys,
true ) ) {
436 $dest = $this->normalizeMessage( $dest );
438 foreach ( $this->errors as [
'message' => &$message ] ) {
457 $status = $this->isOK() ?
"OK" :
"Error";
458 if ( count( $this->errors ) ) {
459 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
461 $errorcount =
"no errors detected";
463 if ( isset( $this->value ) ) {
464 $valstr = gettype( $this->value ) .
" value set";
465 if ( is_object( $this->value ) ) {
466 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
469 $valstr =
"no value set";
471 $out = sprintf(
"<%s, %s, %s>",
476 if ( count( $this->errors ) > 0 ) {
477 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
479 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' =>
$params ] ) {
482 $key = $key->getKey();
485 $keyChunks = mb_str_split( $key, 25 );
486 $paramsChunks = mb_str_split( $this->flattenParams(
$params,
" | " ), 36 );
489 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
490 as [ $typeChunk, $keyChunk, $paramsChunk ]
492 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
511 private function flattenParams( array
$params,
string $joiner =
', ' ): string {
514 if ( is_array( $p ) ) {
515 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
517 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
522 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
524 return implode( $joiner, $ret );
539 foreach ( $this->getErrors() as $error ) {
540 if ( !$type || $error[
'type'] === $type ) {
542 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
544 $result[] = [ $error[
'message'], ...$error[
'params'] ];
557 private function normalizeMessage( $message ) {
560 return $converter->convertMessageValue( $message );
array $params
The job parameters.
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.
getErrors()
Get the list of errors.
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
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.
hasMessagesExcept(... $messages)
Returns true if any other message than the specified ones is present as a warning or error.
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.
merge( $other, $overwriteValue=false)
Merge another status object into this one.
mixed $statusData
arbitrary extra data about the operation
__toString()
Returns a string representation of the status for debugging.
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.
int $successCount
Counter for batch operations.
getKey()
Returns the message key.