52 protected $errors = [];
61 public $successCount = 0;
64 public $failCount = 0;
76 public static function newFatal( $message, ...$parameters ) {
77 $result =
new static();
78 $result->fatal( $message, ...$parameters );
88 public static function newGood( $value =
null ) {
89 $result =
new static();
90 $result->value = $value;
106 $errorsOnlyStatusValue = static::newGood();
107 $warningsOnlyStatusValue = static::newGood();
108 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
109 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
111 foreach ( $this->errors as $item ) {
112 if ( $item[
'type'] ===
'warning' ) {
113 $warningsOnlyStatusValue->errors[] = $item;
115 $errorsOnlyStatusValue->errors[] = $item;
119 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
129 return $this->ok && !$this->errors;
157 return $this->errors;
179 $this->ok = (bool)$ok;
180 $this->value = $value;
201 private function addError( array $newError ) {
203 $isEqual =
static function ( $existingError ) use ( $newError ) {
206 return $newError[
'message'] == $existingError[
'message'];
208 return $newError[
'message']->getKey() === $existingError[
'message'] &&
209 $newError[
'message']->getParams() === $existingError[
'params'];
213 $isEqual =
static function ( $existingError ) use ( $newError ) {
215 return $newError[
'message'] === $existingError[
'message']->getKey() &&
216 $newError[
'params'] === $existingError[
'message']->getParams();
218 return $newError[
'message'] === $existingError[
'message'] &&
219 $newError[
'params'] === $existingError[
'params'];
223 foreach ( $this->errors as $index => $existingError ) {
224 if ( $isEqual( $existingError ) ) {
225 if ( $newError[
'type' ] ===
'error' && $existingError[
'type' ] ===
'warning' ) {
226 $this->errors[ $index ][
'type' ] =
'error';
231 $this->errors[] = $newError;
242 public function warning( $message, ...$parameters ) {
243 $message = $this->normalizeMessage( $message );
245 return $this->addError( [
247 'message' => $message,
248 'params' => $parameters
260 public function error( $message, ...$parameters ) {
261 $message = $this->normalizeMessage( $message );
263 return $this->addError( [
265 'message' => $message,
266 'params' => $parameters
278 public function fatal( $message, ...$parameters ) {
280 return $this->
error( $message, ...$parameters );
290 public function merge( $other, $overwriteValue =
false ) {
291 if ( $this->statusData !==
null && $other->statusData !==
null ) {
292 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
294 $this->statusData ??= $other->statusData;
297 foreach ( $other->errors as $error ) {
298 $this->addError( $error );
300 $this->ok = $this->ok && $other->ok;
301 if ( $overwriteValue ) {
302 $this->value = $other->value;
304 $this->successCount += $other->successCount;
305 $this->failCount += $other->failCount;
323 foreach ( $this->errors as $error ) {
324 if ( $error[
'type'] ===
$type ) {
341 $message = $message->getKey();
343 $message = $message->getKey();
346 foreach ( $this->errors as $error ) {
348 && $error[
'message']->getKey() === $message
351 } elseif ( $error[
'message'] === $message ) {
368 foreach ( $messages as $message ) {
370 $message = $message->getKey();
372 $message = $message->getKey();
374 $exceptedKeys[] = $message;
377 foreach ( $this->errors as $error ) {
379 $actualKey = $error[
'message']->getKey();
381 $actualKey = $error[
'message'];
383 if ( !in_array( $actualKey, $exceptedKeys,
true ) ) {
406 $dest = $this->normalizeMessage( $dest );
408 foreach ( $this->errors as $index => $error ) {
409 if ( $error[
'message'] ===
$source ) {
410 $this->errors[$index][
'message'] = $dest;
413 && $error[
'message']->getKey() ===
$source ) {
414 $this->errors[$index][
'message'] = $dest;
429 $status = $this->
isOK() ?
"OK" :
"Error";
430 if ( count( $this->errors ) ) {
431 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
433 $errorcount =
"no errors detected";
435 if ( isset( $this->value ) ) {
436 $valstr = gettype( $this->value ) .
" value set";
437 if ( is_object( $this->value ) ) {
438 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
441 $valstr =
"no value set";
443 $out = sprintf(
"<%s, %s, %s>",
448 if ( count( $this->errors ) > 0 ) {
449 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
452 foreach ( $this->errors as $error ) {
454 $key = $error[
'message']->getKey();
455 $params = $error[
'message']->getParams();
456 } elseif ( $error[
'params'] ) {
457 $key = $error[
'message'];
458 $params = $error[
'params'];
460 $key = $error[
'message'];
464 $type = $error[
'type'];
465 $keyChunks = mb_str_split( $key, 25 );
466 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
469 foreach ( array_map(
null, [
$type ], $keyChunks, $paramsChunks )
470 as [ $typeChunk, $keyChunk, $paramsChunk ]
472 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
491 private function flattenParams( array $params,
string $joiner =
', ' ): string {
493 foreach ( $params as $p ) {
494 if ( is_array( $p ) ) {
495 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
497 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
502 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
504 return implode( $joiner, $ret );
518 foreach ( $this->getErrors() as $error ) {
519 if (
$type ===
false || $error[
'type'] ===
$type ) {
521 $result[] = array_merge(
522 [ $error[
'message']->getKey() ],
523 $error[
'message']->getParams()
525 } elseif ( $error[
'params'] ) {
526 $result[] = array_merge( [ $error[
'message'] ], $error[
'params'] );
528 $result[] = [ $error[
'message'] ];
541 private function normalizeMessage( $message ) {
544 return $converter->convertMessageValue( $message );
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.
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.