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