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