1 | <?php |
||
7 | class Single implements TypeInterface |
||
8 | { |
||
9 | /** |
||
10 | * Returns a 4-bytes floating-point |
||
11 | * |
||
12 | * @param \PhpBinaryReader\BinaryReader $br |
||
13 | * @param null $length |
||
14 | * |
||
15 | * @return float |
||
16 | * @throws \OutOfBoundsException |
||
17 | */ |
||
18 | 14 | public function read(BinaryReader &$br, $length = null) |
|
19 | { |
||
20 | 14 | if (!$br->canReadBytes(4)) { |
|
21 | 4 | throw new \OutOfBoundsException('Cannot read 4-bytes floating-point, it exceeds the boundary of the file'); |
|
22 | } |
||
23 | |||
24 | 10 | $segment = $br->readFromHandle(4); |
|
25 | |||
26 | 10 | if ($br->getCurrentBit() !== 0) { |
|
27 | 4 | $data = unpack('N', $segment)[1]; |
|
28 | 4 | $data = $this->bitReader($br, $data); |
|
29 | |||
30 | 4 | $endian = $br->getMachineByteOrder() === $br->getEndian() ? 'N' : 'V'; |
|
|
|||
31 | 4 | $segment = pack($endian, $data); |
|
32 | 6 | } elseif ($br->getMachineByteOrder() !== $br->getEndian()) { |
|
33 | 4 | $segment = pack('N', unpack('V', $segment)[1]); |
|
34 | } |
||
35 | |||
36 | 10 | $value = unpack('f', $segment)[1]; |
|
37 | |||
38 | 10 | return $value; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param \PhpBinaryReader\BinaryReader $br |
||
43 | * @param int $data |
||
44 | * |
||
45 | * @return int |
||
46 | */ |
||
47 | 4 | private function bitReader(BinaryReader $br, $data) |
|
55 | } |
||
56 |