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