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;
169 $this->ok = (bool)$ok;
170 $this->value = $value;
179 public function warning( $message, ...$parameters ) {
182 'message' => $message,
183 'params' => $parameters
194 public function error( $message, ...$parameters ) {
197 'message' => $message,
198 'params' => $parameters
209 public function fatal( $message, ...$parameters ) {
212 'message' => $message,
213 'params' => $parameters
224 public function merge( $other, $overwriteValue =
false ) {
225 $this->errors = array_merge( $this->errors, $other->errors );
226 $this->ok = $this->ok && $other->ok;
227 if ( $overwriteValue ) {
228 $this->value = $other->value;
230 $this->successCount += $other->successCount;
231 $this->failCount += $other->failCount;
246 foreach ( $this->errors as $error ) {
247 if ( $error[
'type'] ===
$type ) {
264 $message = $message->getKey();
266 foreach ( $this->errors as $error ) {
268 && $error[
'message']->getKey() === $message
271 } elseif ( $error[
'message'] === $message ) {
293 foreach ( $this->errors as $index => $error ) {
294 if ( $error[
'message'] ===
$source ) {
295 $this->errors[$index][
'message'] = $dest;
307 $status = $this->
isOK() ?
"OK" :
"Error";
308 if ( count( $this->errors ) ) {
309 $errorcount =
"collected " . ( count( $this->errors ) ) .
" error(s) on the way";
311 $errorcount =
"no errors detected";
313 if ( isset( $this->value ) ) {
314 $valstr = gettype( $this->value ) .
" value set";
315 if ( is_object( $this->value ) ) {
316 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
319 $valstr =
"no value set";
321 $out = sprintf(
"<%s, %s, %s>",
326 if ( count( $this->errors ) > 0 ) {
327 $hdr = sprintf(
"+-%'-4s-+-%'-25s-+-%'-40s-+\n",
"",
"",
"" );
331 foreach ( $this->errors as $error ) {
333 $key = $error[
'message']->getKey();
334 $params = $error[
'message']->getParams();
335 } elseif ( $error[
'params'] ) {
336 $key = $error[
'message'];
337 $params = $error[
'params'];
339 $key = $error[
'message'];
343 $out .= sprintf(
"| %4d | %-25.25s | %-40.40s |\n",
346 self::flattenParams( $params )
362 foreach ( $params as $p ) {
363 if ( is_array( $p ) ) {
364 $ret[] =
'[ ' . self::flattenParams( $p ) .
' ]';
366 $ret[] =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
371 return implode(
' ', $ret );
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.
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.
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.