1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Application; |
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 Topic Name identifies the information channel to which payload data is published. |
16
|
|
|
* |
17
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718106 |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $topicName = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The QoS lvl, choose between 0 and 2. |
24
|
|
|
* |
25
|
|
|
* NOTE: Setting a QoS level where it is not needed (basically anything apart from a subscription) will have no |
26
|
|
|
* effect at all, as the QoS level is set not on a Topic level, but on a Message level instead. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $qosLevel = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Topic constructor. |
34
|
|
|
* @param string $topicName |
35
|
|
|
* @param int $qosLevel |
36
|
|
|
* @throws \OutOfBoundsException |
37
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidQoSLevel |
38
|
|
|
* @throws \InvalidArgumentException |
39
|
|
|
*/ |
40
|
19 |
|
public function __construct(string $topicName, int $qosLevel = 0) |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
19 |
|
->setTopicName($topicName) |
44
|
18 |
|
->setQoSLevel($qosLevel); |
45
|
17 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Contains the name of the Topic Filter |
49
|
|
|
* |
50
|
|
|
* @param string $topicName |
51
|
|
|
* @return Topic |
52
|
|
|
* @throws \OutOfBoundsException |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
*/ |
55
|
19 |
|
private function setTopicName(string $topicName): self |
56
|
|
|
{ |
57
|
19 |
|
if ($topicName === '') { |
58
|
1 |
|
throw new \InvalidArgumentException('Topic name must be at least 1 character long'); |
59
|
|
|
} |
60
|
|
|
|
61
|
18 |
|
if (\strlen($topicName) > 65535) { |
62
|
|
|
throw new \OutOfBoundsException('Topic name can not exceed 65535 bytes'); |
63
|
|
|
} |
64
|
|
|
|
65
|
18 |
|
$this->topicName = $topicName; |
66
|
|
|
|
67
|
18 |
|
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
|
18 |
|
private function setQoSLevel(int $qosLevel): self |
78
|
|
|
{ |
79
|
18 |
|
if ($qosLevel > 2 || $qosLevel < 0) { |
80
|
1 |
|
throw new InvalidQoSLevel('The provided QoS level is invalid'); |
81
|
|
|
} |
82
|
|
|
|
83
|
17 |
|
$this->qosLevel = $qosLevel; |
84
|
|
|
|
85
|
17 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
13 |
|
public function getTopicName(): string |
92
|
|
|
{ |
93
|
13 |
|
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
|
|
|
|