52 protected $errors = [];
61 public $successCount = 0;
64 public $failCount = 0;
73 public static function newFatal( $message, ...$parameters ) {
74 $result =
new static();
75 $result->fatal( $message, ...$parameters );
85 public static function newGood( $value =
null ) {
86 $result =
new static();
87 $result->value = $value;
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 ];
126 return $this->ok && !$this->errors;
153 return $this->errors;
175 $this->ok = (bool)$ok;
176 $this->value = $value;
196 private function addError( array $newError ) {
198 $isEqual =
static function ( $existingError ) use ( $newError ) {
201 return $newError[
'message'] == $existingError[
'message'];
203 return $newError[
'message']->getKey() === $existingError[
'message'] &&
204 $newError[
'message']->getParams() === $existingError[
'params'];
208 $isEqual =
static function ( $existingError ) use ( $newError ) {
210 return $newError[
'message'] === $existingError[
'message']->getKey() &&
211 $newError[
'params'] === $existingError[
'message']->getParams();
213 return $newError[
'message'] === $existingError[
'message'] &&
214 $newError[
'params'] === $existingError[
'params'];
218 foreach ( $this->errors as $index => $existingError ) {
219 if ( $isEqual( $existingError ) ) {
220 if ( $newError[
'type' ] ===
'error' && $existingError[
'type' ] ===
'warning' ) {
221 $this->errors[ $index ][
'type' ] =
'error';
226 $this->errors[] = $newError;
237 public function warning( $message, ...$parameters ) {
238 $message = $this->normalizeMessage( $message, $parameters );
240 return $this->addError( [
242 'message' => $message,
243 'params' => $parameters
255 public function error( $message, ...$parameters ) {
256 $message = $this->normalizeMessage( $message, $parameters );
258 return $this->addError( [
260 'message' => $message,
261 'params' => $parameters
273 public function fatal( $message, ...$parameters ) {
275 return $this->
error( $message, ...$parameters );
285 public function merge( $other, $overwriteValue =
false ) {
286 foreach ( $other->errors as $error ) {
287 $this->addError( $error );
289 $this->ok = $this->ok && $other->ok;
290 if ( $overwriteValue ) {
291 $this->value = $other->value;
293 $this->successCount += $other->successCount;
294 $this->failCount += $other->failCount;
310 foreach ( $this->errors as $error ) {
311 if ( $error[
'type'] ===
$type ) {
328 $message = $message->getKey();
330 $message = $message->getKey();
333 foreach ( $this->errors as $error ) {
335 && $error[
'message']->getKey() === $message
338 } elseif ( $error[
'message'] === $message ) {
361 $dest = $this->normalizeMessage( $dest );
363 foreach ( $this->errors as $index => $error ) {
364 if ( $error[
'message'] ===
$source ) {
365 $this->errors[$index][
'message'] = $dest;
368 && $error[
'message']->getKey() ===
$source ) {
369 $this->errors[$index][
'message'] = $dest;
384 $status = $this->
isOK() ?
"OK" :
"Error";
385 if ( count( $this->errors ) ) {
386 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
388 $errorcount =
"no errors detected";
390 if ( isset( $this->value ) ) {
391 $valstr = gettype( $this->value ) .
" value set";
392 if ( is_object( $this->value ) ) {
393 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
396 $valstr =
"no value set";
398 $out = sprintf(
"<%s, %s, %s>",
403 if ( count( $this->errors ) > 0 ) {
404 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
408 foreach ( $this->errors as $error ) {
410 $key = $error[
'message']->getKey();
411 $params = $error[
'message']->getParams();
412 } elseif ( $error[
'params'] ) {
413 $key = $error[
'message'];
414 $params = $error[
'params'];
416 $key = $error[
'message'];
420 $type = $error[
'type'];
421 $keyChunks = mb_str_split( $key, 25 );
422 $paramsChunks = mb_str_split( $this->flattenParams( $params,
" | " ), 36 );
425 foreach ( array_map(
null, [
$type ], $keyChunks, $paramsChunks )
426 as [ $typeChunk, $keyChunk, $paramsChunk ]
428 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
449 private function flattenParams( array $params,
string $joiner =
', ' ): string {
451 foreach ( $params as $p ) {
452 if ( is_array( $p ) ) {
453 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
455 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
460 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
462 return implode( $joiner, $ret );
476 foreach ( $this->getErrors() as $error ) {
477 if (
$type ===
false || $error[
'type'] ===
$type ) {
479 $result[] = array_merge(
480 [ $error[
'message']->getKey() ],
481 $error[
'message']->getParams()
483 } elseif ( $error[
'params'] ) {
484 $result[] = array_merge( [ $error[
'message'] ], $error[
'params'] );
486 $result[] = [ $error[
'message'] ];
500 private function normalizeMessage( $message, array $parameters = [] ) {
503 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.
__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.