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