|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\DataTypes; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* When the client wants to subscribe to a topic, this is done by adding a topic filter. |
|
9
|
|
|
*/ |
|
10
|
|
|
final class Topic |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The Topic Name identifies the information channel to which payload data is published. |
|
14
|
|
|
* |
|
15
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718106 |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $topicName; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The QoS lvl of this topic |
|
22
|
|
|
* |
|
23
|
|
|
* NOTE: Setting a QoS level where it is not needed will have no effect at all, as the QoS level is set not on a |
|
24
|
|
|
* Topic level, but on a Message level instead. |
|
25
|
|
|
* NOTE: The SUBSCRIBE Packet also specifies (for each Subscription) the maximum QoS with which the Server can send |
|
26
|
|
|
* Application Messages to the Client. So even if the server has QoS lvl 2 messages in the queue, it will send |
|
27
|
|
|
* them as QoS lvl 0 if we provide lvl 0 as the input for this function. Defaults to QoS lvl 2. |
|
28
|
|
|
* |
|
29
|
|
|
* @var QoSLevel |
|
30
|
|
|
*/ |
|
31
|
|
|
private $qosLevel = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Topic constructor. |
|
35
|
|
|
* @param string $topicName |
|
36
|
|
|
* @param QoSLevel $qosLevel |
|
37
|
|
|
* @throws \OutOfBoundsException |
|
38
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
|
39
|
|
|
* @throws \InvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
23 |
|
public function __construct(string $topicName, QoSLevel $qosLevel = null) |
|
42
|
|
|
{ |
|
43
|
23 |
|
if ($qosLevel === null) { |
|
44
|
|
|
// QoSLevel defaults at 2 (Enable maximum by default) |
|
45
|
21 |
|
$qosLevel = new QoSLevel(2); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this |
|
49
|
23 |
|
->setTopicName($topicName) |
|
50
|
21 |
|
->setQoSLevel($qosLevel); |
|
51
|
21 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Contains the name of the Topic Filter |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $topicName |
|
57
|
|
|
* @return Topic |
|
58
|
|
|
* @throws \OutOfBoundsException |
|
59
|
|
|
* @throws \InvalidArgumentException |
|
60
|
|
|
*/ |
|
61
|
23 |
|
private function setTopicName(string $topicName): self |
|
62
|
|
|
{ |
|
63
|
23 |
|
if ($topicName === '') { |
|
64
|
1 |
|
throw new \InvalidArgumentException('Topic name must be at least 1 character long'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
22 |
|
if (\strlen($topicName) > 65535) { |
|
68
|
1 |
|
throw new \OutOfBoundsException('Topic name can not exceed 65535 bytes'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
21 |
|
$this->topicName = $topicName; |
|
72
|
21 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Requested QoS level is the maximum QoS level at which the Server can send Application Messages to the Client |
|
77
|
|
|
* |
|
78
|
|
|
* @param QoSLevel $qosLevel |
|
79
|
|
|
* @return Topic |
|
80
|
|
|
*/ |
|
81
|
21 |
|
private function setQoSLevel(QoSLevel $qosLevel): self |
|
82
|
|
|
{ |
|
83
|
21 |
|
$this->qosLevel = $qosLevel; |
|
84
|
|
|
|
|
85
|
21 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
17 |
|
public function getTopicName(): string |
|
92
|
|
|
{ |
|
93
|
17 |
|
return $this->topicName; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
7 |
|
public function getTopicQoSLevel(): int |
|
100
|
|
|
{ |
|
101
|
7 |
|
return $this->qosLevel->getQoSLevel(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|