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.
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.
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
22 * Extension of Message implementing IApiMessage
26 class ApiMessage
extends Message
implements IApiMessage
{
30 * Create an IApiMessage for the message
32 * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
33 * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
35 * @param Message|RawMessage|array|string $msg
36 * @param string|null $code
37 * @param array|null $data
40 public static function create( $msg, $code = null, array $data = null ) {
41 if ( is_array( $msg ) ) {
43 if ( isset( $msg['message'] ) ) {
44 if ( isset( $msg['params'] ) ) {
45 $msg = array_merge( [ $msg['message'] ], $msg['params'] );
47 $msg = [ $msg['message'] ];
51 // Weirdness that comes in sometimes, including the above
52 if ( $msg[0] instanceof MessageSpecifier
) {
57 if ( $msg instanceof IApiMessage
) {
59 } elseif ( $msg instanceof RawMessage
) {
60 return new ApiRawMessage( $msg, $code, $data );
62 return new ApiMessage( $msg, $code, $data );
67 * @param Message|string|array $msg
68 * - Message: is cloned
69 * - array: first element is $key, rest are $params to Message::__construct
70 * - string: passed to Message::__construct
71 * @param string|null $code
72 * @param array|null $data
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;
81 } elseif ( is_array( $msg ) ) {
82 $key = array_shift( $msg );
83 parent
::__construct( $key, $msg );
85 parent
::__construct( $msg );
87 $this->setApiCode( $code, $data );