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
|
21 |
|
private function setTagged(bool $tagged): void |
47
|
|
|
{ |
48
|
21 |
|
$this->tagged = $tagged; |
49
|
|
|
} |
50
|
|
|
|
51
|
21 |
|
private function setAddressable(bool $addressable): void |
52
|
|
|
{ |
53
|
21 |
|
$this->addressable = $addressable; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param int $protocolNo |
58
|
|
|
* @throws InvalidValueException |
59
|
|
|
*/ |
60
|
21 |
|
private function setProtocolNo(int $protocolNo): void |
61
|
|
|
{ |
62
|
21 |
|
if ($protocolNo < 0 || $protocolNo > 4095) { |
63
|
4 |
|
throw new InvalidValueException("Message protocol number {$protocolNo} outside allowable range 0 - 4095"); |
64
|
|
|
} |
65
|
|
|
|
66
|
17 |
|
$this->protocolNo = $protocolNo; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int $source |
71
|
|
|
* @throws InvalidValueException |
72
|
|
|
*/ |
73
|
17 |
|
private function setSource(int $source): void |
74
|
|
|
{ |
75
|
17 |
|
if ($source < UINT32_MIN || $source > UINT32_MAX) { |
76
|
2 |
|
throw new InvalidValueException( |
77
|
2 |
|
"Message source value {$source} outside allowable range " . UINT32_MIN . " - " . UINT32_MAX |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
15 |
|
$this->source = $source; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $size |
86
|
|
|
* @param int $origin |
87
|
|
|
* @param bool $tagged |
88
|
|
|
* @param bool $addressable |
89
|
|
|
* @param int $protocolNo |
90
|
|
|
* @param int $source |
91
|
|
|
* @throws InvalidValueException |
92
|
|
|
*/ |
93
|
29 |
|
public function __construct(int $size, int $origin, bool $tagged, bool $addressable, int $protocolNo, int $source) |
94
|
|
|
{ |
95
|
29 |
|
$this->setSize($size); |
96
|
25 |
|
$this->setOrigin($origin); |
97
|
21 |
|
$this->setTagged($tagged); |
98
|
21 |
|
$this->setAddressable($addressable); |
99
|
21 |
|
$this->setProtocolNo($protocolNo); |
100
|
17 |
|
$this->setSource($source); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function getSize(): int |
104
|
|
|
{ |
105
|
2 |
|
return $this->size; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function getOrigin(): int |
109
|
|
|
{ |
110
|
2 |
|
return $this->origin; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
public function isTagged(): bool |
114
|
|
|
{ |
115
|
2 |
|
return $this->tagged; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
public function isAddressable(): bool |
119
|
|
|
{ |
120
|
2 |
|
return $this->addressable; |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
public function getProtocolNo(): int |
124
|
|
|
{ |
125
|
2 |
|
return $this->protocolNo; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
public function getSource(): int |
129
|
|
|
{ |
130
|
2 |
|
return $this->source; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|