MediaWiki  master
ApiMessage.php
Go to the documentation of this file.
1 <?php
27 class ApiMessage extends Message implements IApiMessage {
28  use ApiMessageTrait;
29 
43  public static function create( $msg, $code = null, array $data = null ) {
44  if ( is_array( $msg ) ) {
45  // From StatusValue
46  if ( isset( $msg['message'] ) ) {
47  if ( isset( $msg['params'] ) ) {
48  $msg = array_merge( [ $msg['message'] ], $msg['params'] );
49  } else {
50  $msg = [ $msg['message'] ];
51  }
52  }
53 
54  // Weirdness that comes in sometimes, including the above
55  if ( $msg[0] instanceof MessageSpecifier ) {
56  $msg = $msg[0];
57  }
58  }
59 
60  if ( $msg instanceof IApiMessage ) {
61  return $msg;
62  } elseif ( $msg instanceof RawMessage ) {
63  return new ApiRawMessage( $msg, $code, $data );
64  } else {
65  return new ApiMessage( $msg, $code, $data );
66  }
67  }
68 
77  public function __construct( $msg, $code = null, array $data = null ) {
78  if ( $msg instanceof Message ) {
79  foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
80  if ( isset( $msg->$key ) ) {
81  $this->$key = $msg->$key;
82  }
83  }
84  } elseif ( is_array( $msg ) ) {
85  $key = array_shift( $msg );
86  parent::__construct( $key, $msg );
87  } else {
88  parent::__construct( $msg );
89  }
90  $this->setApiCode( $code, $data );
91  }
92 }
MessageSpecifier
Stable for implementing.
Definition: MessageSpecifier.php:24
ApiMessage\__construct
__construct( $msg, $code=null, array $data=null)
Definition: ApiMessage.php:77
IApiMessage
Interface for messages with machine-readable data for use by the API.
Definition: IApiMessage.php:39
ApiRawMessage
Extension of RawMessage implementing IApiMessage @newable.
Definition: ApiRawMessage.php:27
ApiMessage
Extension of Message implementing IApiMessage @newable.
Definition: ApiMessage.php:27
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:43
IApiMessage\setApiCode
setApiCode( $code, array $data=null)
Sets the machine-readable code for use by the API.
Message\$key
string $key
The message key.
Definition: Message.php:203
ApiMessageTrait
trait ApiMessageTrait
Trait to implement the IApiMessage interface for Message subclasses.
Definition: ApiMessageTrait.php:28
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:161
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35