|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace DaveRandom\LibLifxLan\Header; |
|
4
|
|
|
|
|
5
|
|
|
use DaveRandom\LibLifxLan\Exceptions\InvalidValueException; |
|
6
|
|
|
use const DaveRandom\LibLifxLan\UINT32_MAX; |
|
7
|
|
|
use const DaveRandom\LibLifxLan\UINT32_MIN; |
|
8
|
|
|
|
|
9
|
|
|
final class Frame |
|
10
|
|
|
{ |
|
11
|
|
|
public const WIRE_SIZE = 8; |
|
12
|
|
|
|
|
13
|
|
|
private $size; |
|
14
|
|
|
private $origin; |
|
15
|
|
|
private $tagged; |
|
16
|
|
|
private $addressable; |
|
17
|
|
|
private $protocolNo; |
|
18
|
|
|
private $source; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param int $size |
|
22
|
|
|
* @throws InvalidValueException |
|
23
|
|
|
*/ |
|
24
|
29 |
|
private function setSize(int $size): void |
|
25
|
|
|
{ |
|
26
|
29 |
|
if ($size < 0 || $size > 65535) { |
|
27
|
4 |
|
throw new InvalidValueException("Message size {$size} outside allowable range of 0 - 65535"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
25 |
|
$this->size = $size; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param int $origin |
|
35
|
|
|
* @throws InvalidValueException |
|
36
|
|
|
*/ |
|
37
|
25 |
|
private function setOrigin(int $origin): void |
|
38
|
|
|
{ |
|
39
|
25 |
|
if ($origin < 0 || $origin > 3) { |
|
40
|
4 |
|
throw new InvalidValueException("Message origin value {$origin} outside allowable range 0 - 3"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
21 |
|
$this->origin = $origin; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $protocolNo |
|
48
|
|
|
* @throws InvalidValueException |
|
49
|
|
|
*/ |
|
50
|
21 |
|
private function setProtocolNo(int $protocolNo): void |
|
51
|
|
|
{ |
|
52
|
21 |
|
if ($protocolNo < 0 || $protocolNo > 4095) { |
|
53
|
4 |
|
throw new InvalidValueException("Message protocol number {$protocolNo} outside allowable range 0 - 4095"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
17 |
|
$this->protocolNo = $protocolNo; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param int $source |
|
61
|
|
|
* @throws InvalidValueException |
|
62
|
|
|
*/ |
|
63
|
17 |
|
private function setSource(int $source): void |
|
64
|
|
|
{ |
|
65
|
17 |
|
if ($source < UINT32_MIN || $source > UINT32_MAX) { |
|
66
|
2 |
|
throw new InvalidValueException( |
|
67
|
2 |
|
"Message source value {$source} outside allowable range " . UINT32_MIN . " - " . UINT32_MAX |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
15 |
|
$this->source = $source; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param int $size |
|
76
|
|
|
* @param int $origin |
|
77
|
|
|
* @param bool $tagged |
|
78
|
|
|
* @param bool $addressable |
|
79
|
|
|
* @param int $protocolNo |
|
80
|
|
|
* @param int $source |
|
81
|
|
|
* @throws InvalidValueException |
|
82
|
|
|
*/ |
|
83
|
29 |
|
public function __construct(int $size, int $origin, bool $tagged, bool $addressable, int $protocolNo, int $source) |
|
84
|
|
|
{ |
|
85
|
29 |
|
$this->setSize($size); |
|
86
|
25 |
|
$this->setOrigin($origin); |
|
87
|
21 |
|
$this->tagged = $tagged; |
|
88
|
21 |
|
$this->addressable = $addressable; |
|
89
|
21 |
|
$this->setProtocolNo($protocolNo); |
|
90
|
17 |
|
$this->setSource($source); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
public function getSize(): int |
|
94
|
|
|
{ |
|
95
|
2 |
|
return $this->size; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
public function getOrigin(): int |
|
99
|
|
|
{ |
|
100
|
2 |
|
return $this->origin; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function isTagged(): bool |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->tagged; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
public function isAddressable(): bool |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $this->addressable; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
public function getProtocolNo(): int |
|
114
|
|
|
{ |
|
115
|
2 |
|
return $this->protocolNo; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function getSource(): int |
|
119
|
|
|
{ |
|
120
|
2 |
|
return $this->source; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|