7use Wikimedia\Assert\Assert;
55 protected $errors = [];
64 public $successCount = 0;
67 public $failCount = 0;
86 public static function newFatal( $message, ...$parameters ): static {
87 $result = new static();
88 $result->fatal( $message, ...$parameters );
97 public static function newGood( $value =
null ): static {
98 $result = new static();
99 $result->value = $value;
115 $errorsOnlyStatusValue = static::newGood();
116 $warningsOnlyStatusValue = static::newGood();
117 $warningsOnlyStatusValue->setResult(
true, $this->getValue() );
118 $errorsOnlyStatusValue->setResult( $this->isOK(), $this->getValue() );
120 foreach ( $this->errors as $item ) {
121 if ( $item[
'type'] ===
'warning' ) {
122 $warningsOnlyStatusValue->errors[] = $item;
124 $errorsOnlyStatusValue->errors[] = $item;
128 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
138 return $this->ok && !$this->errors;
167 return $this->errors;
191 $this->ok = (bool)$ok;
192 $this->value = $value;
213 private function addError( array $newError ) {
214 [
'type' => $newType,
'message' => $newKey,
'params' => $newParams ] = $newError;
216 Assert::parameter( $newParams === [],
217 '$parameters',
"must be empty when using a MessageSpecifier" );
218 $newParams = $newKey->getParams();
219 $newKey = $newKey->getKey();
222 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' => $params ] ) {
225 $key = $key->getKey();
230 if ( $newKey === $key && $newParams == $params ) {
231 if ( $type ===
'warning' && $newType ===
'error' ) {
238 $this->errors[] = $newError;
252 public function warning( $message, ...$parameters ) {
253 return $this->addError( [
255 'message' => $message,
256 'params' => $parameters
270 public function error( $message, ...$parameters ) {
271 return $this->addError( [
273 'message' => $message,
274 '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 ) {
355 Assert::parameter( $type === null || $type ===
'warning' || $type ===
'error',
356 '$type',
"must be null, 'warning', or 'error'" );
358 foreach ( $this->errors as $error ) {
359 if ( $type ===
null || $error[
'type'] === $type ) {
360 [
'message' => $key,
'params' => $params ] = $error;
380 foreach ( $this->errors as [
'message' => $key ] ) {
399 foreach ( $this->errors as [
'message' => $key ] ) {
401 $key = $key->getKey();
403 if ( !in_array( $key, $messages,
true ) ) {
424 foreach ( $this->errors as [
'message' => &$message,
'params' => &$params ] ) {
447 $status = $this->isOK() ?
"OK" :
"Error";
448 if ( count( $this->errors ) ) {
449 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
451 $errorcount =
"no errors detected";
453 if ( $this->value !==
null ) {
454 $valstr = get_debug_type( $this->value ) .
" value set";
456 $valstr =
"no value set";
458 $out = sprintf(
"<%s, %s, %s>",
463 if ( count( $this->errors ) > 0 ) {
464 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
466 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' => $params ] ) {
468 $params = $key->getParams();
469 $key = $key->getKey();
472 $keyChunks = mb_str_split( $key, 25 );
473 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
476 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
477 as [ $typeChunk, $keyChunk, $paramsChunk ]
479 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
498 private function flattenParams( array $params,
string $joiner =
', ' ): string {
500 foreach ( $params as $p ) {
501 if ( is_array( $p ) ) {
502 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
504 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
511 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
513 return implode( $joiner, $ret );
527 foreach ( $this->getErrors() as $error ) {
528 if ( !$type || $error[
'type'] === $type ) {
530 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
532 $result[] = [ $error[
'message'], ...$error[
'params'] ];
Generic operation result class Has warning/error list, boolean status and arbitrary value.
hasMessagesExcept(string ... $messages)
Returns true if any other message than the specified ones 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.
splitByErrorType()
Splits this StatusValue object into two new StatusValue objects, one which contains only the error me...
setOK( $ok)
Change operation status.
hasMessage(string $message)
Returns true if the specified message is present as a warning or error.
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
replaceMessage(string $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
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.