Passed
Push — master ( a2a2c3...5dc036 )
by Carlos
03:01
created

Message::toXmlArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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;
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 ALL = 1049598;
40
41
    /**
42
     * @var string
43
     */
44
    protected $type;
45
46
    /**
47
     * @var int
48
     */
49
    protected $id;
50
51
    /**
52
     * @var string
53
     */
54
    protected $to;
55
56
    /**
57
     * @var string
58
     */
59
    protected $from;
60
61
    /**
62
     * @var array
63
     */
64
    protected $properties = [];
65
66
    /**
67
     * @var array
68
     */
69
    protected $jsonAliases = [];
70
71
    /**
72
     * Message constructor.
73
     *
74
     * @param array $attributes
75
     */
76
    public function __construct(array $attributes = [])
77
    {
78
        $this->setAttributes($attributes);
79
    }
80
81
    /**
82
     * Return type name message.
83
     *
84
     * @return string
85
     */
86
    public function getType(): string
87
    {
88
        return $this->type;
89
    }
90
91
    /**
92
     * @param string $type
93
     */
94
    public function setType(string $type)
95
    {
96
        $this->type = $type;
97
    }
98
99
    /**
100
     * Magic getter.
101
     *
102
     * @param string $property
103
     *
104
     * @return mixed
105
     */
106
    public function __get($property)
107
    {
108
        if (property_exists($this, $property)) {
109
            return $this->$property;
110
        }
111
112
        return $this->getAttribute($property);
113
    }
114
115
    /**
116
     * Magic setter.
117
     *
118
     * @param string $property
119
     * @param mixed  $value
120
     *
121
     * @return Message
122
     */
123
    public function __set($property, $value)
124
    {
125
        if (property_exists($this, $property)) {
126
            $this->$property = $value;
127
        } else {
128
            $this->setAttribute($property, $value);
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param array $appends
136
     *
137
     * @return array
138
     */
139
    public function transformForJsonRequestWithoutType(array $appends = [])
140
    {
141
        return $this->transformForJsonRequest($appends, false);
142
    }
143
144
    /**
145
     * @param array $appends
146
     * @param bool  $withType
147
     *
148
     * @return array
149
     */
150
    public function transformForJsonRequest(array $appends = [], $withType = true): array
151
    {
152
        if (!$withType) {
153
            return $this->propertiesToArray([], $this->jsonAliases);
154
        }
155
        $messageType = $this->getType();
156
        $data = array_merge(['msgtype' => $messageType], $appends);
157
158
        $data[$messageType] = array_merge($data[$messageType] ?? [], $this->propertiesToArray([], $this->jsonAliases));
159
160
        return $data;
161
    }
162
163
    /**
164
     * @param array $appends
165
     * @param bool  $returnAsArray
166
     *
167
     * @return string
168
     */
169
    public function transformToXml(array $appends = [], bool $returnAsArray = false): string
170
    {
171
        $data = array_merge(['MsgType' => $this->getType()], $this->toXmlArray(), $appends);
172
173
        return $returnAsArray ? $data : XML::build($data);
174
    }
175
176
    /**
177
     * @param array $data
178
     * @param array $aliases
179
     *
180
     * @return array|mixed
181
     */
182
    protected function propertiesToArray(array $data, array $aliases = []): array
183
    {
184
        $this->checkRequiredAttributes();
185
186
        foreach ($this->attributes as $property => $value) {
187
            if (is_null($value) && !$this->isRequired($property)) {
188
                continue;
189
            }
190
            $alias = array_search($property, $aliases, true);
191
192
            $data[$alias ?: $property] = $this->get($property);
193
        }
194
195
        return $data;
196
    }
197
198
    public function toXmlArray()
199
    {
200
        throw new BadMethodCallException(sprintf('Class "%s" cannot support transform to XML message.', __CLASS__));
201
    }
202
}
203