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->errors[] = [
211  'type' => 'error',
212  'message' => $message,
213  'params' => $parameters
214  ];
215  $this->ok = false;
216  }
217 
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;
229  }
230  $this->successCount += $other->successCount;
231  $this->failCount += $other->failCount;
232  }
233 
244  public function getErrorsByType( $type ) {
245  $result = [];
246  foreach ( $this->errors as $error ) {
247  if ( $error['type'] === $type ) {
248  $result[] = $error;
249  }
250  }
251 
252  return $result;
253  }
254 
262  public function hasMessage( $message ) {
263  if ( $message instanceof MessageSpecifier ) {
264  $message = $message->getKey();
265  }
266  foreach ( $this->errors as $error ) {
267  if ( $error['message'] instanceof MessageSpecifier
268  && $error['message']->getKey() === $message
269  ) {
270  return true;
271  } elseif ( $error['message'] === $message ) {
272  return true;
273  }
274  }
275 
276  return false;
277  }
278 
290  public function replaceMessage( $source, $dest ) {
291  $replaced = false;
292 
293  foreach ( $this->errors as $index => $error ) {
294  if ( $error['message'] === $source ) {
295  $this->errors[$index]['message'] = $dest;
296  $replaced = true;
297  }
298  }
299 
300  return $replaced;
301  }
302 
306  public function __toString() {
307  $status = $this->isOK() ? "OK" : "Error";
308  if ( count( $this->errors ) ) {
309  $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
310  } else {
311  $errorcount = "no errors detected";
312  }
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";
317  }
318  } else {
319  $valstr = "no value set";
320  }
321  $out = sprintf( "<%s, %s, %s>",
322  $status,
323  $errorcount,
324  $valstr
325  );
326  if ( count( $this->errors ) > 0 ) {
327  $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
328  $i = 1;
329  $out .= "\n";
330  $out .= $hdr;
331  foreach ( $this->errors as $error ) {
332  if ( $error['message'] instanceof MessageSpecifier ) {
333  $key = $error['message']->getKey();
334  $params = $error['message']->getParams();
335  } elseif ( $error['params'] ) {
336  $key = $error['message'];
337  $params = $error['params'];
338  } else {
339  $key = $error['message'];
340  $params = [];
341  }
342 
343  $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
344  $i,
345  $key,
346  self::flattenParams( $params )
347  );
348  $i += 1;
349  }
350  $out .= $hdr;
351  }
352 
353  return $out;
354  }
355 
360  private function flattenParams( array $params ) : string {
361  $ret = [];
362  foreach ( $params as $p ) {
363  if ( is_array( $p ) ) {
364  $ret[] = '[ ' . self::flattenParams( $p ) . ' ]';
365  } elseif ( $p instanceof MessageSpecifier ) {
366  $ret[] = '{ ' . $p->getKey() . ': ' . self::flattenParams( $p->getParams() ) . ' }';
367  } else {
368  $ret[] = (string)$p;
369  }
370  }
371  return implode( ' ', $ret );
372  }
373 }
StatusValue\__toString
__toString()
Definition: StatusValue.php:306
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:360
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:244
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:290
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:224
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:262
StatusValue\$successCount
int $successCount
Counter for batch operations.
Definition: StatusValue.php:58
$type
$type
Definition: testCompression.php:52