7use Wikimedia\Assert\Assert;
8use Wikimedia\JsonCodec\Hint;
9use Wikimedia\JsonCodec\HintType;
10use Wikimedia\JsonCodec\JsonCodecable;
11use Wikimedia\JsonCodec\JsonCodecableTrait;
46 use JsonCodecableTrait;
91 public static function newFatal( $message, ...$parameters ): static {
92 $result = new static();
93 $result->fatal( $message, ...$parameters );
103 $result = new static();
139 if ( $sv instanceof
static ) {
143 $result =
new static();
144 $result->ok = $sv->ok;
145 $result->errors = $sv->errors;
146 $result->value = $sv->value;
147 $result->successCount = $sv->successCount;
148 $result->failCount = $sv->failCount;
149 $result->success = $sv->success;
150 $result->statusData = $sv->statusData;
167 $errorsOnlyStatusValue = static::newGood();
168 $warningsOnlyStatusValue = static::newGood();
169 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
170 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
172 foreach ( $this->errors as $item ) {
173 if ( $item[
'type'] ===
'warning' ) {
174 $warningsOnlyStatusValue->errors[] = $item;
176 $errorsOnlyStatusValue->errors[] = $item;
180 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
190 return $this->ok && !$this->errors;
219 return $this->errors;
243 $this->ok = (bool)
$ok;
265 private function addError( array $newError ) {
266 [
'type' => $newType,
'message' => $newKey,
'params' => $newParams ] = $newError;
267 if ( $newKey instanceof MessageSpecifier ) {
268 Assert::parameter( $newParams === [],
269 '$parameters',
"must be empty when using a MessageSpecifier" );
270 $newParams = $newKey->getParams();
271 $newKey = $newKey->getKey();
274 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' => $params ] ) {
275 if ( $key instanceof MessageSpecifier ) {
277 $key = $key->getKey();
282 if ( $newKey === $key && $newParams == $params ) {
283 if ( $type ===
'warning' && $newType ===
'error' ) {
290 $this->errors[] = $newError;
304 public function warning( $message, ...$parameters ) {
305 return $this->addError( [
307 'message' => $message,
308 'params' => $parameters
322 public function error( $message, ...$parameters ) {
323 return $this->addError( [
325 'message' => $message,
326 'params' => $parameters
340 public function fatal( $message, ...$parameters ) {
342 return $this->
error( $message, ...$parameters );
352 public function merge( $other, $overwriteValue =
false ) {
353 if ( $this->statusData !==
null &&
354 $other->statusData !==
null &&
356 $this->statusData !== $other->statusData
358 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
360 $this->statusData ??= $other->statusData;
363 foreach ( $other->errors as $error ) {
364 $this->addError( $error );
366 $this->ok = $this->ok && $other->ok;
367 if ( $overwriteValue ) {
368 $this->value = $other->value;
370 $this->successCount += $other->successCount;
371 $this->failCount += $other->failCount;
390 foreach ( $this->errors as $error ) {
391 if ( $error[
'type'] === $type ) {
411 Assert::parameter( $type === null || $type ===
'warning' || $type ===
'error',
412 '$type',
"must be null, 'warning', or 'error'" );
414 foreach ( $this->errors as $error ) {
415 if ( $type ===
null || $error[
'type'] === $type ) {
416 [
'message' => $key,
'params' => $params ] = $error;
436 foreach ( $this->errors as [
'message' => $key ] ) {
455 foreach ( $this->errors as [
'message' => $key ] ) {
457 $key = $key->getKey();
459 if ( !in_array( $key, $messages,
true ) ) {
480 foreach ( $this->errors as [
'message' => &$message,
'params' => &$params ] ) {
503 $status = $this->
isOK() ?
"OK" :
"Error";
504 if ( count( $this->errors ) ) {
505 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
507 $errorcount =
"no errors detected";
509 if ( $this->value !==
null ) {
510 $valstr = get_debug_type( $this->value ) .
" value set";
512 $valstr =
"no value set";
514 $out = sprintf(
"<%s, %s, %s>",
519 if ( count( $this->errors ) > 0 ) {
520 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
522 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' => $params ] ) {
524 $params = $key->getParams();
525 $key = $key->getKey();
528 $keyChunks = mb_str_split( $key, 25 );
529 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
532 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
533 as [ $typeChunk, $keyChunk, $paramsChunk ]
535 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
554 private function flattenParams( array $params,
string $joiner =
', ' ): string {
556 foreach ( $params as $p ) {
557 if ( is_array( $p ) ) {
558 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
559 } elseif ( $p instanceof MessageSpecifier ) {
560 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
561 } elseif ( $p instanceof MessageParam ) {
567 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
569 return implode( $joiner, $ret );
583 foreach ( $this->
getErrors() as $error ) {
584 if ( !$type || $error[
'type'] === $type ) {
586 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
588 $result[] = [ $error[
'message'], ...$error[
'params'] ];
602 foreach ( $this->errors as $error ) {
604 $errorData = [
'message' => $error[
'message'] ];
609 $errorData = [
'message' => $error[
'message'] ];
610 if ( $error[
'params'] ) {
611 $errorData[
'params'] = $error[
'params'];
614 if ( $error[
'type'] !==
'error' ) {
615 $errorData[
'type'] = $error[
'type'];
619 $data = [
'ok' => $this->ok ];
621 $data[
'errors'] = $errors;
623 if ( $this->value !==
null ) {
624 $data[
'value'] = $this->value;
626 if ( $this->success ) {
629 if ( $this->successCount ) {
630 $data[
'successCount'] = $this->successCount;
632 if ( $this->failCount ) {
633 $data[
'failCount'] = $this->failCount;
635 if ( $this->statusData !==
null ) {
636 $data[
'statusData'] = $this->statusData;
643 $status =
new static;
644 $status->ok = $json[
'ok'];
646 foreach ( $json[
'errors'] ?? [] as $error ) {
647 $status->errors[] = [
648 'type' => $error[
'type'] ??
'error',
649 'message' => $error[
'message'],
650 'params' => $error[
'params'] ?? [],
653 $status->value = $json[
'value'] ??
null;
654 $status->success = $json[
'success'] ?? [];
655 $status->successCount = $json[
'successCount'] ?? 0;
656 $status->failCount = $json[
'failCount'] ?? 0;
657 $status->statusData = $json[
'statusData'] ??
null;
663 return match ( $keyName ) {
665 'errors' => Hint::build(
'array', HintType::LIST ),
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.
static newFromJsonArray(array $json)
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.
toJsonArray()
JSON serializing a StatusValue is possible only if the value and statusData are JSON-serializable,...
bool[] $success
Map of (key => bool) to indicate success of each part of batch operations.
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.
static jsonClassHintFor(string $keyName)
int $successCount
Counter for batch operations.