49 protected $errors = [];
58 public $successCount = 0;
61 public $failCount = 0;
70 public static function newFatal( $message, ...$parameters ) {
71 $result =
new static();
72 $result->fatal( $message, ...$parameters );
82 public static function newGood( $value =
null ) {
83 $result =
new static();
84 $result->value = $value;
100 $errorsOnlyStatusValue = static::newGood();
101 $warningsOnlyStatusValue = static::newGood();
102 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
103 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
105 foreach ( $this->errors as $item ) {
106 if ( $item[
'type'] ===
'warning' ) {
107 $warningsOnlyStatusValue->errors[] = $item;
109 $errorsOnlyStatusValue->errors[] = $item;
113 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
123 return $this->ok && !$this->errors;
150 return $this->errors;
172 $this->ok = (bool)$ok;
173 $this->value = $value;
195 $isEqual =
static function ( $existingError ) use ( $newError ) {
198 return $newError[
'message'] == $existingError[
'message'];
200 return $newError[
'message']->getKey() === $existingError[
'message'] &&
201 $newError[
'message']->getParams() === $existingError[
'params'];
205 $isEqual =
static function ( $existingError ) use ( $newError ) {
207 return $newError[
'message'] === $existingError[
'message']->getKey() &&
208 $newError[
'params'] === $existingError[
'message']->getParams();
210 return $newError[
'message'] === $existingError[
'message'] &&
211 $newError[
'params'] === $existingError[
'params'];
215 foreach ( $this->errors as $index => $existingError ) {
216 if ( $isEqual( $existingError ) ) {
217 if ( $newError[
'type' ] ===
'error' && $existingError[
'type' ] ===
'warning' ) {
218 $this->errors[ $index ][
'type' ] =
'error';
223 $this->errors[] = $newError;
234 public function warning( $message, ...$parameters ) {
237 'message' => $message,
238 'params' => $parameters
250 public function error( $message, ...$parameters ) {
253 'message' => $message,
254 'params' => $parameters
266 public function fatal( $message, ...$parameters ) {
268 return $this->
error( $message, ...$parameters );
278 public function merge( $other, $overwriteValue =
false ) {
279 foreach ( $other->errors as $error ) {
282 $this->ok = $this->ok && $other->ok;
283 if ( $overwriteValue ) {
284 $this->value = $other->value;
286 $this->successCount += $other->successCount;
287 $this->failCount += $other->failCount;
303 foreach ( $this->errors as $error ) {
304 if ( $error[
'type'] ===
$type ) {
321 $message = $message->getKey();
323 foreach ( $this->errors as $error ) {
325 && $error[
'message']->getKey() === $message
328 } elseif ( $error[
'message'] === $message ) {
350 foreach ( $this->errors as $index => $error ) {
351 if ( $error[
'message'] ===
$source ) {
352 $this->errors[$index][
'message'] = $dest;
364 $status = $this->
isOK() ?
"OK" :
"Error";
365 if ( count( $this->errors ) ) {
366 $errorcount =
"collected " . ( count( $this->errors ) ) .
" error(s) on the way";
368 $errorcount =
"no errors detected";
370 if ( isset( $this->value ) ) {
371 $valstr = gettype( $this->value ) .
" value set";
372 if ( is_object( $this->value ) ) {
373 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
376 $valstr =
"no value set";
378 $out = sprintf(
"<%s, %s, %s>",
383 if ( count( $this->errors ) > 0 ) {
384 $hdr = sprintf(
"+-%'-4s-+-%'-25s-+-%'-40s-+\n",
"",
"",
"" );
388 foreach ( $this->errors as $error ) {
390 $key = $error[
'message']->getKey();
391 $params = $error[
'message']->getParams();
392 } elseif ( $error[
'params'] ) {
393 $key = $error[
'message'];
394 $params = $error[
'params'];
396 $key = $error[
'message'];
400 $out .= sprintf(
"| %4d | %-25.25s | %-40.40s |\n",
419 foreach ( $params as $p ) {
420 if ( is_array( $p ) ) {
421 $ret[] =
'[ ' . self::flattenParams( $p ) .
' ]';
423 $ret[] =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
428 return implode(
' ', $ret );
442 foreach ( $this->getErrors() as $error ) {
443 if (
$type ===
false || $error[
'type'] ===
$type ) {
445 $result[] = array_merge(
446 [ $error[
'message']->getKey() ],
447 $error[
'message']->getParams()
449 } elseif ( $error[
'params'] ) {
450 $result[] = array_merge( [ $error[
'message'] ], $error[
'params'] );
452 $result[] = [ $error[
'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.
addError(array $newError)
Add a new error to the error array ($this->errors) if that error is not already in the error array.
merge( $other, $overwriteValue=false)
Merge another status object into this one.
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.
flattenParams(array $params)
int $successCount
Counter for batch operations.