1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\DataTypes; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\MessageTooBig; |
8
|
|
|
|
9
|
|
|
final class Message |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* This field indicates the level of assurance for delivery of an Application Message. Can be 0, 1 or 2 |
13
|
|
|
* |
14
|
|
|
* 0: At most once delivery (default) |
15
|
|
|
* 1: At least once delivery |
16
|
|
|
* 2: Exactly once delivery |
17
|
|
|
* |
18
|
|
|
* @var QoSLevel |
19
|
|
|
*/ |
20
|
|
|
private $qosLevel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $payload; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a Server, the Server MUST store the |
29
|
|
|
* Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its |
30
|
|
|
* topic name |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $isRetained = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The Topic Name identifies the information channel to which payload data is published |
37
|
|
|
* @var Topic |
38
|
|
|
*/ |
39
|
|
|
private $topic; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Message constructor. |
43
|
|
|
* @param string $payload |
44
|
|
|
* @param Topic $topic |
45
|
|
|
* @throws \unreal4u\MQTT\Exceptions\MessageTooBig |
46
|
|
|
*/ |
47
|
16 |
|
public function __construct(string $payload, Topic $topic) |
48
|
|
|
{ |
49
|
16 |
|
$this->topic = $topic; |
50
|
16 |
|
if (mb_strlen($payload) > 65535) { |
51
|
1 |
|
throw new MessageTooBig('Message payload can not exceed 65535 characters!'); |
52
|
|
|
} |
53
|
|
|
|
54
|
15 |
|
$this->payload = $payload; |
55
|
15 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets the QoS level to the indicated value. Must be 0, 1 or 2. |
59
|
|
|
* |
60
|
|
|
* @param QoSLevel $level |
61
|
|
|
* @return Message |
62
|
|
|
*/ |
63
|
6 |
|
public function setQoSLevel(QosLevel $level): Message |
64
|
|
|
{ |
65
|
6 |
|
$this->qosLevel = $level; |
66
|
6 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets the retain flag to the given value |
71
|
|
|
* |
72
|
|
|
* @param bool $flag Set to true if message should be retained, false otherwise (default) |
73
|
|
|
* @return Message |
74
|
|
|
*/ |
75
|
2 |
|
public function setRetainFlag(bool $flag): Message |
76
|
|
|
{ |
77
|
2 |
|
$this->isRetained = $flag; |
78
|
2 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
public function getPayload(): string |
82
|
|
|
{ |
83
|
7 |
|
return $this->payload; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the topic name |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
9 |
|
public function getTopicName(): string |
92
|
|
|
{ |
93
|
9 |
|
return $this->topic->getTopicName(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the current QoS level |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
101
|
|
|
*/ |
102
|
12 |
|
public function getQoSLevel(): int |
103
|
|
|
{ |
104
|
12 |
|
if ($this->qosLevel === null) { |
105
|
|
|
// QoSLevel defaults at 0 |
106
|
6 |
|
$this->qosLevel = new QoSLevel(0); |
107
|
|
|
} |
108
|
12 |
|
return $this->qosLevel->getQoSLevel(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets the set retain flag |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
9 |
|
public function isRetained(): bool |
117
|
|
|
{ |
118
|
9 |
|
return $this->isRetained; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|