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 
179  public function warning( $message, ...$parameters ) {
180  $this->errors[] = [
181  'type' => 'warning',
182  'message' => $message,
183  'params' => $parameters
184  ];
185  }
186 
194  public function error( $message, ...$parameters ) {
195  $this->errors[] = [
196  'type' => 'error',
197  'message' => $message,
198  'params' => $parameters
199  ];
200  }
201 
209  public function fatal( $message, ...$parameters ) {
210  $this->error( $message, ...$parameters );
211  $this->ok = false;
212  }
213 
220  public function merge( $other, $overwriteValue = false ) {
221  $this->errors = array_merge( $this->errors, $other->errors );
222  $this->ok = $this->ok && $other->ok;
223  if ( $overwriteValue ) {
224  $this->value = $other->value;
225  }
226  $this->successCount += $other->successCount;
227  $this->failCount += $other->failCount;
228  }
229 
240  public function getErrorsByType( $type ) {
241  $result = [];
242  foreach ( $this->errors as $error ) {
243  if ( $error['type'] === $type ) {
244  $result[] = $error;
245  }
246  }
247 
248  return $result;
249  }
250 
258  public function hasMessage( $message ) {
259  if ( $message instanceof MessageSpecifier ) {
260  $message = $message->getKey();
261  }
262  foreach ( $this->errors as $error ) {
263  if ( $error['message'] instanceof MessageSpecifier
264  && $error['message']->getKey() === $message
265  ) {
266  return true;
267  } elseif ( $error['message'] === $message ) {
268  return true;
269  }
270  }
271 
272  return false;
273  }
274 
286  public function replaceMessage( $source, $dest ) {
287  $replaced = false;
288 
289  foreach ( $this->errors as $index => $error ) {
290  if ( $error['message'] === $source ) {
291  $this->errors[$index]['message'] = $dest;
292  $replaced = true;
293  }
294  }
295 
296  return $replaced;
297  }
298 
302  public function __toString() {
303  $status = $this->isOK() ? "OK" : "Error";
304  if ( count( $this->errors ) ) {
305  $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
306  } else {
307  $errorcount = "no errors detected";
308  }
309  if ( isset( $this->value ) ) {
310  $valstr = gettype( $this->value ) . " value set";
311  if ( is_object( $this->value ) ) {
312  $valstr .= "\"" . get_class( $this->value ) . "\" instance";
313  }
314  } else {
315  $valstr = "no value set";
316  }
317  $out = sprintf( "<%s, %s, %s>",
318  $status,
319  $errorcount,
320  $valstr
321  );
322  if ( count( $this->errors ) > 0 ) {
323  $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
324  $i = 1;
325  $out .= "\n";
326  $out .= $hdr;
327  foreach ( $this->errors as $error ) {
328  if ( $error['message'] instanceof MessageSpecifier ) {
329  $key = $error['message']->getKey();
330  $params = $error['message']->getParams();
331  } elseif ( $error['params'] ) {
332  $key = $error['message'];
333  $params = $error['params'];
334  } else {
335  $key = $error['message'];
336  $params = [];
337  }
338 
339  $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
340  $i,
341  $key,
342  self::flattenParams( $params )
343  );
344  $i += 1;
345  }
346  $out .= $hdr;
347  }
348 
349  return $out;
350  }
351 
356  private function flattenParams( array $params ) : string {
357  $ret = [];
358  foreach ( $params as $p ) {
359  if ( is_array( $p ) ) {
360  $ret[] = '[ ' . self::flattenParams( $p ) . ' ]';
361  } elseif ( $p instanceof MessageSpecifier ) {
362  $ret[] = '{ ' . $p->getKey() . ': ' . self::flattenParams( $p->getParams() ) . ' }';
363  } else {
364  $ret[] = (string)$p;
365  }
366  }
367  return implode( ' ', $ret );
368  }
369 }
StatusValue\__toString
__toString()
Definition: StatusValue.php:302
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:356
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:240
StatusValue\error
error( $message,... $parameters)
Add an error, do not set fatal flag This can be used for non-fatal errors.
Definition: StatusValue.php:194
StatusValue\replaceMessage
replaceMessage( $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
Definition: StatusValue.php:286
StatusValue\$errors
array[] $errors
Definition: StatusValue.php:49
MessageSpecifier
Stable for implementing.
Definition: MessageSpecifier.php:24
StatusValue\$ok
bool $ok
Definition: StatusValue.php:46
StatusValue\warning
warning( $message,... $parameters)
Add a new warning.
Definition: StatusValue.php:179
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:209
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:220
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:258
StatusValue\$successCount
int $successCount
Counter for batch operations.
Definition: StatusValue.php:58
$type
$type
Definition: testCompression.php:52