1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Application; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\DataTypes\QoSLevel; |
8
|
|
|
use unreal4u\MQTT\DataTypes\TopicName; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* When the client wants to subscribe to a topic, this is done by adding a topic filter. |
12
|
|
|
*/ |
13
|
|
|
final class Topic |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The Topic Name identifies the information channel to which payload data is published. |
17
|
|
|
* |
18
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718106 |
19
|
|
|
* @var TopicName |
20
|
|
|
*/ |
21
|
|
|
private $topicName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The QoS lvl of this topic |
25
|
|
|
* |
26
|
|
|
* NOTE: Setting a QoS level where it is not needed (basically anything apart from a subscription) will have no |
27
|
|
|
* effect at all, as the QoS level is set not on a Topic level, but on a Message level instead. |
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
|
|
|
public function __construct(TopicName $topicName, QoSLevel $qosLevel = null) |
42
|
|
|
{ |
43
|
|
|
if ($qosLevel === null) { |
44
|
|
|
$qosLevel = new QoSLevel(0); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this |
48
|
|
|
->setTopicName($topicName) |
49
|
|
|
->setQoSLevel($qosLevel); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Contains the name of the Topic Filter |
54
|
|
|
* |
55
|
|
|
* @param string $topicName |
56
|
|
|
* @return Topic |
57
|
|
|
* @throws \OutOfBoundsException |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
private function setTopicName(TopicName $topicName): self |
61
|
|
|
{ |
62
|
|
|
$this->topicName = $topicName; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Requested QoS level is the maximum QoS level at which the Server can send Application Messages to the Client |
68
|
|
|
* |
69
|
|
|
* @param int $qosLevel |
70
|
|
|
* @return Topic |
71
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
72
|
|
|
*/ |
73
|
|
|
private function setQoSLevel(QoSLevel $qosLevel): self |
74
|
|
|
{ |
75
|
|
|
$this->qosLevel = $qosLevel; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getTopicName(): string |
84
|
|
|
{ |
85
|
|
|
return $this->topicName->getTopicName(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getTopicQoSLevel(): int |
92
|
|
|
{ |
93
|
|
|
return $this->qosLevel->getQoSLevel(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|