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;
134 if ( $sv instanceof
static ) {
138 $result =
new static();
139 $result->ok = $sv->ok;
140 $result->errors = $sv->errors;
141 $result->value = $sv->value;
142 $result->successCount = $sv->successCount;
143 $result->failCount = $sv->failCount;
144 $result->success = $sv->success;
145 $result->statusData = $sv->statusData;
162 $errorsOnlyStatusValue = static::newGood();
163 $warningsOnlyStatusValue = static::newGood();
164 $warningsOnlyStatusValue->setResult(
true, $this->getValue() );
165 $errorsOnlyStatusValue->setResult( $this->isOK(), $this->getValue() );
167 foreach ( $this->errors as $item ) {
168 if ( $item[
'type'] ===
'warning' ) {
169 $warningsOnlyStatusValue->errors[] = $item;
171 $errorsOnlyStatusValue->errors[] = $item;
175 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
185 return $this->ok && !$this->errors;
214 return $this->errors;
238 $this->ok = (bool)$ok;
239 $this->value = $value;
260 private function addError( array $newError ) {
261 [
'type' => $newType,
'message' => $newKey,
'params' => $newParams ] = $newError;
263 Assert::parameter( $newParams === [],
264 '$parameters',
"must be empty when using a MessageSpecifier" );
265 $newParams = $newKey->getParams();
266 $newKey = $newKey->getKey();
269 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' => $params ] ) {
272 $key = $key->getKey();
277 if ( $newKey === $key && $newParams == $params ) {
278 if ( $type ===
'warning' && $newType ===
'error' ) {
285 $this->errors[] = $newError;
299 public function warning( $message, ...$parameters ) {
300 return $this->addError( [
302 'message' => $message,
303 'params' => $parameters
317 public function error( $message, ...$parameters ) {
318 return $this->addError( [
320 'message' => $message,
321 'params' => $parameters
335 public function fatal( $message, ...$parameters ) {
337 return $this->error( $message, ...$parameters );
347 public function merge( $other, $overwriteValue =
false ) {
348 if ( $this->statusData !==
null && $other->statusData !==
null ) {
349 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
351 $this->statusData ??= $other->statusData;
354 foreach ( $other->errors as $error ) {
355 $this->addError( $error );
357 $this->ok = $this->ok && $other->ok;
358 if ( $overwriteValue ) {
359 $this->value = $other->value;
361 $this->successCount += $other->successCount;
362 $this->failCount += $other->failCount;
381 foreach ( $this->errors as $error ) {
382 if ( $error[
'type'] === $type ) {
402 Assert::parameter( $type === null || $type ===
'warning' || $type ===
'error',
403 '$type',
"must be null, 'warning', or 'error'" );
405 foreach ( $this->errors as $error ) {
406 if ( $type ===
null || $error[
'type'] === $type ) {
407 [
'message' => $key,
'params' => $params ] = $error;
427 foreach ( $this->errors as [
'message' => $key ] ) {
446 foreach ( $this->errors as [
'message' => $key ] ) {
448 $key = $key->getKey();
450 if ( !in_array( $key, $messages,
true ) ) {
471 foreach ( $this->errors as [
'message' => &$message,
'params' => &$params ] ) {
494 $status = $this->isOK() ?
"OK" :
"Error";
495 if ( count( $this->errors ) ) {
496 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
498 $errorcount =
"no errors detected";
500 if ( $this->value !==
null ) {
501 $valstr = get_debug_type( $this->value ) .
" value set";
503 $valstr =
"no value set";
505 $out = sprintf(
"<%s, %s, %s>",
510 if ( count( $this->errors ) > 0 ) {
511 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
513 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' => $params ] ) {
515 $params = $key->getParams();
516 $key = $key->getKey();
519 $keyChunks = mb_str_split( $key, 25 );
520 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
523 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
524 as [ $typeChunk, $keyChunk, $paramsChunk ]
526 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
545 private function flattenParams( array $params,
string $joiner =
', ' ): string {
547 foreach ( $params as $p ) {
548 if ( is_array( $p ) ) {
549 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
551 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
558 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
560 return implode( $joiner, $ret );
574 foreach ( $this->getErrors() as $error ) {
575 if ( !$type || $error[
'type'] === $type ) {
577 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
579 $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.
static cast(StatusValue $sv)
Succinct helper method to wrap a StatusValue in some other specific subclass.
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.