/**
* Factory function for fatal errors
*
- * @param string|Message $message message name or object
+ * @param string|Message $message Message name or object
* @return Status
*/
static function newFatal( $message /*, parameters...*/ ) {
/**
* Factory function for good results
*
- * @param $value Mixed
+ * @param mixed $value
* @return Status
*/
static function newGood( $value = null ) {
* Returns whether the operation completed and didn't have any error or
* warnings
*
- * @return Boolean
+ * @return bool
*/
public function isGood() {
return $this->ok && !$this->errors;
/**
* Returns whether the operation completed
*
- * @return Boolean
+ * @return bool
*/
public function isOK() {
return $this->ok;
/**
* Add a new warning
*
- * @param string|Message $message message name or object
+ * @param string|Message $message Message name or object
*/
public function warning( $message /*, parameters... */ ) {
$params = array_slice( func_get_args(), 1 );
* Add an error, do not set fatal flag
* This can be used for non-fatal errors
*
- * @param string|Message $message message name or object
+ * @param string|Message $message Message name or object
*/
public function error( $message /*, parameters... */ ) {
$params = array_slice( func_get_args(), 1 );
* Add an error and set OK to false, indicating that the operation
* as a whole was fatal
*
- * @param string|Message $message message name or object
+ * @param string|Message $message Message name or object
*/
public function fatal( $message /*, parameters... */ ) {
$params = array_slice( func_get_args(), 1 );
$this->ok = false;
}
+ /**
+ * Don't save the callback when serializing, because Closures can't be
+ * serialized and we're going to clear it in __wakeup anyway.
+ */
+ public function __sleep() {
+ $keys = array_keys( get_object_vars( $this ) );
+ return array_diff( $keys, array( 'cleanCallback' ) );
+ }
+
/**
* Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
*/
}
/**
- * @param $params array
+ * @param array $params
* @return array
*/
protected function cleanParams( $params ) {
/**
* Get the error list as a wikitext formatted list
*
- * @param string $shortContext a short enclosing context message name, to
+ * @param string|bool $shortContext A short enclosing context message name, to
* be used when there is a single error
- * @param string $longContext a long enclosing context message name, for a list
- * @return String
+ * @param string|bool $longContext A long enclosing context message name, for a list
+ * @return string
*/
public function getWikiText( $shortContext = false, $longContext = false ) {
if ( count( $this->errors ) == 0 ) {
/**
* Return the message for a single error.
- * @param $error Mixed With an array & two values keyed by
+ * @param mixed $error With an array & two values keyed by
* 'message' and 'params', use those keys-value pairs.
* Otherwise, if its an array, just use the first value as the
* message and the remaining items as the params.
*
- * @return String
+ * @return string
*/
protected function getErrorMessage( $error ) {
if ( is_array( $error ) ) {
/**
* Get the error message as HTML. This is done by parsing the wikitext error
* message.
- * @param string $shortContext a short enclosing context message name, to
+ * @param string $shortContext A short enclosing context message name, to
* be used when there is a single error
- * @param string $longContext a long enclosing context message name, for a list
- * @return String
+ * @param string $longContext A long enclosing context message name, for a list
+ * @return string
*/
public function getHTML( $shortContext = false, $longContext = false ) {
$text = $this->getWikiText( $shortContext, $longContext );
/**
* Return an array with the wikitext for each item in the array.
- * @param $errors Array
- * @return Array
+ * @param array $errors
+ * @return array
*/
protected function getErrorMessageArray( $errors ) {
return array_map( array( $this, 'getErrorMessage' ), $errors );
}
/**
- * Returns a list of status messages of the given type
- * @param $type String
- * @return Array
+ * Returns a list of status messages of the given type (or all if false)
+ * @param string $type
+ * @return array
*/
- protected function getStatusArray( $type ) {
+ protected function getStatusArray( $type = false ) {
$result = array();
foreach ( $this->errors as $error ) {
- if ( $error['type'] === $type ) {
+ if ( $type === false || $error['type'] === $type ) {
if ( $error['message'] instanceof Message ) {
$result[] = array_merge(
array( $error['message']->getKey() ),
* Returns a list of status messages of the given type, with message and
* params left untouched, like a sane version of getStatusArray
*
- * @param $type String
+ * @param string $type
*
- * @return Array
+ * @return array
*/
public function getErrorsByType( $type ) {
$result = array();
/**
* Returns true if the specified message is present as a warning or error
*
- * Note, due to the lack of tools for comparing Message objects, this
- * function will not work when using a Message object as a parameter.
+ * @param string|Message $message Message key or object to search for
*
- * @param string $msg message name
- * @return Boolean
+ * @return bool
*/
- public function hasMessage( $msg ) {
+ public function hasMessage( $message ) {
+ if ( $message instanceof Message ) {
+ $message = $message->getKey();
+ }
foreach ( $this->errors as $error ) {
- if ( $error['message'] === $msg ) {
+ if ( $error['message'] instanceof Message
+ && $error['message']->getKey() === $message
+ ) {
+ return true;
+ } elseif ( $error['message'] === $message ) {
return true;
}
}
public function getValue() {
return $this->value;
}
+
+ /**
+ * @return string
+ */
+ public function __toString() {
+ $status = $this->isOK() ? "OK" : "Error";
+ if ( count( $this->errors ) ) {
+ $errorcount = "collected " . ( count( $this->errors ) ) . " error(s) on the way";
+ } else {
+ $errorcount = "no errors detected";
+ }
+ if ( isset( $this->value ) ) {
+ $valstr = gettype( $this->value ) . " value set";
+ if ( is_object( $this->value ) ) {
+ $valstr .= "\"" . get_class( $this->value ) . "\" instance";
+ }
+ } else {
+ $valstr = "no value set";
+ }
+ $out = sprintf( "<%s, %s, %s>",
+ $status,
+ $errorcount,
+ $valstr
+ );
+ if ( count( $this->errors ) > 0 ) {
+ $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
+ $i = 1;
+ $out .= "\n";
+ $out .= $hdr;
+ foreach ( $this->getStatusArray() as $stat ) {
+ $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
+ $i,
+ $stat[0],
+ implode( " ", array_slice( $stat, 1 ) )
+ );
+ $i += 1;
+ }
+ $out .= $hdr;
+ };
+ return $out;
+ }
}