1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Internals; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\InvalidResponseType; |
8
|
|
|
use unreal4u\MQTT\Utilities; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait ReadableContent |
12
|
|
|
* @package unreal4u\MQTT\Internals |
13
|
|
|
*/ |
14
|
|
|
trait ReadableContent |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Length of variable header |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $variableHeaderSize = 0; |
21
|
|
|
|
22
|
3 |
|
final public function populate(string $rawMQTTHeaders): ReadableContentInterface |
23
|
|
|
{ |
24
|
|
|
//var_dump(base64_encode($rawMQTTHeaders)); // For now: make it a bit easier to create unit tests |
25
|
|
|
$this |
26
|
3 |
|
->checkControlPacketValue(\ord($rawMQTTHeaders[0]) >> 4) |
27
|
2 |
|
->fillObject($rawMQTTHeaders); |
28
|
|
|
|
29
|
2 |
|
return $this; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
final public function checkControlPacketValue(int $controlPacketValue): ReadableContentInterface |
33
|
|
|
{ |
34
|
|
|
// Check whether the first byte corresponds to the expected control packet value |
35
|
3 |
|
if (static::CONTROL_PACKET_VALUE !== $controlPacketValue) { |
|
|
|
|
36
|
1 |
|
throw new InvalidResponseType(sprintf( |
37
|
1 |
|
'Value of received value does not correspond to response (Expected: %d, Actual: %d)', |
38
|
1 |
|
static::CONTROL_PACKET_VALUE, |
39
|
1 |
|
$controlPacketValue |
40
|
|
|
)); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
return $this; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Extracts the packet identifier from the raw headers |
48
|
|
|
* |
49
|
|
|
* @param string $rawMQTTHeaders |
50
|
|
|
* @return int |
51
|
|
|
* @throws \OutOfRangeException |
52
|
|
|
*/ |
53
|
1 |
|
private function extractPacketIdentifier(string $rawMQTTHeaders): int |
54
|
|
|
{ |
55
|
1 |
|
return Utilities::convertBinaryStringToNumber($rawMQTTHeaders{2} . $rawMQTTHeaders{3}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Any class can overwrite the default behaviour |
60
|
|
|
* @param string $rawMQTTHeaders |
61
|
|
|
* @return ReadableContentInterface |
62
|
|
|
*/ |
63
|
1 |
|
public function fillObject(string $rawMQTTHeaders): ReadableContentInterface |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
return $this; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Any class can overwrite the default behaviour |
70
|
|
|
* @param ClientInterface $client |
71
|
|
|
* @param WritableContentInterface $originalRequest |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|