61 protected $errors = [];
70 public $successCount = 0;
73 public $failCount = 0;
85 public static function newFatal( $message, ...$parameters ) {
86 $result =
new static();
87 $result->fatal( $message, ...$parameters );
97 public static function newGood( $value =
null ) {
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;
166 return $this->errors;
188 $this->ok = (bool)$ok;
189 $this->value = $value;
210 private function addError( array $newError ) {
212 $isEqual =
static function ( $key,
$params ) use ( $newError ) {
215 return $newError[
'message'] == $key;
217 return $newError[
'message']->getKey() === $key &&
218 $newError[
'message']->getParams() ===
$params;
222 $isEqual =
static function ( $key,
$params ) use ( $newError ) {
225 $key = $key->getKey();
227 return $newError[
'message'] === $key && $newError[
'params'] ===
$params;
230 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' =>
$params ] ) {
231 if ( $isEqual( $key,
$params ) ) {
232 if ( $type ===
'warning' && $newError[
'type'] ===
'error' ) {
238 $this->errors[] = $newError;
249 public function warning( $message, ...$parameters ) {
250 $message = $this->normalizeMessage( $message );
252 return $this->addError( [
254 'message' => $message,
255 'params' => $parameters
267 public function error( $message, ...$parameters ) {
268 $message = $this->normalizeMessage( $message );
270 return $this->addError( [
272 'message' => $message,
273 'params' => $parameters
285 public function fatal( $message, ...$parameters ) {
287 return $this->
error( $message, ...$parameters );
297 public function merge( $other, $overwriteValue =
false ) {
298 if ( $this->statusData !==
null && $other->statusData !==
null ) {
299 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
301 $this->statusData ??= $other->statusData;
304 foreach ( $other->errors as $error ) {
305 $this->addError( $error );
307 $this->ok = $this->ok && $other->ok;
308 if ( $overwriteValue ) {
309 $this->value = $other->value;
311 $this->successCount += $other->successCount;
312 $this->failCount += $other->failCount;
330 foreach ( $this->errors as $error ) {
331 if ( $error[
'type'] === $type ) {
348 $message = $message->getKey();
351 foreach ( $this->errors as [
'message' => $key ] ) {
371 foreach ( $messages as $message ) {
373 $message = $message->getKey();
375 $exceptedKeys[] = $message;
378 foreach ( $this->errors as [
'message' => $key ] ) {
380 $key = $key->getKey();
382 if ( !in_array( $key, $exceptedKeys,
true ) ) {
405 $dest = $this->normalizeMessage( $dest );
407 foreach ( $this->errors as [
'message' => &$message ] ) {
426 $status = $this->
isOK() ?
"OK" :
"Error";
427 if ( count( $this->errors ) ) {
428 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
430 $errorcount =
"no errors detected";
432 if ( isset( $this->value ) ) {
433 $valstr = gettype( $this->value ) .
" value set";
434 if ( is_object( $this->value ) ) {
435 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
438 $valstr =
"no value set";
440 $out = sprintf(
"<%s, %s, %s>",
445 if ( count( $this->errors ) > 0 ) {
446 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
448 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' =>
$params ] ) {
451 $key = $key->getKey();
454 $keyChunks = mb_str_split( $key, 25 );
455 $paramsChunks = mb_str_split( $this->flattenParams(
$params,
" | " ), 36 );
458 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
459 as [ $typeChunk, $keyChunk, $paramsChunk ]
461 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
480 private function flattenParams( array
$params,
string $joiner =
', ' ): string {
483 if ( is_array( $p ) ) {
484 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
486 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
491 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
493 return implode( $joiner, $ret );
508 foreach ( $this->getErrors() as $error ) {
509 if ( !$type || $error[
'type'] === $type ) {
511 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
513 $result[] = [ $error[
'message'], ...$error[
'params'] ];
526 private function normalizeMessage( $message ) {
529 return $converter->convertMessageValue( $message );
array $params
The job parameters.
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.
getKey()
Returns the message key.