AbstractMessage.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Message;
  7. /**
  8. * Abstract message model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractMessage implements MessageInterface
  14. {
  15. /**
  16. * @var string
  17. */
  18. protected $text;
  19. /**
  20. * @var string
  21. */
  22. protected $identifier;
  23. /**
  24. * @var bool
  25. */
  26. protected $isSticky = false;
  27. /**
  28. * @var array
  29. */
  30. protected $data;
  31. /**
  32. * @param string $text
  33. */
  34. public function __construct(
  35. $text = null
  36. ) {
  37. $this->text = $text;
  38. }
  39. /**
  40. * Getter message type
  41. *
  42. * @return string
  43. */
  44. abstract public function getType();
  45. /**
  46. * Getter for text of message
  47. *
  48. * @return string
  49. */
  50. public function getText()
  51. {
  52. return (string)$this->text;
  53. }
  54. /**
  55. * Setter message text
  56. *
  57. * @param string $text
  58. * @return $this
  59. */
  60. public function setText($text)
  61. {
  62. $this->text = $text;
  63. return $this;
  64. }
  65. /**
  66. * Setter message identifier
  67. *
  68. * @param string $identifier
  69. * @return $this
  70. */
  71. public function setIdentifier($identifier)
  72. {
  73. $this->identifier = $identifier;
  74. return $this;
  75. }
  76. /**
  77. * Getter message identifier
  78. *
  79. * @return string
  80. */
  81. public function getIdentifier()
  82. {
  83. return $this->identifier;
  84. }
  85. /**
  86. * Setter for flag. Whether message is sticky
  87. *
  88. * @param bool $isSticky
  89. * @return $this
  90. */
  91. public function setIsSticky($isSticky = true)
  92. {
  93. $this->isSticky = $isSticky;
  94. return $this;
  95. }
  96. /**
  97. * Getter for flag. Whether message is sticky
  98. *
  99. * @return bool
  100. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  101. */
  102. public function getIsSticky()
  103. {
  104. return $this->isSticky;
  105. }
  106. /**
  107. * Retrieve message as a string
  108. *
  109. * @return string
  110. */
  111. public function toString()
  112. {
  113. $out = $this->getType() . ': ' . $this->getIdentifier() . ': ' . $this->getText();
  114. return $out;
  115. }
  116. /**
  117. * Sets message data
  118. *
  119. * @param array $data
  120. * @return $this
  121. * @throws \InvalidArgumentException
  122. */
  123. public function setData(array $data = [])
  124. {
  125. array_walk_recursive(
  126. $data,
  127. function ($element) {
  128. if (is_object($element) && !$element instanceof \Serializable) {
  129. throw new \InvalidArgumentException('Only serializable content is allowed.');
  130. }
  131. }
  132. );
  133. $this->data = $data;
  134. return $this;
  135. }
  136. /**
  137. * Returns message data
  138. *
  139. * @return array
  140. */
  141. public function getData()
  142. {
  143. return (array)$this->data;
  144. }
  145. }