73 public static function newFatal( $message, ...$parameters ) {
74 $result =
new static();
75 $result->fatal( $message, ...$parameters );
86 $result =
new static();
103 $errorsOnlyStatusValue = static::newGood();
104 $warningsOnlyStatusValue = static::newGood();
105 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
106 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
108 foreach ( $this->errors as $item ) {
109 if ( $item[
'type'] ===
'warning' ) {
110 $warningsOnlyStatusValue->errors[] = $item;
112 $errorsOnlyStatusValue->errors[] = $item;
116 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
176 $this->ok = (bool)
$ok;
198 private function addError( array $newError ) {
200 $isEqual =
static function ( $existingError ) use ( $newError ) {
203 return $newError[
'message'] == $existingError[
'message'];
205 return $newError[
'message']->getKey() === $existingError[
'message'] &&
206 $newError[
'message']->getParams() === $existingError[
'params'];
210 $isEqual =
static function ( $existingError ) use ( $newError ) {
212 return $newError[
'message'] === $existingError[
'message']->getKey() &&
213 $newError[
'params'] === $existingError[
'message']->getParams();
215 return $newError[
'message'] === $existingError[
'message'] &&
216 $newError[
'params'] === $existingError[
'params'];
220 foreach ( $this->errors as $index => $existingError ) {
221 if ( $isEqual( $existingError ) ) {
222 if ( $newError[
'type' ] ===
'error' && $existingError[
'type' ] ===
'warning' ) {
223 $this->errors[ $index ][
'type' ] =
'error';
228 $this->errors[] = $newError;
239 public function warning( $message, ...$parameters ) {
240 $message = $this->normalizeMessage( $message );
242 return $this->addError( [
244 'message' => $message,
245 'params' => $parameters
257 public function error( $message, ...$parameters ) {
258 $message = $this->normalizeMessage( $message );
260 return $this->addError( [
262 'message' => $message,
263 'params' => $parameters
275 public function fatal( $message, ...$parameters ) {
277 return $this->
error( $message, ...$parameters );
287 public function merge( $other, $overwriteValue =
false ) {
288 foreach ( $other->errors as $error ) {
289 $this->addError( $error );
291 $this->ok = $this->ok && $other->ok;
292 if ( $overwriteValue ) {
293 $this->value = $other->value;
295 $this->successCount += $other->successCount;
296 $this->failCount += $other->failCount;
313 foreach ( $this->errors as $error ) {
314 if ( $error[
'type'] ===
$type ) {
331 $message = $message->getKey();
333 $message = $message->getKey();
336 foreach ( $this->errors as $error ) {
338 && $error[
'message']->getKey() === $message
341 } elseif ( $error[
'message'] === $message ) {
358 foreach ( $messages as $message ) {
360 $message = $message->getKey();
362 $message = $message->getKey();
364 $exceptedKeys[] = $message;
367 foreach ( $this->errors as $error ) {
369 $actualKey = $error[
'message']->getKey();
371 $actualKey = $error[
'message'];
373 if ( !in_array( $actualKey, $exceptedKeys,
true ) ) {
396 $dest = $this->normalizeMessage( $dest );
398 foreach ( $this->errors as $index => $error ) {
399 if ( $error[
'message'] ===
$source ) {
400 $this->errors[$index][
'message'] = $dest;
403 && $error[
'message']->getKey() ===
$source ) {
404 $this->errors[$index][
'message'] = $dest;
419 $status = $this->
isOK() ?
"OK" :
"Error";
420 if ( count( $this->errors ) ) {
421 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
423 $errorcount =
"no errors detected";
425 if ( isset( $this->value ) ) {
426 $valstr = gettype( $this->value ) .
" value set";
427 if ( is_object( $this->value ) ) {
428 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
431 $valstr =
"no value set";
433 $out = sprintf(
"<%s, %s, %s>",
438 if ( count( $this->errors ) > 0 ) {
439 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
442 foreach ( $this->errors as $error ) {
444 $key = $error[
'message']->getKey();
445 $params = $error[
'message']->getParams();
446 } elseif ( $error[
'params'] ) {
447 $key = $error[
'message'];
448 $params = $error[
'params'];
450 $key = $error[
'message'];
454 $type = $error[
'type'];
455 $keyChunks = str_split( $key, 25 );
456 $paramsChunks = str_split( $this->flattenParams( $params,
" | " ), 36 );
459 foreach ( array_map(
null, [
$type ], $keyChunks, $paramsChunks )
460 as [ $typeChunk, $keyChunk, $paramsChunk ]
462 $out .= sprintf(
"| %-8s | %-25.25s | %-36.36s |\n",
481 private function flattenParams( array $params,
string $joiner =
', ' ): string {
483 foreach ( $params as $p ) {
484 if ( is_array( $p ) ) {
485 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
487 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
492 $ret[] = strlen( $r ) > 100 ? substr( $r, 0, 99 ) .
"..." : $r;
494 return implode( $joiner, $ret );
508 foreach ( $this->getErrors() as $error ) {
509 if (
$type ===
false || $error[
'type'] ===
$type ) {
511 $result[] = array_merge(
512 [ $error[
'message']->getKey() ],
513 $error[
'message']->getParams()
515 } elseif ( $error[
'params'] ) {
516 $result[] = array_merge( [ $error[
'message'] ], $error[
'params'] );
518 $result[] = [ $error[
'message'] ];
531 private function normalizeMessage( $message ) {
534 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.
__toString()
Returns a string representation of the status for debugging.
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.
int $successCount
Counter for batch operations.