RawMessage.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * Variant of the Message class.
  22. *
  23. * Rather than treating the message key as a lookup
  24. * value (which is passed to the MessageCache and
  25. * translated as necessary), a RawMessage key is
  26. * treated as the actual message.
  27. *
  28. * All other functionality (parsing, escaping, etc.)
  29. * is preserved.
  30. *
  31. * @since 1.21
  32. */
  33. class RawMessage extends Message {
  34. /**
  35. * Call the parent constructor, then store the key as
  36. * the message.
  37. *
  38. * @see Message::__construct
  39. *
  40. * @param string $text Message to use.
  41. * @param array $params Parameters for the message.
  42. *
  43. * @throws InvalidArgumentException
  44. */
  45. public function __construct( $text, $params = [] ) {
  46. if ( !is_string( $text ) ) {
  47. throw new InvalidArgumentException( '$text must be a string' );
  48. }
  49. parent::__construct( $text, $params );
  50. // The key is the message.
  51. $this->message = $text;
  52. }
  53. /**
  54. * Fetch the message (in this case, the key).
  55. *
  56. * @return string
  57. */
  58. public function fetchMessage() {
  59. // Just in case the message is unset somewhere.
  60. if ( $this->message === null ) {
  61. $this->message = $this->key;
  62. }
  63. return $this->message;
  64. }
  65. }