Passed
Push — master ( b682e4...9aa869 )
by Carlos
09:31 queued 11s
created

src/Kernel/Messages/Message.php (1 issue)

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