Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiMessage
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
11
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21use MediaWiki\Language\RawMessage;
22
23/**
24 * Extension of Message implementing IApiMessage
25 * @newable
26 * @since 1.25
27 * @ingroup API
28 */
29class ApiMessage extends Message implements IApiMessage {
30    use ApiMessageTrait;
31
32    /**
33     * Create an IApiMessage for the message
34     *
35     * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
36     * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
37     *
38     * @stable to call
39     * @param MessageSpecifier|array|string $msg
40     * @param string|null $code
41     * @param array|null $data
42     * @return IApiMessage
43     * @param-taint $msg tainted
44     */
45    public static function create( $msg, $code = null, array $data = null ) {
46        if ( is_array( $msg ) ) {
47            // From StatusValue
48            if ( isset( $msg['message'] ) ) {
49                $msg = [ $msg['message'], ...$msg['params'] ?? [] ];
50            }
51
52            // Weirdness that comes in sometimes, including the above
53            if ( $msg[0] instanceof MessageSpecifier ) {
54                $msg = $msg[0];
55            }
56        }
57
58        if ( $msg instanceof IApiMessage ) {
59            return $msg;
60        } elseif ( $msg instanceof RawMessage ) {
61            return new ApiRawMessage( $msg, $code, $data );
62        } else {
63            return new ApiMessage( $msg, $code, $data );
64        }
65    }
66
67    /**
68     * @param MessageSpecifier|string|array $msg
69     *  - Message: is cloned
70     *  - array: first element is $key, rest are $params to Message::__construct
71     *  - string, any other MessageSpecifier: passed to Message::__construct
72     * @param string|null $code
73     * @param array|null $data
74     */
75    public function __construct( $msg, $code = null, array $data = null ) {
76        if ( $msg instanceof Message ) {
77            foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
78                if ( isset( $msg->$key ) ) {
79                    $this->$key = $msg->$key;
80                }
81            }
82        } elseif ( is_array( $msg ) ) {
83            $key = array_shift( $msg );
84            parent::__construct( $key, $msg );
85        } else {
86            parent::__construct( $msg );
87        }
88        $this->setApiCode( $code, $data );
89    }
90}