MessageBuilder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  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. /**
  11. * MessageBuilder.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Staff;
  20. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  21. use EasyWeChat\Core\Exceptions\RuntimeException;
  22. use EasyWeChat\Message\AbstractMessage;
  23. use EasyWeChat\Message\Raw as RawMessage;
  24. use EasyWeChat\Message\Text;
  25. /**
  26. * Class MessageBuilder.
  27. */
  28. class MessageBuilder
  29. {
  30. /**
  31. * Message to send.
  32. *
  33. * @var \EasyWeChat\Message\AbstractMessage;
  34. */
  35. protected $message;
  36. /**
  37. * Message target user open id.
  38. *
  39. * @var string
  40. */
  41. protected $to;
  42. /**
  43. * Message sender staff id.
  44. *
  45. * @var string
  46. */
  47. protected $account;
  48. /**
  49. * Staff instance.
  50. *
  51. * @var \EasyWeChat\Staff\Staff
  52. */
  53. protected $staff;
  54. /**
  55. * MessageBuilder constructor.
  56. *
  57. * @param \EasyWeChat\Staff\Staff $staff
  58. */
  59. public function __construct(Staff $staff)
  60. {
  61. $this->staff = $staff;
  62. }
  63. /**
  64. * Set message to send.
  65. *
  66. * @param string|AbstractMessage $message
  67. *
  68. * @return MessageBuilder
  69. *
  70. * @throws InvalidArgumentException
  71. */
  72. public function message($message)
  73. {
  74. if (is_string($message)) {
  75. $message = new Text(['content' => $message]);
  76. }
  77. $this->message = $message;
  78. return $this;
  79. }
  80. /**
  81. * Set staff account to send message.
  82. *
  83. * @param string $account
  84. *
  85. * @return MessageBuilder
  86. */
  87. public function by($account)
  88. {
  89. $this->account = $account;
  90. return $this;
  91. }
  92. /**
  93. * Set target user open id.
  94. *
  95. * @param string $openId
  96. *
  97. * @return MessageBuilder
  98. */
  99. public function to($openId)
  100. {
  101. $this->to = $openId;
  102. return $this;
  103. }
  104. /**
  105. * Send the message.
  106. *
  107. * @return bool
  108. *
  109. * @throws RuntimeException
  110. */
  111. public function send()
  112. {
  113. if (empty($this->message)) {
  114. throw new RuntimeException('No message to send.');
  115. }
  116. $transformer = new Transformer();
  117. if ($this->message instanceof RawMessage) {
  118. $message = $this->message->get('content');
  119. } else {
  120. $content = $transformer->transform($this->message);
  121. $message = [
  122. 'touser' => $this->to,
  123. ];
  124. if ($this->account) {
  125. $message['customservice'] = ['kf_account' => $this->account];
  126. }
  127. $message = array_merge($message, $content);
  128. }
  129. return $this->staff->send($message);
  130. }
  131. /**
  132. * Return property.
  133. *
  134. * @param $property
  135. *
  136. * @return mixed
  137. */
  138. public function __get($property)
  139. {
  140. if (property_exists($this, $property)) {
  141. return $this->$property;
  142. }
  143. }
  144. }