1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the stream package |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Stream\Writer; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Stream\ByteOrder\ByteOrder; |
11
|
|
|
use GravityMedia\Stream\ByteOrder\ByteOrderAwareTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Integer writer |
15
|
|
|
* |
16
|
|
|
* @package GravityMedia\Stream\Writer |
17
|
|
|
*/ |
18
|
|
|
class IntegerWriter extends Writer |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Use byte order aware trait |
22
|
|
|
*/ |
23
|
|
|
use ByteOrderAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Write unsigned 8-bit integer (char) data to the stream |
27
|
|
|
* |
28
|
|
|
* @param int $value The value |
29
|
|
|
* |
30
|
|
|
* @return int |
31
|
|
|
*/ |
32
|
|
|
public function writeUnsignedInteger8($value) |
33
|
|
|
{ |
34
|
|
|
return $this->write(pack('C', $value)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Write signed 8-bit integer (char) data to the stream |
39
|
|
|
* |
40
|
|
|
* @param int $value The value |
41
|
|
|
* |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
public function writeSignedInteger8($value) |
45
|
|
|
{ |
46
|
|
|
return $this->write(pack('c', $value)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Write unsigned 16-bit integer (short) data to the stream |
51
|
|
|
* |
52
|
|
|
* @param int $value The value |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function writeUnsignedInteger16($value) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
switch ($this->getByteOrder()) { |
59
|
|
|
case ByteOrder::BIG_ENDIAN: |
60
|
|
|
$format = 'n'; |
61
|
|
|
break; |
62
|
|
|
case ByteOrder::LITTLE_ENDIAN: |
63
|
|
|
$format = 'v'; |
64
|
|
|
break; |
65
|
|
|
default: |
66
|
|
|
$format = 'S'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->write(pack($format, $value)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Write signed 16-bit integer (short) data to the stream |
74
|
|
|
* |
75
|
|
|
* @param int $value The value |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function writeSignedInteger16($value) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$data = pack('s', $value); |
82
|
|
|
|
83
|
|
|
if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN |
84
|
|
|
&& $this->getByteOrder() !== $this->getMachineByteOrder() |
85
|
|
|
) { |
86
|
|
|
$data = strrev($data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->write($data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Write unsigned 24-bit integer (short) data to the stream |
94
|
|
|
* |
95
|
|
|
* @param int $value The value |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
|
|
public function writeUnsignedInteger24($value) |
100
|
|
|
{ |
101
|
|
|
$data = pack('C3', $value, $value >> 8, $value >> 16); |
102
|
|
|
|
103
|
|
|
$byteOrder = $this->getByteOrder(); |
104
|
|
|
if ($byteOrder === ByteOrder::MACHINE_ENDIAN) { |
105
|
|
|
$byteOrder = $this->getMachineByteOrder(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($byteOrder !== $this->getMachineByteOrder()) { |
109
|
|
|
$data = strrev($data); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->write($data); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Write signed 24-bit integer (short) data to the stream |
117
|
|
|
* |
118
|
|
|
* @param int $value The value |
119
|
|
|
* |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function writeSignedInteger24($value) |
123
|
|
|
{ |
124
|
|
|
if ($value & 0x7fffff) { |
125
|
|
|
$value += 2 ** 24; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->writeUnsignedInteger24($value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Write unsigned 32-bit integer (long) data to the stream |
133
|
|
|
* |
134
|
|
|
* @param int $value The value |
135
|
|
|
* |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function writeUnsignedInteger32($value) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
switch ($this->getByteOrder()) { |
141
|
|
|
case ByteOrder::BIG_ENDIAN: |
142
|
|
|
$format = 'N'; |
143
|
|
|
break; |
144
|
|
|
case ByteOrder::LITTLE_ENDIAN: |
145
|
|
|
$format = 'V'; |
146
|
|
|
break; |
147
|
|
|
default: |
148
|
|
|
$format = 'L'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->write(pack($format, $value)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Write signed 32-bit integer (long) data to the stream |
156
|
|
|
* |
157
|
|
|
* @param int $value The value |
158
|
|
|
* |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function writeSignedInteger32($value) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$data = pack('l', $value); |
164
|
|
|
|
165
|
|
|
if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN |
166
|
|
|
&& $this->getByteOrder() !== $this->getMachineByteOrder() |
167
|
|
|
) { |
168
|
|
|
$data = strrev($data); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->write($data); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Write unsigned 64-bit integer (long long) data to the stream |
176
|
|
|
* |
177
|
|
|
* @param int $value The value |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function writeUnsignedInteger64($value) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
switch ($this->getByteOrder()) { |
184
|
|
|
case ByteOrder::BIG_ENDIAN: |
185
|
|
|
$format = 'J'; |
186
|
|
|
break; |
187
|
|
|
case ByteOrder::LITTLE_ENDIAN: |
188
|
|
|
$format = 'P'; |
189
|
|
|
break; |
190
|
|
|
default: |
191
|
|
|
$format = 'Q'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this->write(pack($format, $value)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Write signed 64-bit integer (long long) data to the stream |
199
|
|
|
* |
200
|
|
|
* @param int $value The value |
201
|
|
|
* |
202
|
|
|
* @return int |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
public function writeSignedInteger64($value) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$data = pack('q', $value); |
207
|
|
|
|
208
|
|
|
if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN |
209
|
|
|
&& $this->getByteOrder() !== $this->getMachineByteOrder() |
210
|
|
|
) { |
211
|
|
|
$data = strrev($data); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this->write($data); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.