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 ( $key, $params ) use ( $newError ) {
206 return $newError[
'message'] == $key;
208 return $newError[
'message']->getKey() === $key &&
209 $newError[
'message']->getParams() === $params;
213 $isEqual =
static function ( $key, $params ) use ( $newError ) {
215 $params = $key->getParams();
216 $key = $key->getKey();
218 return $newError[
'message'] === $key && $newError[
'params'] === $params;
221 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' => $params ] ) {
222 if ( $isEqual( $key, $params ) ) {
223 if ( $type ===
'warning' && $newError[
'type'] ===
'error' ) {
229 $this->errors[] = $newError;
240 public function warning( $message, ...$parameters ) {
241 $message = $this->normalizeMessage( $message );
243 return $this->addError( [
245 'message' => $message,
246 'params' => $parameters
258 public function error( $message, ...$parameters ) {
259 $message = $this->normalizeMessage( $message );
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;
321 foreach ( $this->errors as $error ) {
322 if ( $error[
'type'] === $type ) {
339 $message = $message->getKey();
342 foreach ( $this->errors as [
'message' => $key ] ) {
362 foreach ( $messages as $message ) {
364 $message = $message->getKey();
366 $exceptedKeys[] = $message;
369 foreach ( $this->errors as [
'message' => $key ] ) {
371 $key = $key->getKey();
373 if ( !in_array( $key, $exceptedKeys,
true ) ) {
396 $dest = $this->normalizeMessage( $dest );
398 foreach ( $this->errors as [
'message' => &$message ] ) {
417 $status = $this->
isOK() ?
"OK" :
"Error";
418 if ( count( $this->errors ) ) {
419 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
421 $errorcount =
"no errors detected";
423 if ( isset( $this->value ) ) {
424 $valstr = gettype( $this->value ) .
" value set";
425 if ( is_object( $this->value ) ) {
426 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
429 $valstr =
"no value set";
431 $out = sprintf(
"<%s, %s, %s>",
436 if ( count( $this->errors ) > 0 ) {
437 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
439 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' => $params ] ) {
441 $params = $key->getParams();
442 $key = $key->getKey();
445 $keyChunks = mb_str_split( $key, 25 );
446 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
449 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
450 as [ $typeChunk, $keyChunk, $paramsChunk ]
452 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
471 private function flattenParams( array $params,
string $joiner =
', ' ): string {
473 foreach ( $params as $p ) {
474 if ( is_array( $p ) ) {
475 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
477 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
482 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
484 return implode( $joiner, $ret );
498 foreach ( $this->getErrors() as $error ) {
499 if ( !$type || $error[
'type'] === $type ) {
501 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
503 $result[] = [ $error[
'message'], ...$error[
'params'] ];
516 private function normalizeMessage( $message ) {
519 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.
getKey()
Returns the message key.