1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Protocol\Subscribe; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\InvalidQoSLevel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* When the client wants to subscribe to a topic, this is done by adding a topic filter. |
11
|
|
|
*/ |
12
|
|
|
final class Topic |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The 1st byte can contain some bits |
16
|
|
|
* |
17
|
|
|
* The order of these flags are: |
18
|
|
|
* |
19
|
|
|
* 7-6-5-4-3-2-1-0 |
20
|
|
|
* b'0-0-0-0-0-0-0-0' |
21
|
|
|
* |
22
|
|
|
* Bit 7-4: Control packet value ID (3 for PUBLISH) |
23
|
|
|
* Bit 3: Duplicate delivery of a PUBLISH Control Packet |
24
|
|
|
* Bit 2 & 1: PUBLISH Quality of Service |
25
|
|
|
* Bit 0: PUBLISH Retain flag |
26
|
|
|
* |
27
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.2_- |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $topicName = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The QoS lvl, choose between 0 and 2 |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $qosLevel = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Topic constructor. |
40
|
|
|
* @param string $topicName |
41
|
|
|
* @param int $qosLevel |
42
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
43
|
|
|
* @throws \InvalidArgumentException |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct(string $topicName, int $qosLevel = 0) |
46
|
|
|
{ |
47
|
|
|
$this |
48
|
5 |
|
->setTopicName($topicName) |
49
|
4 |
|
->setQoSLevel($qosLevel); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Contains the name of the Topic Filter |
54
|
|
|
* |
55
|
|
|
* @param string $topicName |
56
|
|
|
* @return Topic |
57
|
|
|
* @throws \InvalidArgumentException |
58
|
|
|
*/ |
59
|
5 |
|
private function setTopicName(string $topicName): Topic |
60
|
|
|
{ |
61
|
5 |
|
if ($topicName === '') { |
62
|
1 |
|
throw new \InvalidArgumentException('Topic name must be set'); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
$this->topicName = $topicName; |
66
|
|
|
|
67
|
4 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Requested QoS level is the maximum QoS level at which the Server can send Application Messages to the Client |
72
|
|
|
* |
73
|
|
|
* @param int $qosLevel |
74
|
|
|
* @return Topic |
75
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
76
|
|
|
*/ |
77
|
4 |
|
private function setQoSLevel(int $qosLevel): Topic |
78
|
|
|
{ |
79
|
4 |
|
if ($qosLevel > 2 || $qosLevel < 0) { |
80
|
1 |
|
throw new InvalidQoSLevel('The provided QoS level is invalid'); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
$this->qosLevel = $qosLevel; |
84
|
|
|
|
85
|
3 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
3 |
|
public function getTopicName(): string |
92
|
|
|
{ |
93
|
3 |
|
return $this->topicName; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
3 |
|
public function getTopicQoSLevel(): int |
100
|
|
|
{ |
101
|
3 |
|
return $this->qosLevel; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|