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 instantiateObject(string $rawMQTTHeaders, ClientInterface $client): bool |
23
|
|
|
{ |
24
|
|
|
//var_dump(base64_encode($rawMQTTHeaders)); // For now: make it a bit easier to create unit tests |
25
|
3 |
|
$this->checkControlPacketValue(\ord($rawMQTTHeaders[0]) >> 4); |
26
|
2 |
|
$this->fillObject($rawMQTTHeaders, $client); |
27
|
|
|
|
28
|
2 |
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Checks whether the control packet corresponds to this object |
33
|
|
|
* |
34
|
|
|
* @param int $controlPacketValue |
35
|
|
|
* @return bool |
36
|
|
|
* @throws \unreal4u\MQTT\Exceptions\InvalidResponseType |
37
|
|
|
*/ |
38
|
3 |
|
private function checkControlPacketValue(int $controlPacketValue): bool |
39
|
|
|
{ |
40
|
|
|
// Check whether the first byte corresponds to the expected control packet value |
41
|
3 |
|
if (static::CONTROL_PACKET_VALUE !== $controlPacketValue) { |
|
|
|
|
42
|
1 |
|
throw new InvalidResponseType(sprintf( |
43
|
1 |
|
'Value of received value does not correspond to response (Expected: %d, Actual: %d)', |
44
|
1 |
|
static::CONTROL_PACKET_VALUE, |
45
|
1 |
|
$controlPacketValue |
46
|
|
|
)); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* All classes must implement how to handle the object filling |
54
|
|
|
* @param string $rawMQTTHeaders |
55
|
|
|
* @param ClientInterface $client |
56
|
|
|
* @return ReadableContentInterface |
57
|
|
|
*/ |
58
|
|
|
abstract public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Any class can overwrite the default behaviour |
62
|
|
|
* @param ClientInterface $client |
63
|
|
|
* @param WritableContentInterface $originalRequest |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|