overtrue /
wechat
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the overtrue/wechat. |
||
| 5 | * |
||
| 6 | * (c) overtrue <[email protected]> |
||
| 7 | * |
||
| 8 | * This source file is subject to the MIT license that is bundled |
||
| 9 | * with this source code in the file LICENSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace EasyWeChat\Kernel\Messages; |
||
| 13 | |||
| 14 | use EasyWeChat\Kernel\Contracts\MessageInterface; |
||
| 15 | use EasyWeChat\Kernel\Exceptions\RuntimeException; |
||
| 16 | use EasyWeChat\Kernel\Support\XML; |
||
| 17 | use EasyWeChat\Kernel\Traits\HasAttributes; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class Messages. |
||
| 21 | */ |
||
| 22 | abstract class Message implements MessageInterface |
||
| 23 | { |
||
| 24 | use HasAttributes; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 25 | |||
| 26 | public const TEXT = 2; |
||
| 27 | public const IMAGE = 4; |
||
| 28 | public const VOICE = 8; |
||
| 29 | public const VIDEO = 16; |
||
| 30 | public const SHORT_VIDEO = 32; |
||
| 31 | public const LOCATION = 64; |
||
| 32 | public const LINK = 128; |
||
| 33 | public const DEVICE_EVENT = 256; |
||
| 34 | public const DEVICE_TEXT = 512; |
||
| 35 | public const FILE = 1024; |
||
| 36 | public const TEXT_CARD = 2048; |
||
| 37 | public const TRANSFER = 4096; |
||
| 38 | public const EVENT = 1048576; |
||
| 39 | public const MINIPROGRAM_PAGE = 2097152; |
||
| 40 | public const MINIPROGRAM_NOTICE = 4194304; |
||
| 41 | public const ALL = self::TEXT | self::IMAGE | self::VOICE | self::VIDEO | self::SHORT_VIDEO | self::LOCATION | self::LINK |
||
| 42 | | self::DEVICE_EVENT | self::DEVICE_TEXT | self::FILE | self::TEXT_CARD | self::TRANSFER | self::EVENT |
||
| 43 | | self::MINIPROGRAM_PAGE | self::MINIPROGRAM_NOTICE; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $type; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $id; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $to; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $from; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $properties = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $jsonAliases = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Message constructor. |
||
| 77 | * |
||
| 78 | * @param array $attributes |
||
| 79 | */ |
||
| 80 | 64 | public function __construct(array $attributes = []) |
|
| 81 | { |
||
| 82 | 64 | $this->setAttributes($attributes); |
|
| 83 | 64 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Return type name message. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 32 | public function getType(): string |
|
| 91 | { |
||
| 92 | 32 | return $this->type; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $type |
||
| 97 | */ |
||
| 98 | 10 | public function setType(string $type) |
|
| 99 | { |
||
| 100 | 10 | $this->type = $type; |
|
| 101 | 10 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Magic getter. |
||
| 105 | * |
||
| 106 | * @param string $property |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | 27 | public function __get($property) |
|
| 111 | { |
||
| 112 | 27 | if (property_exists($this, $property)) { |
|
| 113 | 1 | return $this->$property; |
|
| 114 | } |
||
| 115 | |||
| 116 | 27 | return $this->getAttribute($property); |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Magic setter. |
||
| 121 | * |
||
| 122 | * @param string $property |
||
| 123 | * @param mixed $value |
||
| 124 | * |
||
| 125 | * @return Message |
||
| 126 | */ |
||
| 127 | 3 | public function __set($property, $value) |
|
| 128 | { |
||
| 129 | 3 | if (property_exists($this, $property)) { |
|
| 130 | 1 | $this->$property = $value; |
|
| 131 | } else { |
||
| 132 | 3 | $this->setAttribute($property, $value); |
|
| 133 | } |
||
| 134 | |||
| 135 | 3 | return $this; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $appends |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | 2 | public function transformForJsonRequestWithoutType(array $appends = []) |
|
| 144 | { |
||
| 145 | 2 | return $this->transformForJsonRequest($appends, false); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param array $appends |
||
| 150 | * @param bool $withType |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | * |
||
| 154 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 155 | */ |
||
| 156 | 20 | public function transformForJsonRequest(array $appends = [], $withType = true): array |
|
| 157 | { |
||
| 158 | 20 | if (!$withType) { |
|
| 159 | 2 | return $this->propertiesToArray([], $this->jsonAliases); |
|
| 160 | } |
||
| 161 | 18 | $messageType = $this->getType(); |
|
| 162 | 18 | $data = array_merge(['msgtype' => $messageType], $appends); |
|
| 163 | |||
| 164 | 18 | $data[$messageType] = array_merge($data[$messageType] ?? [], $this->propertiesToArray([], $this->jsonAliases)); |
|
| 165 | |||
| 166 | 17 | return $data; |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $appends |
||
| 171 | * @param bool $returnAsArray |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 3 | public function transformToXml(array $appends = [], bool $returnAsArray = false): string |
|
| 176 | { |
||
| 177 | 3 | $data = array_merge(['MsgType' => $this->getType()], $this->toXmlArray(), $appends); |
|
| 178 | |||
| 179 | 3 | return $returnAsArray ? $data : XML::build($data); |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param array $data |
||
| 184 | * @param array $aliases |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | * |
||
| 188 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 189 | */ |
||
| 190 | 17 | protected function propertiesToArray(array $data, array $aliases = []): array |
|
| 191 | { |
||
| 192 | 17 | $this->checkRequiredAttributes(); |
|
| 193 | |||
| 194 | 16 | foreach ($this->attributes as $property => $value) { |
|
| 195 | 16 | if (is_null($value) && !$this->isRequired($property)) { |
|
| 196 | 1 | continue; |
|
| 197 | } |
||
| 198 | 16 | $alias = array_search($property, $aliases, true); |
|
| 199 | |||
| 200 | 16 | $data[$alias ?: $property] = $this->get($property); |
|
| 201 | } |
||
| 202 | |||
| 203 | 16 | return $data; |
|
| 204 | } |
||
| 205 | |||
| 206 | 1 | public function toXmlArray() |
|
| 207 | { |
||
| 208 | 1 | throw new RuntimeException(sprintf('Class "%s" cannot support transform to XML message.', __CLASS__)); |
|
| 209 | } |
||
| 210 | } |
||
| 211 |