Transformer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * Transformer.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. /**
  22. * Class Transformer.
  23. */
  24. class Transformer
  25. {
  26. /**
  27. * Message type.
  28. *
  29. * @var string
  30. */
  31. protected $msgType;
  32. /**
  33. * message.
  34. *
  35. * @var mixed
  36. */
  37. protected $message;
  38. /**
  39. * Transformer constructor.
  40. *
  41. * @param $msgType
  42. * @param $message
  43. */
  44. public function __construct($msgType, $message)
  45. {
  46. $this->msgType = $msgType;
  47. $this->message = $message;
  48. }
  49. /**
  50. * Transform message.
  51. *
  52. * @return array
  53. */
  54. public function transform()
  55. {
  56. $handle = sprintf('transform%s', ucfirst($this->msgType));
  57. return method_exists($this, $handle) ? $this->$handle($this->message) : [];
  58. }
  59. /**
  60. * Transform text message.
  61. *
  62. * @param string $message
  63. *
  64. * @return array
  65. */
  66. public function transformText($message)
  67. {
  68. return [
  69. 'text' => [
  70. 'content' => $message,
  71. ],
  72. 'msgtype' => 'text',
  73. ];
  74. }
  75. /**
  76. * Transform news message.
  77. *
  78. * @param string $message
  79. *
  80. * @return array
  81. */
  82. public function transformNews($message)
  83. {
  84. return [
  85. 'mpnews' => [
  86. 'media_id' => $message,
  87. ],
  88. 'msgtype' => 'mpnews',
  89. ];
  90. }
  91. /**
  92. * Transform image message.
  93. *
  94. * @param string $message
  95. *
  96. * @return array
  97. */
  98. public function transformImage($message)
  99. {
  100. return [
  101. 'image' => [
  102. 'media_id' => $message,
  103. ],
  104. 'msgtype' => 'image',
  105. ];
  106. }
  107. /**
  108. * Transform video message.
  109. *
  110. * @param array $message
  111. *
  112. * @return array
  113. *
  114. * @throws InvalidArgumentException
  115. */
  116. public function transformVideo(array $message)
  117. {
  118. if (3 !== count($message)) {
  119. throw new InvalidArgumentException('send message to openids, the message must be three arguments.');
  120. }
  121. return [
  122. 'mpvideo' => [
  123. 'media_id' => $message[0],
  124. 'title' => $message[1],
  125. 'description' => $message[2],
  126. ],
  127. 'msgtype' => 'mpvideo',
  128. ];
  129. }
  130. /**
  131. * Transform mpvideo message.
  132. *
  133. * @param string $message
  134. *
  135. * @return array
  136. */
  137. public function transformMpvideo($message)
  138. {
  139. return [
  140. 'mpvideo' => [
  141. 'media_id' => $message,
  142. ],
  143. 'msgtype' => 'mpvideo',
  144. ];
  145. }
  146. /**
  147. * Transform voice message.
  148. *
  149. * @param string $message
  150. *
  151. * @return array
  152. */
  153. public function transformVoice($message)
  154. {
  155. return [
  156. 'voice' => [
  157. 'media_id' => $message,
  158. ],
  159. 'msgtype' => 'voice',
  160. ];
  161. }
  162. /**
  163. * Transform card message.
  164. *
  165. * @param $message
  166. *
  167. * @return array
  168. */
  169. public function transformCard($message)
  170. {
  171. return [
  172. 'wxcard' => [
  173. 'card_id' => $message,
  174. ],
  175. 'msgtype' => 'wxcard',
  176. ];
  177. }
  178. }