/** @var bool */
protected $ok = true;
/** @var array */
- protected $errors = array();
+ protected $errors = [];
/** @var mixed */
public $value;
/** @var array Map of (key => bool) to indicate success of each part of batch operations */
- public $success = array();
+ public $success = [];
/** @var int Counter for batch operations */
public $successCount = 0;
/** @var int Counter for batch operations */
* Factory function for fatal errors
*
* @param string|MessageSpecifier $message Message key or object
- * @return Status
+ * @return static
*/
public static function newFatal( $message /*, parameters...*/ ) {
$params = func_get_args();
$result = new static();
- call_user_func_array( array( &$result, 'fatal' ), $params );
+ call_user_func_array( [ &$result, 'fatal' ], $params );
return $result;
}
* Factory function for good results
*
* @param mixed $value
- * @return Status
+ * @return static
*/
public static function newGood( $value = null ) {
$result = new static();
return $result;
}
+ /**
+ * Splits this StatusValue object into two new StatusValue objects, one which contains only
+ * the error messages, and one that contains the warnings, only. The returned array is
+ * defined as:
+ * [
+ * 0 => object(StatusValue) # the StatusValue with error messages, only
+ * 1 => object(StatusValue) # The StatusValue with warning messages, only
+ * ]
+ *
+ * @return StatusValue[]
+ */
+ public function splitByErrorType() {
+ $errorsOnlyStatusValue = clone $this;
+ $warningsOnlyStatusValue = clone $this;
+ $warningsOnlyStatusValue->ok = true;
+
+ $errorsOnlyStatusValue->errors = $warningsOnlyStatusValue->errors = [];
+ foreach ( $this->errors as $item ) {
+ if ( $item['type'] === 'warning' ) {
+ $warningsOnlyStatusValue->errors[] = $item;
+ } else {
+ $errorsOnlyStatusValue->errors[] = $item;
+ }
+ };
+
+ return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
+ }
+
/**
* Returns whether the operation completed and didn't have any error or
* warnings
}
/**
- * Change operation resuklt
+ * Change operation result
*
* @param bool $ok Whether the operation completed
* @param mixed $value
*/
public function setResult( $ok, $value = null ) {
- $this->ok = $ok;
+ $this->ok = (bool)$ok;
$this->value = $value;
}
* @param string|MessageSpecifier $message Message key or object
*/
public function warning( $message /*, parameters... */ ) {
- $this->errors[] = array(
+ $this->errors[] = [
'type' => 'warning',
'message' => $message,
'params' => array_slice( func_get_args(), 1 )
- );
+ ];
}
/**
* @param string|MessageSpecifier $message Message key or object
*/
public function error( $message /*, parameters... */ ) {
- $this->errors[] = array(
+ $this->errors[] = [
'type' => 'error',
'message' => $message,
'params' => array_slice( func_get_args(), 1 )
- );
+ ];
}
/**
* @param string|MessageSpecifier $message Message key or object
*/
public function fatal( $message /*, parameters... */ ) {
- $this->errors[] = array(
+ $this->errors[] = [
'type' => 'error',
'message' => $message,
'params' => array_slice( func_get_args(), 1 )
- );
+ ];
$this->ok = false;
}
/**
* Returns a list of status messages of the given type
*
- * Each entry is a map of (message:string or MessageSpecifier,params:array))
+ * Each entry is a map of:
+ * - message: string message key or MessageSpecifier
+ * - params: array list of parameters
*
* @param string $type
* @return array
*/
public function getErrorsByType( $type ) {
- $result = array();
+ $result = [];
foreach ( $this->errors as $error ) {
if ( $error['type'] === $type ) {
$result[] = $error;
* Note, due to the lack of tools for comparing IStatusMessage objects, this
* function will not work when using such an object as the search parameter.
*
- * @param IStatusMessage|string $source Message key or object to search for
- * @param IStatusMessage|string $dest Replacement message key or object
+ * @param MessageSpecifier|string $source Message key or object to search for
+ * @param MessageSpecifier|string $dest Replacement message key or object
* @return bool Return true if the replacement was done, false otherwise.
*/
public function replaceMessage( $source, $dest ) {
$params = $error['params'];
} else {
$key = $error['message'];
- $params = array();
+ $params = [];
}
$out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",