69 public static function newFatal( $message, ...$parameters ) {
70 $result =
new static();
71 $result->fatal( $message, ...$parameters );
81 public static function newGood( $value =
null ) {
82 $result =
new static();
99 $errorsOnlyStatusValue = clone $this;
100 $warningsOnlyStatusValue = clone $this;
101 $warningsOnlyStatusValue->ok =
true;
103 $errorsOnlyStatusValue->errors = $warningsOnlyStatusValue->errors = [];
104 foreach ( $this->errors as $item ) {
105 if ( $item[
'type'] ===
'warning' ) {
106 $warningsOnlyStatusValue->errors[] = $item;
108 $errorsOnlyStatusValue->errors[] = $item;
112 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
122 return $this->ok && !$this->errors;
149 return $this->errors;
168 $this->ok = (bool)
$ok;
178 public function warning( $message, ...$parameters ) {
181 'message' => $message,
182 'params' => $parameters
193 public function error( $message, ...$parameters ) {
196 'message' => $message,
197 'params' => $parameters
208 public function fatal( $message, ...$parameters ) {
211 'message' => $message,
212 'params' => $parameters
223 public function merge( $other, $overwriteValue =
false ) {
224 $this->errors = array_merge( $this->errors, $other->errors );
225 $this->ok = $this->ok && $other->ok;
226 if ( $overwriteValue ) {
227 $this->value = $other->value;
229 $this->successCount += $other->successCount;
230 $this->failCount += $other->failCount;
245 foreach ( $this->errors as $error ) {
246 if ( $error[
'type'] ===
$type ) {
263 $message = $message->getKey();
265 foreach ( $this->errors as $error ) {
267 && $error[
'message']->getKey() === $message
270 } elseif ( $error[
'message'] === $message ) {
292 foreach ( $this->errors as $index => $error ) {
293 if ( $error[
'message'] ===
$source ) {
294 $this->errors[$index][
'message'] = $dest;
306 $status = $this->
isOK() ?
"OK" :
"Error";
307 if ( count( $this->errors ) ) {
308 $errorcount =
"collected " . ( count( $this->errors ) ) .
" error(s) on the way";
310 $errorcount =
"no errors detected";
312 if ( isset( $this->value ) ) {
313 $valstr = gettype( $this->value ) .
" value set";
314 if ( is_object( $this->value ) ) {
315 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
318 $valstr =
"no value set";
320 $out = sprintf(
"<%s, %s, %s>",
325 if ( count( $this->errors ) > 0 ) {
326 $hdr = sprintf(
"+-%'-4s-+-%'-25s-+-%'-40s-+\n",
"",
"",
"" );
330 foreach ( $this->errors as $error ) {
332 $key = $error[
'message']->getKey();
333 $params = $error[
'message']->getParams();
334 } elseif ( $error[
'params'] ) {
335 $key = $error[
'message'];
336 $params = $error[
'params'];
338 $key = $error[
'message'];
342 $out .= sprintf(
"| %4d | %-25.25s | %-40.40s |\n",
345 implode(
" ", $params )
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.
int $successCount
Counter for batch operations.