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