MediaWiki  1.34.0
StatusValue.php
Go to the documentation of this file.
1 <?php
42 class StatusValue {
43 
45  protected $ok = true;
46 
48  protected $errors = [];
49 
51  public $value;
52 
54  public $success = [];
55 
57  public $successCount = 0;
58 
60  public $failCount = 0;
61 
69  public static function newFatal( $message, ...$parameters ) {
70  $result = new static();
71  $result->fatal( $message, ...$parameters );
72  return $result;
73  }
74 
81  public static function newGood( $value = null ) {
82  $result = new static();
83  $result->value = $value;
84  return $result;
85  }
86 
98  public function splitByErrorType() {
99  $errorsOnlyStatusValue = clone $this;
100  $warningsOnlyStatusValue = clone $this;
101  $warningsOnlyStatusValue->ok = true;
102 
103  $errorsOnlyStatusValue->errors = $warningsOnlyStatusValue->errors = [];
104  foreach ( $this->errors as $item ) {
105  if ( $item['type'] === 'warning' ) {
106  $warningsOnlyStatusValue->errors[] = $item;
107  } else {
108  $errorsOnlyStatusValue->errors[] = $item;
109  }
110  }
111 
112  return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
113  }
114 
121  public function isGood() {
122  return $this->ok && !$this->errors;
123  }
124 
130  public function isOK() {
131  return $this->ok;
132  }
133 
137  public function getValue() {
138  return $this->value;
139  }
140 
148  public function getErrors() {
149  return $this->errors;
150  }
151 
157  public function setOK( $ok ) {
158  $this->ok = $ok;
159  }
160 
167  public function setResult( $ok, $value = null ) {
168  $this->ok = (bool)$ok;
169  $this->value = $value;
170  }
171 
178  public function warning( $message, ...$parameters ) {
179  $this->errors[] = [
180  'type' => 'warning',
181  'message' => $message,
182  'params' => $parameters
183  ];
184  }
185 
193  public function error( $message, ...$parameters ) {
194  $this->errors[] = [
195  'type' => 'error',
196  'message' => $message,
197  'params' => $parameters
198  ];
199  }
200 
208  public function fatal( $message, ...$parameters ) {
209  $this->errors[] = [
210  'type' => 'error',
211  'message' => $message,
212  'params' => $parameters
213  ];
214  $this->ok = false;
215  }
216 
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;
228  }
229  $this->successCount += $other->successCount;
230  $this->failCount += $other->failCount;
231  }
232 
243  public function getErrorsByType( $type ) {
244  $result = [];
245  foreach ( $this->errors as $error ) {
246  if ( $error['type'] === $type ) {
247  $result[] = $error;
248  }
249  }
250 
251  return $result;
252  }
253 
261  public function hasMessage( $message ) {
262  if ( $message instanceof MessageSpecifier ) {
263  $message = $message->getKey();
264  }
265  foreach ( $this->errors as $error ) {
266  if ( $error['message'] instanceof MessageSpecifier
267  && $error['message']->getKey() === $message
268  ) {
269  return true;
270  } elseif ( $error['message'] === $message ) {
271  return true;
272  }
273  }
274 
275  return false;
276  }
277 
289  public function replaceMessage( $source, $dest ) {
290  $replaced = false;
291 
292  foreach ( $this->errors as $index => $error ) {
293  if ( $error['message'] === $source ) {
294  $this->errors[$index]['message'] = $dest;
295  $replaced = true;
296  }
297  }
298 
299  return $replaced;
300  }
301 
305  public function __toString() {
306  $status = $this->isOK() ? "OK" : "Error";
307  if ( count( $this->errors ) ) {
308  $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
309  } else {
310  $errorcount = "no errors detected";
311  }
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";
316  }
317  } else {
318  $valstr = "no value set";
319  }
320  $out = sprintf( "<%s, %s, %s>",
321  $status,
322  $errorcount,
323  $valstr
324  );
325  if ( count( $this->errors ) > 0 ) {
326  $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
327  $i = 1;
328  $out .= "\n";
329  $out .= $hdr;
330  foreach ( $this->errors as $error ) {
331  if ( $error['message'] instanceof MessageSpecifier ) {
332  $key = $error['message']->getKey();
333  $params = $error['message']->getParams();
334  } elseif ( $error['params'] ) {
335  $key = $error['message'];
336  $params = $error['params'];
337  } else {
338  $key = $error['message'];
339  $params = [];
340  }
341 
342  $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
343  $i,
344  $key,
345  implode( " ", $params )
346  );
347  $i += 1;
348  }
349  $out .= $hdr;
350  }
351 
352  return $out;
353  }
354 }
StatusValue\__toString
__toString()
Definition: StatusValue.php:305
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:42
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
StatusValue\getErrorsByType
getErrorsByType( $type)
Returns a list of status messages of the given type.
Definition: StatusValue.php:243
StatusValue\error
error( $message,... $parameters)
Add an error, do not set fatal flag This can be used for non-fatal errors.
Definition: StatusValue.php:193
StatusValue\replaceMessage
replaceMessage( $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
Definition: StatusValue.php:289
StatusValue\$errors
array[] $errors
Definition: StatusValue.php:48
MessageSpecifier
Definition: MessageSpecifier.php:21
StatusValue\$ok
bool $ok
Definition: StatusValue.php:45
StatusValue\warning
warning( $message,... $parameters)
Add a new warning.
Definition: StatusValue.php:178
StatusValue\setResult
setResult( $ok, $value=null)
Change operation result.
Definition: StatusValue.php:167
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:208
value
if( $inline) $status value
Definition: SyntaxHighlight.php:346
StatusValue\setOK
setOK( $ok)
Change operation status.
Definition: StatusValue.php:157
StatusValue\getValue
getValue()
Definition: StatusValue.php:137
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:121
StatusValue\isOK
isOK()
Returns whether the operation completed.
Definition: StatusValue.php:130
StatusValue\splitByErrorType
splitByErrorType()
Splits this StatusValue object into two new StatusValue objects, one which contains only the error me...
Definition: StatusValue.php:98
StatusValue\merge
merge( $other, $overwriteValue=false)
Merge another status object into this one.
Definition: StatusValue.php:223
StatusValue\$failCount
int $failCount
Counter for batch operations.
Definition: StatusValue.php:60
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
StatusValue\getErrors
getErrors()
Get the list of errors.
Definition: StatusValue.php:148
StatusValue\$success
bool[] $success
Map of (key => bool) to indicate success of each part of batch operations.
Definition: StatusValue.php:54
$status
return $status
Definition: SyntaxHighlight.php:347
StatusValue\$value
mixed $value
Definition: StatusValue.php:51
$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:261
StatusValue\$successCount
int $successCount
Counter for batch operations.
Definition: StatusValue.php:57
$type
$type
Definition: testCompression.php:48