MessageBuilder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\Broadcast;
  20. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  21. use EasyWeChat\Core\Exceptions\RuntimeException;
  22. /**
  23. * Class MessageBuilder.
  24. */
  25. class MessageBuilder
  26. {
  27. /**
  28. * Message target user or group.
  29. *
  30. * @var mixed
  31. */
  32. protected $to;
  33. /**
  34. * Message type.
  35. *
  36. * @var string
  37. */
  38. protected $msgType;
  39. /**
  40. * Message.
  41. *
  42. * @var mixed
  43. */
  44. protected $message;
  45. /**
  46. * Message types.
  47. *
  48. * @var array
  49. */
  50. private $msgTypes = [
  51. Broadcast::MSG_TYPE_TEXT,
  52. Broadcast::MSG_TYPE_NEWS,
  53. Broadcast::MSG_TYPE_IMAGE,
  54. Broadcast::MSG_TYPE_VIDEO,
  55. Broadcast::MSG_TYPE_VOICE,
  56. Broadcast::MSG_TYPE_CARD,
  57. ];
  58. /**
  59. * Preview bys.
  60. *
  61. * @var array
  62. */
  63. private $previewBys = [
  64. Broadcast::PREVIEW_BY_OPENID,
  65. Broadcast::PREVIEW_BY_NAME,
  66. ];
  67. /**
  68. * Set message type.
  69. *
  70. * @param string $msgType
  71. *
  72. * @return MessageBuilder
  73. *
  74. * @throws InvalidArgumentException
  75. */
  76. public function msgType($msgType)
  77. {
  78. if (!in_array($msgType, $this->msgTypes, true)) {
  79. throw new InvalidArgumentException('This message type not exist.');
  80. }
  81. $this->msgType = $msgType;
  82. return $this;
  83. }
  84. /**
  85. * Set message.
  86. *
  87. * @param string|array $message
  88. *
  89. * @return MessageBuilder
  90. */
  91. public function message($message)
  92. {
  93. $this->message = $message;
  94. return $this;
  95. }
  96. /**
  97. * Set target user or group.
  98. *
  99. * @param mixed $to
  100. *
  101. * @return MessageBuilder
  102. */
  103. public function to($to)
  104. {
  105. $this->to = $to;
  106. return $this;
  107. }
  108. /**
  109. * Build message.
  110. *
  111. * @return bool
  112. *
  113. * @throws RuntimeException
  114. */
  115. public function build()
  116. {
  117. if (empty($this->msgType)) {
  118. throw new RuntimeException('message type not exist.');
  119. }
  120. if (empty($this->message)) {
  121. throw new RuntimeException('No message content to send.');
  122. }
  123. // 群发视频消息给用户列表时,视频消息格式需要另外处理,具体见文档
  124. if (Broadcast::MSG_TYPE_VIDEO === $this->msgType) {
  125. if (is_array($this->message)) {
  126. $this->message = array_shift($this->message);
  127. }
  128. $this->msgType = 'mpvideo';
  129. }
  130. $content = (new Transformer($this->msgType, $this->message))->transform();
  131. $group = isset($this->to) ? $this->to : null;
  132. $message = array_merge($this->buildGroup($group), $content);
  133. return $message;
  134. }
  135. /**
  136. * Build preview message.
  137. *
  138. * @param string $by
  139. *
  140. * @return array
  141. *
  142. * @throws RuntimeException
  143. * @throws InvalidArgumentException
  144. */
  145. public function buildPreview($by)
  146. {
  147. if (!in_array($by, $this->previewBys, true)) {
  148. throw new InvalidArgumentException('This preview by not exist.');
  149. }
  150. if (empty($this->msgType)) {
  151. throw new RuntimeException('Message type not exist.');
  152. } elseif (Broadcast::MSG_TYPE_VIDEO === $this->msgType) {
  153. if (is_array($this->message)) {
  154. $this->message = array_shift($this->message);
  155. }
  156. $this->msgType = 'mpvideo';
  157. }
  158. if (empty($this->message)) {
  159. throw new RuntimeException('No message content to send.');
  160. }
  161. if (empty($this->to)) {
  162. throw new RuntimeException('No to.');
  163. }
  164. $content = (new Transformer($this->msgType, $this->message))->transform();
  165. $message = array_merge($this->buildTo($this->to, $by), $content);
  166. return $message;
  167. }
  168. /**
  169. * Build group.
  170. *
  171. * @param mixed $group
  172. *
  173. * @return array
  174. */
  175. private function buildGroup($group)
  176. {
  177. if (is_null($group)) {
  178. $group = [
  179. 'filter' => [
  180. 'is_to_all' => true,
  181. ],
  182. ];
  183. } elseif (is_array($group)) {
  184. $group = [
  185. 'touser' => $group,
  186. ];
  187. } else {
  188. $group = [
  189. 'filter' => [
  190. 'is_to_all' => false,
  191. 'group_id' => $group,
  192. ],
  193. ];
  194. }
  195. return $group;
  196. }
  197. /**
  198. * Build to.
  199. *
  200. * @param string $to
  201. * @param string $by
  202. *
  203. * @return array
  204. */
  205. private function buildTo($to, $by)
  206. {
  207. return [
  208. $by => $to,
  209. ];
  210. }
  211. /**
  212. * Return property.
  213. *
  214. * @param string $property
  215. *
  216. * @return mixed
  217. */
  218. public function __get($property)
  219. {
  220. if (property_exists($this, $property)) {
  221. return $this->$property;
  222. }
  223. }
  224. }