Message::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tasksuki\Component\Message;
4
5
use JsonSerializable;
6
7
/**
8
 * Class Message
9
 *
10
 * @package Tasksuki\Component\Message
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class Message implements JsonSerializable
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * @var array
22
     */
23
    protected $data;
24
25 4
    public function __construct()
26
    {
27 4
        $this->data = [];
28 4
    }
29
30
    /**
31
     * @return string
32
     */
33 3
    public function getName(): string
34
    {
35 3
        return $this->name;
36
    }
37
38
    /**
39
     * @param string $name
40
     *
41
     * @return Message
42
     */
43 3
    public function setName(string $name): Message
44
    {
45 3
        $this->name = $name;
46
47 3
        return $this;
48
    }
49
50
    /**
51
     * @return array
52
     */
53 3
    public function getData(): array
54
    {
55 3
        return $this->data;
56
    }
57
58
    /**
59
     * @param array $data
60
     *
61
     * @return Message
62
     */
63 3
    public function setData(array $data): Message
64
    {
65 3
        $this->data = $data;
66
67 3
        return $this;
68
    }
69
70
    /**
71
     * Returns Message serialized to array for custom serializers
72
     *
73
     * @return array
74
     */
75 2
    public function toArray(): array
76
    {
77
        return [
78 2
            'name' => $this->getName(),
79 2
            'data' => $this->getData(),
80
        ];
81
    }
82
83
    /**
84
     * Creates Message object from serialized array for custom serializers
85
     *
86
     * @param array $data
87
     *
88
     * @return Message
89
     */
90 1
    public static function fromArray(array $data): Message
91
    {
92 1
        $message = new Message();
93
94
        return $message
95 1
            ->setName($data['name'])
96 1
            ->setData($data['data']);
97
    }
98
99
    /**
100
     * @return array
101
     */
102 1
    public function jsonSerialize(): array
103
    {
104 1
        return $this->toArray();
105
    }
106
}