ApiMessage.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  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.
  7. *
  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.
  12. *
  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
  17. *
  18. * @file
  19. */
  20. /**
  21. * Extension of Message implementing IApiMessage
  22. * @since 1.25
  23. * @ingroup API
  24. */
  25. class ApiMessage extends Message implements IApiMessage {
  26. use ApiMessageTrait;
  27. /**
  28. * Create an IApiMessage for the message
  29. *
  30. * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
  31. * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
  32. *
  33. * @param Message|RawMessage|array|string $msg
  34. * @param string|null $code
  35. * @param array|null $data
  36. * @return IApiMessage
  37. */
  38. public static function create( $msg, $code = null, array $data = null ) {
  39. if ( is_array( $msg ) ) {
  40. // From StatusValue
  41. if ( isset( $msg['message'] ) ) {
  42. if ( isset( $msg['params'] ) ) {
  43. $msg = array_merge( [ $msg['message'] ], $msg['params'] );
  44. } else {
  45. $msg = [ $msg['message'] ];
  46. }
  47. }
  48. // Weirdness that comes in sometimes, including the above
  49. if ( $msg[0] instanceof MessageSpecifier ) {
  50. $msg = $msg[0];
  51. }
  52. }
  53. if ( $msg instanceof IApiMessage ) {
  54. return $msg;
  55. } elseif ( $msg instanceof RawMessage ) {
  56. return new ApiRawMessage( $msg, $code, $data );
  57. } else {
  58. return new ApiMessage( $msg, $code, $data );
  59. }
  60. }
  61. /**
  62. * @param Message|string|array $msg
  63. * - Message: is cloned
  64. * - array: first element is $key, rest are $params to Message::__construct
  65. * - string: passed to Message::__construct
  66. * @param string|null $code
  67. * @param array|null $data
  68. */
  69. public function __construct( $msg, $code = null, array $data = null ) {
  70. if ( $msg instanceof Message ) {
  71. foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
  72. if ( isset( $msg->$key ) ) {
  73. $this->$key = $msg->$key;
  74. }
  75. }
  76. } elseif ( is_array( $msg ) ) {
  77. $key = array_shift( $msg );
  78. parent::__construct( $key, $msg );
  79. } else {
  80. parent::__construct( $msg );
  81. }
  82. $this->setApiCode( $code, $data );
  83. }
  84. }