Message.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the overtrue/easy-sms.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\EasySms;
  11. use Overtrue\EasySms\Contracts\GatewayInterface;
  12. use Overtrue\EasySms\Contracts\MessageInterface;
  13. /**
  14. * Class Message.
  15. */
  16. class Message implements MessageInterface
  17. {
  18. /**
  19. * @var array
  20. */
  21. protected $gateways = [];
  22. /**
  23. * @var string
  24. */
  25. protected $type;
  26. /**
  27. * @var string
  28. */
  29. protected $content;
  30. /**
  31. * @var string
  32. */
  33. protected $template;
  34. /**
  35. * @var array
  36. */
  37. protected $data = [];
  38. /**
  39. * Message constructor.
  40. *
  41. * @param array $attributes
  42. * @param string $type
  43. */
  44. public function __construct(array $attributes = [], $type = MessageInterface::TEXT_MESSAGE)
  45. {
  46. $this->type = $type;
  47. foreach ($attributes as $property => $value) {
  48. if (property_exists($this, $property)) {
  49. $this->$property = $value;
  50. }
  51. }
  52. }
  53. /**
  54. * Return the message type.
  55. *
  56. * @return string
  57. */
  58. public function getMessageType()
  59. {
  60. return $this->type;
  61. }
  62. /**
  63. * Return message content.
  64. *
  65. * @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
  66. *
  67. * @return string
  68. */
  69. public function getContent(GatewayInterface $gateway = null)
  70. {
  71. return is_callable($this->content) ? call_user_func($this->content, $gateway) : $this->content;
  72. }
  73. /**
  74. * Return the template id of message.
  75. *
  76. * @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
  77. *
  78. * @return string
  79. */
  80. public function getTemplate(GatewayInterface $gateway = null)
  81. {
  82. return is_callable($this->template) ? call_user_func($this->template, $gateway) : $this->template;
  83. }
  84. /**
  85. * @param $type
  86. *
  87. * @return $this
  88. */
  89. public function setType($type)
  90. {
  91. $this->type = $type;
  92. return $this;
  93. }
  94. /**
  95. * @param mixed $content
  96. *
  97. * @return $this
  98. */
  99. public function setContent($content)
  100. {
  101. $this->content = $content;
  102. return $this;
  103. }
  104. /**
  105. * @param mixed $template
  106. *
  107. * @return $this
  108. */
  109. public function setTemplate($template)
  110. {
  111. $this->template = $template;
  112. return $this;
  113. }
  114. /**
  115. * @param \Overtrue\EasySms\Contracts\GatewayInterface|null $gateway
  116. *
  117. * @return array
  118. */
  119. public function getData(GatewayInterface $gateway = null)
  120. {
  121. return is_callable($this->data) ? call_user_func($this->data, $gateway) : $this->data;
  122. }
  123. /**
  124. * @param array|callable $data
  125. *
  126. * @return $this
  127. */
  128. public function setData($data)
  129. {
  130. $this->data = $data;
  131. return $this;
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function getGateways()
  137. {
  138. return $this->gateways;
  139. }
  140. /**
  141. * @param array $gateways
  142. *
  143. * @return $this
  144. */
  145. public function setGateways(array $gateways)
  146. {
  147. $this->gateways = $gateways;
  148. return $this;
  149. }
  150. /**
  151. * @param $property
  152. *
  153. * @return string
  154. */
  155. public function __get($property)
  156. {
  157. if (property_exists($this, $property)) {
  158. return $this->$property;
  159. }
  160. }
  161. }