@@ -33,6 +33,11 @@ class Packer implements IPacker
3333 private const LARGE = 65536 ;
3434 private const HUGE = 4294967295 ;
3535
36+ /**
37+ * @var bool
38+ */
39+ private $ littleEndian ;
40+
3641 private $ structuresLt = [
3742 Relationship::class => [0x52 , 'id ' => 'packInteger ' , 'startNodeId ' => 'packInteger ' , 'endNodeId ' => 'packInteger ' , 'type ' => 'packString ' , 'properties ' => 'packMap ' ],
3843 Date::class => [0x44 , 'days ' => 'packInteger ' ],
@@ -57,6 +62,8 @@ public function pack($signature, ...$params): Generator
5762 {
5863 $ output = '' ;
5964
65+ $ this ->littleEndian = unpack ('S ' , "\x01\x00" )[1 ] === 1 ;
66+
6067 //structure
6168 $ length = count ($ params );
6269 if ($ length < self ::SMALL ) { //TINY_STRUCT
@@ -163,27 +170,25 @@ private function packFloat(float $value): string
163170 private function packInteger (int $ value ): string
164171 {
165172 $ output = '' ;
166- $ tmp = unpack ('S ' , "\x01\x00" );
167- $ little = $ tmp [1 ] == 1 ;
168173
169174 if ($ value >= 0 && $ value <= 127 ) { //+TINY_INT
170175 $ packed = pack ('C ' , 0b00000000 | $ value );
171- $ output .= $ little ? strrev ($ packed ) : $ packed ;
176+ $ output .= $ this -> littleEndian ? strrev ($ packed ) : $ packed ;
172177 } elseif ($ value >= -16 && $ value < 0 ) { //-TINY_INT
173178 $ packed = pack ('c ' , 0b11110000 | $ value );
174- $ output .= $ little ? strrev ($ packed ) : $ packed ;
179+ $ output .= $ this -> littleEndian ? strrev ($ packed ) : $ packed ;
175180 } elseif ($ value >= -128 && $ value <= -17 ) { //INT_8
176181 $ packed = pack ('c ' , $ value );
177- $ output .= chr (0xC8 ) . ($ little ? strrev ($ packed ) : $ packed );
182+ $ output .= chr (0xC8 ) . ($ this -> littleEndian ? strrev ($ packed ) : $ packed );
178183 } elseif (($ value >= 128 && $ value <= 32767 ) || ($ value >= -32768 && $ value <= -129 )) { //INT_16
179184 $ packed = pack ('s ' , $ value );
180- $ output .= chr (0xC9 ) . ($ little ? strrev ($ packed ) : $ packed );
185+ $ output .= chr (0xC9 ) . ($ this -> littleEndian ? strrev ($ packed ) : $ packed );
181186 } elseif (($ value >= 32768 && $ value <= 2147483647 ) || ($ value >= -2147483648 && $ value <= -32769 )) { //INT_32
182187 $ packed = pack ('l ' , $ value );
183- $ output .= chr (0xCA ) . ($ little ? strrev ($ packed ) : $ packed );
188+ $ output .= chr (0xCA ) . ($ this -> littleEndian ? strrev ($ packed ) : $ packed );
184189 } elseif (($ value >= 2147483648 && $ value <= 9223372036854775807 ) || ($ value >= -9223372036854775808 && $ value <= -2147483649 )) { //INT_64
185190 $ packed = pack ('q ' , $ value );
186- $ output .= chr (0xCB ) . ($ little ? strrev ($ packed ) : $ packed );
191+ $ output .= chr (0xCB ) . ($ this -> littleEndian ? strrev ($ packed ) : $ packed );
187192 } else {
188193 throw new PackException ('Integer out of range ' );
189194 }
0 commit comments