MediaWiki  1.34.0
ApiMessage.php
Go to the documentation of this file.
1 <?php
26 class ApiMessage extends Message implements IApiMessage {
27  use ApiMessageTrait;
28 
40  public static function create( $msg, $code = null, array $data = null ) {
41  if ( is_array( $msg ) ) {
42  // From StatusValue
43  if ( isset( $msg['message'] ) ) {
44  if ( isset( $msg['params'] ) ) {
45  $msg = array_merge( [ $msg['message'] ], $msg['params'] );
46  } else {
47  $msg = [ $msg['message'] ];
48  }
49  }
50 
51  // Weirdness that comes in sometimes, including the above
52  if ( $msg[0] instanceof MessageSpecifier ) {
53  $msg = $msg[0];
54  }
55  }
56 
57  if ( $msg instanceof IApiMessage ) {
58  return $msg;
59  } elseif ( $msg instanceof RawMessage ) {
60  return new ApiRawMessage( $msg, $code, $data );
61  } else {
62  return new ApiMessage( $msg, $code, $data );
63  }
64  }
65 
74  public function __construct( $msg, $code = null, array $data = null ) {
75  if ( $msg instanceof Message ) {
76  foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
77  if ( isset( $msg->$key ) ) {
78  $this->$key = $msg->$key;
79  }
80  }
81  } elseif ( is_array( $msg ) ) {
82  $key = array_shift( $msg );
83  parent::__construct( $key, $msg );
84  } else {
85  parent::__construct( $msg );
86  }
87  $this->setApiCode( $code, $data );
88  }
89 }
MessageSpecifier
Definition: MessageSpecifier.php:21
ApiMessage\__construct
__construct( $msg, $code=null, array $data=null)
Definition: ApiMessage.php:74
Message
IApiMessage
Interface for messages with machine-readable data for use by the API.
Definition: IApiMessage.php:39
ApiRawMessage
Extension of RawMessage implementing IApiMessage.
Definition: ApiRawMessage.php:26
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40
IApiMessage\setApiCode
setApiCode( $code, array $data=null)
Sets the machine-readable code for use by the API.
ApiMessageTrait
trait ApiMessageTrait
Trait to implement the IApiMessage interface for Message subclasses.
Definition: ApiMessageTrait.php:28
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34