Message::propertiesToArray()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
1
<?php
2
3
namespace EasyIM\TencentIM\Kernel\Messages;
4
5
use EasyIM\Kernel\Contracts\MessageInterface;
6
use EasyIM\Kernel\Traits\HasAttributes;
7
8
/**
9
 * Class Messages.
10
 */
11
abstract class Message implements MessageInterface
12
{
13
    use HasAttributes;
0 ignored issues
show
Bug introduced by
The trait EasyIM\Kernel\Traits\HasAttributes requires the property $required which is not provided by EasyIM\TencentIM\Kernel\Messages\Message.
Loading history...
14
15
16
    /**
17
     * @var string
18
     */
19
    protected $type;
20
21
22
    /**
23
     * @var array
24
     */
25
    protected $properties = [];
26
27
    /**
28
     * Message constructor.
29
     *
30
     * @param array $attributes
31
     */
32 8
    public function __construct(array $attributes = [])
33
    {
34 8
        $this->setAttributes($attributes);
35 8
    }
36
37
    /**
38
     * Return type name message.
39
     *
40
     * @return string
41
     */
42 4
    public function getType(): string
43
    {
44 4
        return $this->type;
45
    }
46
47
    /**
48
     * @param string $type
49
     */
50 1
    public function setType(string $type)
51
    {
52 1
        $this->type = $type;
53 1
    }
54
55
    /**
56
     * Magic getter.
57
     *
58
     * @param string $property
59
     *
60
     * @return mixed
61
     */
62 5
    public function __get($property)
63
    {
64 5
        if (property_exists($this, $property)) {
65
            return $this->$property;
66
        }
67
68 5
        return $this->getAttribute($property);
69
    }
70
71
    /**
72
     * Magic setter.
73
     *
74
     * @param string $property
75
     * @param mixed  $value
76
     *
77
     * @return Message
78
     */
79 1
    public function __set($property, $value)
80
    {
81 1
        if (property_exists($this, $property)) {
82
            $this->$property = $value;
83
        } else {
84 1
            $this->setAttribute($property, $value);
85
        }
86
87 1
        return $this;
88
    }
89
90
    /**
91
     *
92
     * @param array $appends
93
     * @param bool  $isFlat
94
     *
95
     * @return array|array[]
96
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
97
     */
98 3
    public function transformToArray(array $appends = [], bool $isFlat = false): array
99
    {
100 3
        $messageType = $this->getType();
101 3
        $messageContent = array_merge($this->propertiesToArray([]), $appends);
102
103 2
        $message = ['MsgType' => $messageType, 'MsgContent' => $messageContent];
104
105 2
        return $isFlat ? $message : [$message];
106
    }
107
108
    /**
109
     *
110
     * @param array $data
111
     *
112
     * @return array
113
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
114
     */
115 3
    protected function propertiesToArray(array $data): array
116
    {
117 3
        $this->checkRequiredAttributes();
118
119 2
        foreach ($this->attributes as $property => $value) {
120 2
            if (is_null($value) && !$this->isRequired($property)) {
121
                continue;
122
            }
123
124 2
            $data[$property] = $this->get($property);
125
        }
126
127 2
        return $data;
128
    }
129
130
    /**
131
     *
132
     * @param array $appends
133
     *
134
     * @return string
135
     */
136
    public function transformToJson(array $appends = []): string
137
    {
138
        return json_encode($this->transformToArray($appends));
139
    }
140
}
141