MediaWiki  master
StatusValue.php
Go to the documentation of this file.
1 <?php
43 class StatusValue {
44 
46  protected $ok = true;
47 
49  protected $errors = [];
50 
52  public $value;
53 
55  public $success = [];
56 
58  public $successCount = 0;
59 
61  public $failCount = 0;
62 
70  public static function newFatal( $message, ...$parameters ) {
71  $result = new static();
72  $result->fatal( $message, ...$parameters );
73  return $result;
74  }
75 
82  public static function newGood( $value = null ) {
83  $result = new static();
84  $result->value = $value;
85  return $result;
86  }
87 
99  public function splitByErrorType() {
100  $errorsOnlyStatusValue = static::newGood();
101  $warningsOnlyStatusValue = static::newGood();
102  $warningsOnlyStatusValue->setResult( true, $this->getValue() );
103  $errorsOnlyStatusValue->setResult( $this->isOK(), $this->getValue() );
104 
105  foreach ( $this->errors as $item ) {
106  if ( $item['type'] === 'warning' ) {
107  $warningsOnlyStatusValue->errors[] = $item;
108  } else {
109  $errorsOnlyStatusValue->errors[] = $item;
110  }
111  }
112 
113  return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
114  }
115 
122  public function isGood() {
123  return $this->ok && !$this->errors;
124  }
125 
131  public function isOK() {
132  return $this->ok;
133  }
134 
138  public function getValue() {
139  return $this->value;
140  }
141 
149  public function getErrors() {
150  return $this->errors;
151  }
152 
158  public function setOK( $ok ) {
159  $this->ok = $ok;
160  }
161 
168  public function setResult( $ok, $value = null ) {
169  $this->ok = (bool)$ok;
170  $this->value = $value;
171  }
172 
188  private function addError( array $newError ) {
189  if ( $newError[ 'message' ] instanceof MessageSpecifier ) {
190  $isEqual = static function ( $existingError ) use ( $newError ) {
191  if ( $existingError['message'] instanceof MessageSpecifier ) {
192  // compare attributes of both MessageSpecifiers
193  return $newError['message'] == $existingError['message'];
194  } else {
195  return $newError['message']->getKey() === $existingError['message'] &&
196  $newError['message']->getParams() === $existingError['params'];
197  }
198  };
199  } else {
200  $isEqual = static function ( $existingError ) use ( $newError ) {
201  if ( $existingError['message'] instanceof MessageSpecifier ) {
202  return $newError['message'] === $existingError['message']->getKey() &&
203  $newError['params'] === $existingError['message']->getParams();
204  } else {
205  return $newError['message'] === $existingError['message'] &&
206  $newError['params'] === $existingError['params'];
207  }
208  };
209  }
210  foreach ( $this->errors as $index => $existingError ) {
211  if ( $isEqual( $existingError ) ) {
212  if ( $newError[ 'type' ] === 'error' && $existingError[ 'type' ] === 'warning' ) {
213  $this->errors[ $index ][ 'type' ] = 'error';
214  }
215  return;
216  }
217  }
218  $this->errors[] = $newError;
219  }
220 
227  public function warning( $message, ...$parameters ) {
228  $this->addError( [
229  'type' => 'warning',
230  'message' => $message,
231  'params' => $parameters
232  ] );
233  }
234 
242  public function error( $message, ...$parameters ) {
243  $this->addError( [
244  'type' => 'error',
245  'message' => $message,
246  'params' => $parameters
247  ] );
248  }
249 
257  public function fatal( $message, ...$parameters ) {
258  $this->error( $message, ...$parameters );
259  $this->ok = false;
260  }
261 
268  public function merge( $other, $overwriteValue = false ) {
269  foreach ( $other->errors as $error ) {
270  $this->addError( $error );
271  }
272  $this->ok = $this->ok && $other->ok;
273  if ( $overwriteValue ) {
274  $this->value = $other->value;
275  }
276  $this->successCount += $other->successCount;
277  $this->failCount += $other->failCount;
278  }
279 
290  public function getErrorsByType( $type ) {
291  $result = [];
292  foreach ( $this->errors as $error ) {
293  if ( $error['type'] === $type ) {
294  $result[] = $error;
295  }
296  }
297 
298  return $result;
299  }
300 
308  public function hasMessage( $message ) {
309  if ( $message instanceof MessageSpecifier ) {
310  $message = $message->getKey();
311  }
312  foreach ( $this->errors as $error ) {
313  if ( $error['message'] instanceof MessageSpecifier
314  && $error['message']->getKey() === $message
315  ) {
316  return true;
317  } elseif ( $error['message'] === $message ) {
318  return true;
319  }
320  }
321 
322  return false;
323  }
324 
336  public function replaceMessage( $source, $dest ) {
337  $replaced = false;
338 
339  foreach ( $this->errors as $index => $error ) {
340  if ( $error['message'] === $source ) {
341  $this->errors[$index]['message'] = $dest;
342  $replaced = true;
343  }
344  }
345 
346  return $replaced;
347  }
348 
352  public function __toString() {
353  $status = $this->isOK() ? "OK" : "Error";
354  if ( count( $this->errors ) ) {
355  $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
356  } else {
357  $errorcount = "no errors detected";
358  }
359  if ( isset( $this->value ) ) {
360  $valstr = gettype( $this->value ) . " value set";
361  if ( is_object( $this->value ) ) {
362  $valstr .= "\"" . get_class( $this->value ) . "\" instance";
363  }
364  } else {
365  $valstr = "no value set";
366  }
367  $out = sprintf( "<%s, %s, %s>",
368  $status,
369  $errorcount,
370  $valstr
371  );
372  if ( count( $this->errors ) > 0 ) {
373  $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
374  $i = 1;
375  $out .= "\n";
376  $out .= $hdr;
377  foreach ( $this->errors as $error ) {
378  if ( $error['message'] instanceof MessageSpecifier ) {
379  $key = $error['message']->getKey();
380  $params = $error['message']->getParams();
381  } elseif ( $error['params'] ) {
382  $key = $error['message'];
383  $params = $error['params'];
384  } else {
385  $key = $error['message'];
386  $params = [];
387  }
388 
389  $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
390  $i,
391  $key,
392  $this->flattenParams( $params )
393  );
394  $i++;
395  }
396  $out .= $hdr;
397  }
398 
399  return $out;
400  }
401 
406  private function flattenParams( array $params ) : string {
407  $ret = [];
408  foreach ( $params as $p ) {
409  if ( is_array( $p ) ) {
410  $ret[] = '[ ' . self::flattenParams( $p ) . ' ]';
411  } elseif ( $p instanceof MessageSpecifier ) {
412  $ret[] = '{ ' . $p->getKey() . ': ' . self::flattenParams( $p->getParams() ) . ' }';
413  } else {
414  $ret[] = (string)$p;
415  }
416  }
417  return implode( ' ', $ret );
418  }
419 
428  protected function getStatusArray( $type = false ) {
429  $result = [];
430 
431  foreach ( $this->getErrors() as $error ) {
432  if ( $type === false || $error['type'] === $type ) {
433  if ( $error['message'] instanceof MessageSpecifier ) {
434  $result[] = array_merge(
435  [ $error['message']->getKey() ],
436  $error['message']->getParams()
437  );
438  } elseif ( $error['params'] ) {
439  $result[] = array_merge( [ $error['message'] ], $error['params'] );
440  } else {
441  $result[] = [ $error['message'] ];
442  }
443  }
444  }
445 
446  return $result;
447  }
448 }
StatusValue\__toString
__toString()
Definition: StatusValue.php:352
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
StatusValue\flattenParams
flattenParams(array $params)
Definition: StatusValue.php:406
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
StatusValue\getErrorsByType
getErrorsByType( $type)
Returns a list of status messages of the given type.
Definition: StatusValue.php:290
StatusValue\error
error( $message,... $parameters)
Add an error, do not set fatal flag This can be used for non-fatal errors.
Definition: StatusValue.php:242
StatusValue\replaceMessage
replaceMessage( $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
Definition: StatusValue.php:336
StatusValue\$errors
array[] $errors
Definition: StatusValue.php:49
MessageSpecifier
Definition: MessageSpecifier.php:24
StatusValue\$ok
bool $ok
Definition: StatusValue.php:46
StatusValue\warning
warning( $message,... $parameters)
Add a new warning.
Definition: StatusValue.php:227
StatusValue\setResult
setResult( $ok, $value=null)
Change operation result.
Definition: StatusValue.php:168
StatusValue\fatal
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
Definition: StatusValue.php:257
StatusValue\setOK
setOK( $ok)
Change operation status.
Definition: StatusValue.php:158
StatusValue\getValue
getValue()
Definition: StatusValue.php:138
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:122
StatusValue\isOK
isOK()
Returns whether the operation completed.
Definition: StatusValue.php:131
StatusValue\splitByErrorType
splitByErrorType()
Splits this StatusValue object into two new StatusValue objects, one which contains only the error me...
Definition: StatusValue.php:99
StatusValue\merge
merge( $other, $overwriteValue=false)
Merge another status object into this one.
Definition: StatusValue.php:268
StatusValue\getStatusArray
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
Definition: StatusValue.php:428
StatusValue\$failCount
int $failCount
Counter for batch operations.
Definition: StatusValue.php:61
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
StatusValue\getErrors
getErrors()
Get the list of errors.
Definition: StatusValue.php:149
StatusValue\$success
bool[] $success
Map of (key => bool) to indicate success of each part of batch operations.
Definition: StatusValue.php:55
StatusValue\$value
mixed $value
Definition: StatusValue.php:52
$source
$source
Definition: mwdoc-filter.php:34
StatusValue\hasMessage
hasMessage( $message)
Returns true if the specified message is present as a warning or error.
Definition: StatusValue.php:308
StatusValue\$successCount
int $successCount
Counter for batch operations.
Definition: StatusValue.php:58
StatusValue\addError
addError(array $newError)
Add a new error to the error array ($this->errors) if that error is not already in the error array.
Definition: StatusValue.php:188
$type
$type
Definition: testCompression.php:52