|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PBurggraf\BinaryUtilities\DataType; |
|
6
|
|
|
|
|
7
|
|
|
use PBurggraf\BinaryUtilities\Exception\EndOfFileReachedException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Philip Burggraf <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class FloatingPoint extends AbstractDataType |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @return array |
|
16
|
|
|
* @throws EndOfFileReachedException |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
View Code Duplication |
public function read(): array |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
$bytes = []; |
|
22
|
|
|
|
|
23
|
|
|
$this->assertNotEndOfFile(); |
|
24
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
25
|
|
|
$this->assertNotEndOfFile(); |
|
26
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
27
|
|
|
$this->assertNotEndOfFile(); |
|
28
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
29
|
|
|
$this->assertNotEndOfFile(); |
|
30
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
31
|
|
|
|
|
32
|
|
|
$data = $this->endianMode->applyEndianess($bytes); |
|
33
|
|
|
|
|
34
|
|
|
return [ |
|
35
|
|
|
$this->mergeBytes($data), |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param int $length |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws EndOfFileReachedException |
|
44
|
|
|
* |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
public function readArray(int $length): array |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$buffer = []; |
|
49
|
|
|
|
|
50
|
|
|
for ($iterator = 0; $iterator < $length; ++$iterator) { |
|
51
|
|
|
$bytes = []; |
|
52
|
|
|
|
|
53
|
|
|
$this->assertNotEndOfFile(); |
|
54
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
55
|
|
|
$this->assertNotEndOfFile(); |
|
56
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
57
|
|
|
$this->assertNotEndOfFile(); |
|
58
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
59
|
|
|
$this->assertNotEndOfFile(); |
|
60
|
|
|
$bytes[] = $this->getByte($this->offset++); |
|
61
|
|
|
|
|
62
|
|
|
$data = $this->endianMode->applyEndianess($bytes); |
|
63
|
|
|
|
|
64
|
|
|
$buffer[] = $this->mergeBytes($data); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $buffer; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $data |
|
72
|
|
|
* |
|
73
|
|
|
* @return float |
|
74
|
|
|
*/ |
|
75
|
|
|
private function mergeBytes(array $data): float |
|
76
|
|
|
{ |
|
77
|
|
|
$string = hex2bin(str_pad(dechex($data[0]), 2, '0', STR_PAD_LEFT) . str_pad(dechex($data[1]), 2, '0', STR_PAD_LEFT) . str_pad(dechex($data[2]), 2, '0', STR_PAD_LEFT) . str_pad(dechex($data[3]), 2, '0', STR_PAD_LEFT)); |
|
78
|
|
|
|
|
79
|
|
|
$result = unpack('G', $string); |
|
80
|
|
|
|
|
81
|
|
|
if (is_array($result) === false) { |
|
82
|
|
|
throw new \Exception(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $result[1]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param int|float $data |
|
90
|
|
|
* |
|
91
|
|
|
* @throws EndOfFileReachedException |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function write($data): void |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$bytes = $this->splitBytes($data); |
|
96
|
|
|
|
|
97
|
|
|
$bytes = $this->endianMode->applyEndianess($bytes); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertNotEndOfFile(); |
|
100
|
|
|
$this->setByte($this->offset++, $bytes[0]); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertNotEndOfFile(); |
|
103
|
|
|
$this->setByte($this->offset++, $bytes[1]); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertNotEndOfFile(); |
|
106
|
|
|
$this->setByte($this->offset++, $bytes[2]); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertNotEndOfFile(); |
|
109
|
|
|
$this->setByte($this->offset++, $bytes[3]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $data |
|
114
|
|
|
* |
|
115
|
|
|
* @throws EndOfFileReachedException |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
public function writeArray(array $data): void |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$dataLength = count($data); |
|
120
|
|
|
$startBytePosition = $this->offset; |
|
121
|
|
|
|
|
122
|
|
|
for ($i = $startBytePosition; $i <= $startBytePosition - 1 + $dataLength; ++$i) { |
|
123
|
|
|
$bytes = $this->splitBytes($data[$i - $startBytePosition]); |
|
124
|
|
|
|
|
125
|
|
|
$bytes = $this->endianMode->applyEndianess($bytes); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertNotEndOfFile(); |
|
128
|
|
|
$this->setByte($this->offset++, $bytes[0]); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertNotEndOfFile(); |
|
131
|
|
|
$this->setByte($this->offset++, $bytes[1]); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertNotEndOfFile(); |
|
134
|
|
|
$this->setByte($this->offset++, $bytes[2]); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertNotEndOfFile(); |
|
137
|
|
|
$this->setByte($this->offset++, $bytes[3]); |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int|float $data |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
public function splitBytes($data): array |
|
148
|
|
|
{ |
|
149
|
|
|
$data = hexdec(bin2hex(pack('G', $data))); |
|
150
|
|
|
|
|
151
|
|
|
$bytes = []; |
|
152
|
|
|
|
|
153
|
|
|
$bytes[] = ($data & 0xff000000) >> 24; |
|
154
|
|
|
$bytes[] = ($data & 0x00ff0000) >> 16; |
|
155
|
|
|
$bytes[] = ($data & 0x0000ff00) >> 8; |
|
156
|
|
|
$bytes[] = ($data & 0x000000ff); |
|
157
|
|
|
|
|
158
|
|
|
return $bytes; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
public function newContent(): string |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->content; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
public function newOffset(): int |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->offset; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.