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;
156 return $this->errors;
178 $this->ok = (bool)$ok;
179 $this->value = $value;
199 private function addError( array $newError ) {
201 $isEqual =
static function ( $existingError ) use ( $newError ) {
204 return $newError[
'message'] == $existingError[
'message'];
206 return $newError[
'message']->getKey() === $existingError[
'message'] &&
207 $newError[
'message']->getParams() === $existingError[
'params'];
211 $isEqual =
static function ( $existingError ) use ( $newError ) {
213 return $newError[
'message'] === $existingError[
'message']->getKey() &&
214 $newError[
'params'] === $existingError[
'message']->getParams();
216 return $newError[
'message'] === $existingError[
'message'] &&
217 $newError[
'params'] === $existingError[
'params'];
221 foreach ( $this->errors as $index => $existingError ) {
222 if ( $isEqual( $existingError ) ) {
223 if ( $newError[
'type' ] ===
'error' && $existingError[
'type' ] ===
'warning' ) {
224 $this->errors[ $index ][
'type' ] =
'error';
229 $this->errors[] = $newError;
240 public function warning( $message, ...$parameters ) {
241 $message = $this->normalizeMessage( $message, $parameters );
243 return $this->addError( [
245 'message' => $message,
246 'params' => $parameters
258 public function error( $message, ...$parameters ) {
259 $message = $this->normalizeMessage( $message, $parameters );
261 return $this->addError( [
263 'message' => $message,
264 'params' => $parameters
276 public function fatal( $message, ...$parameters ) {
278 return $this->
error( $message, ...$parameters );
288 public function merge( $other, $overwriteValue =
false ) {
289 if ( $this->statusData !==
null && $other->statusData !==
null ) {
290 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
292 $this->statusData ??= $other->statusData;
295 foreach ( $other->errors as $error ) {
296 $this->addError( $error );
298 $this->ok = $this->ok && $other->ok;
299 if ( $overwriteValue ) {
300 $this->value = $other->value;
302 $this->successCount += $other->successCount;
303 $this->failCount += $other->failCount;
320 foreach ( $this->errors as $error ) {
321 if ( $error[
'type'] ===
$type ) {
338 $message = $message->getKey();
340 $message = $message->getKey();
343 foreach ( $this->errors as $error ) {
345 && $error[
'message']->getKey() === $message
348 } elseif ( $error[
'message'] === $message ) {
371 $dest = $this->normalizeMessage( $dest );
373 foreach ( $this->errors as $index => $error ) {
374 if ( $error[
'message'] ===
$source ) {
375 $this->errors[$index][
'message'] = $dest;
378 && $error[
'message']->getKey() ===
$source ) {
379 $this->errors[$index][
'message'] = $dest;
394 $status = $this->
isOK() ?
"OK" :
"Error";
395 if ( count( $this->errors ) ) {
396 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
398 $errorcount =
"no errors detected";
400 if ( isset( $this->value ) ) {
401 $valstr = gettype( $this->value ) .
" value set";
402 if ( is_object( $this->value ) ) {
403 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
406 $valstr =
"no value set";
408 $out = sprintf(
"<%s, %s, %s>",
413 if ( count( $this->errors ) > 0 ) {
414 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
418 foreach ( $this->errors as $error ) {
420 $key = $error[
'message']->getKey();
421 $params = $error[
'message']->getParams();
422 } elseif ( $error[
'params'] ) {
423 $key = $error[
'message'];
424 $params = $error[
'params'];
426 $key = $error[
'message'];
430 $type = $error[
'type'];
431 $keyChunks = mb_str_split( $key, 25 );
432 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
435 foreach ( array_map(
null, [
$type ], $keyChunks, $paramsChunks )
436 as [ $typeChunk, $keyChunk, $paramsChunk ]
438 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
459 private function flattenParams( array $params,
string $joiner =
', ' ): string {
461 foreach ( $params as $p ) {
462 if ( is_array( $p ) ) {
463 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
465 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
470 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
472 return implode( $joiner, $ret );
486 foreach ( $this->getErrors() as $error ) {
487 if (
$type ===
false || $error[
'type'] ===
$type ) {
489 $result[] = array_merge(
490 [ $error[
'message']->getKey() ],
491 $error[
'message']->getParams()
493 } elseif ( $error[
'params'] ) {
494 $result[] = array_merge( [ $error[
'message'] ], $error[
'params'] );
496 $result[] = [ $error[
'message'] ];
510 private function normalizeMessage( $message, array $parameters = [] ) {
513 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.
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.