3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * Generic operation result class
23 * Has warning/error list, boolean status and arbitrary value
25 * "Good" means the operation was completed with no warnings or errors.
27 * "OK" means the operation was partially or wholly completed.
29 * An operation which is not OK should have errors so that the user can be
30 * informed as to what went wrong. Calling the fatal() function sets an error
31 * message and simultaneously switches off the OK flag.
33 * The recommended pattern for Status objects is to return a StatusValue
34 * unconditionally, i.e. both on success and on failure -- so that the
35 * developer of the calling code is reminded that the function can fail, and
36 * so that a lack of error-handling will be explicit.
38 * The use of Message objects should be avoided when serializability is needed.
46 protected $errors = [];
50 /** @var array Map of (key => bool) to indicate success of each part of batch operations */
52 /** @var int Counter for batch operations */
53 public $successCount = 0;
54 /** @var int Counter for batch operations */
55 public $failCount = 0;
58 * Factory function for fatal errors
60 * @param string|MessageSpecifier $message Message key or object
63 public static function newFatal( $message /*, parameters...*/ ) {
64 $params = func_get_args();
65 $result = new static();
66 call_user_func_array( [ &$result, 'fatal' ], $params );
71 * Factory function for good results
76 public static function newGood( $value = null ) {
77 $result = new static();
78 $result->value
= $value;
83 * Splits this StatusValue object into two new StatusValue objects, one which contains only
84 * the error messages, and one that contains the warnings, only. The returned array is
87 * 0 => object(StatusValue) # the StatusValue with error messages, only
88 * 1 => object(StatusValue) # The StatusValue with warning messages, only
91 * @return StatusValue[]
93 public function splitByErrorType() {
94 $errorsOnlyStatusValue = clone $this;
95 $warningsOnlyStatusValue = clone $this;
96 $warningsOnlyStatusValue->ok
= true;
98 $errorsOnlyStatusValue->errors
= $warningsOnlyStatusValue->errors
= [];
99 foreach ( $this->errors
as $item ) {
100 if ( $item['type'] === 'warning' ) {
101 $warningsOnlyStatusValue->errors
[] = $item;
103 $errorsOnlyStatusValue->errors
[] = $item;
107 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
111 * Returns whether the operation completed and didn't have any error or
116 public function isGood() {
117 return $this->ok
&& !$this->errors
;
121 * Returns whether the operation completed
125 public function isOK() {
132 public function getValue() {
137 * Get the list of errors
139 * Each error is a (message:string or MessageSpecifier,params:array) map
143 public function getErrors() {
144 return $this->errors
;
148 * Change operation status
152 public function setOK( $ok ) {
157 * Change operation resuklt
159 * @param bool $ok Whether the operation completed
160 * @param mixed $value
162 public function setResult( $ok, $value = null ) {
163 $this->ok
= (bool)$ok;
164 $this->value
= $value;
170 * @param string|MessageSpecifier $message Message key or object
172 public function warning( $message /*, parameters... */ ) {
175 'message' => $message,
176 'params' => array_slice( func_get_args(), 1 )
181 * Add an error, do not set fatal flag
182 * This can be used for non-fatal errors
184 * @param string|MessageSpecifier $message Message key or object
186 public function error( $message /*, parameters... */ ) {
189 'message' => $message,
190 'params' => array_slice( func_get_args(), 1 )
195 * Add an error and set OK to false, indicating that the operation
196 * as a whole was fatal
198 * @param string|MessageSpecifier $message Message key or object
200 public function fatal( $message /*, parameters... */ ) {
203 'message' => $message,
204 'params' => array_slice( func_get_args(), 1 )
210 * Merge another status object into this one
212 * @param StatusValue $other Other StatusValue object
213 * @param bool $overwriteValue Whether to override the "value" member
215 public function merge( $other, $overwriteValue = false ) {
216 $this->errors
= array_merge( $this->errors
, $other->errors
);
217 $this->ok
= $this->ok
&& $other->ok
;
218 if ( $overwriteValue ) {
219 $this->value
= $other->value
;
221 $this->successCount +
= $other->successCount
;
222 $this->failCount +
= $other->failCount
;
226 * Returns a list of status messages of the given type
228 * Each entry is a map of:
229 * - message: string message key or MessageSpecifier
230 * - params: array list of parameters
232 * @param string $type
235 public function getErrorsByType( $type ) {
237 foreach ( $this->errors
as $error ) {
238 if ( $error['type'] === $type ) {
247 * Returns true if the specified message is present as a warning or error
249 * @param string|MessageSpecifier $message Message key or object to search for
253 public function hasMessage( $message ) {
254 if ( $message instanceof MessageSpecifier
) {
255 $message = $message->getKey();
257 foreach ( $this->errors
as $error ) {
258 if ( $error['message'] instanceof MessageSpecifier
259 && $error['message']->getKey() === $message
262 } elseif ( $error['message'] === $message ) {
271 * If the specified source message exists, replace it with the specified
272 * destination message, but keep the same parameters as in the original error.
274 * Note, due to the lack of tools for comparing IStatusMessage objects, this
275 * function will not work when using such an object as the search parameter.
277 * @param MessageSpecifier|string $source Message key or object to search for
278 * @param MessageSpecifier|string $dest Replacement message key or object
279 * @return bool Return true if the replacement was done, false otherwise.
281 public function replaceMessage( $source, $dest ) {
284 foreach ( $this->errors
as $index => $error ) {
285 if ( $error['message'] === $source ) {
286 $this->errors
[$index]['message'] = $dest;
297 public function __toString() {
298 $status = $this->isOK() ?
"OK" : "Error";
299 if ( count( $this->errors
) ) {
300 $errorcount = "collected " . ( count( $this->errors
) ) . " error(s) on the way";
302 $errorcount = "no errors detected";
304 if ( isset( $this->value
) ) {
305 $valstr = gettype( $this->value
) . " value set";
306 if ( is_object( $this->value
) ) {
307 $valstr .= "\"" . get_class( $this->value
) . "\" instance";
310 $valstr = "no value set";
312 $out = sprintf( "<%s, %s, %s>",
317 if ( count( $this->errors
) > 0 ) {
318 $hdr = sprintf( "+-%'-4s-+-%'-25s-+-%'-40s-+\n", "", "", "" );
322 foreach ( $this->errors
as $error ) {
323 if ( $error['message'] instanceof MessageSpecifier
) {
324 $key = $error['message']->getKey();
325 $params = $error['message']->getParams();
326 } elseif ( $error['params'] ) {
327 $key = $error['message'];
328 $params = $error['params'];
330 $key = $error['message'];
334 $out .= sprintf( "| %4d | %-25.25s | %-40.40s |\n",
337 implode( " ", $params )