Skip to content

Commit e0b1b57

Browse files
Fixed issue with unpack. Added const visibility.
1 parent b58d192 commit e0b1b57

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

Bolt.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
*/
1414
class Bolt
1515
{
16-
const SUCCESS = 0x70;
17-
const FAILURE = 0x7F;
18-
const IGNORED = 0x7E;
19-
const RECORD = 0x71;
16+
private const SUCCESS = 0x70;
17+
private const FAILURE = 0x7F;
18+
private const IGNORED = 0x7E;
19+
private const RECORD = 0x71;
2020

2121
/**
2222
* @var Packer
@@ -284,7 +284,7 @@ private function read()
284284
$signature = 0;
285285
if (!empty($msg)) {
286286
if (self::$debug) {
287-
self::printHex($msg);
287+
self::printHex($msg, false);
288288
}
289289

290290
try {
@@ -347,7 +347,9 @@ private function error(string $msg, string $code = '')
347347
if (is_callable(self::$errorHandler)) {
348348
call_user_func(self::$errorHandler, $msg, $code);
349349
} else {
350-
$msg .= ' (' . $code . ')';
350+
if (!empty($code)) {
351+
$msg .= ' (' . $code . ')';
352+
}
351353
throw new Exception($msg);
352354
}
353355
}
@@ -356,10 +358,11 @@ private function error(string $msg, string $code = '')
356358
* Print buffer as HEX
357359
* @param string $str
358360
*/
359-
public static function printHex(string $str)
361+
public static function printHex(string $str, bool $write = true)
360362
{
361363
$str = implode(unpack('H*', $str));
362364
echo '<pre>';
365+
echo $write ? '> ' : '< ';
363366
foreach (str_split($str, 8) as $chunk) {
364367
echo implode(' ', str_split($chunk, 2));
365368
echo ' ';

Packer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bolt;
44

5+
use Exception;
6+
57
/**
68
* Class Packer
79
* Pack bolt messages

Unpacker.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Bolt;
44

55
use Bolt\structures\{Node, Path, Relationship, UnboundRelationship};
6+
use Exception;
67

78
/**
89
* Class Unpacker
@@ -46,10 +47,11 @@ public function unpack(string $msg, int &$signature = 0)
4647

4748
/**
4849
* @param string $msg
50+
* @param bool $structures
4951
* @return mixed
5052
* @throws Exception
5153
*/
52-
private function u(string &$msg)
54+
private function u(string &$msg, bool $structures = true)
5355
{
5456
if (empty($msg)) {
5557
return false;
@@ -59,26 +61,29 @@ private function u(string &$msg)
5961
$msg = mb_strcut($msg, 1, null, '8bit');
6062
$result = false;
6163

62-
$output = $this->unpackStruct($marker, $msg, $result);
63-
if ($result) {
64-
return $output;
65-
}
66-
$output = $this->unpackNode($marker, $msg, $result);
67-
if ($result) {
68-
return $output;
69-
}
70-
$output = $this->unpackRelationship($marker, $msg, $result);
71-
if ($result) {
72-
return $output;
73-
}
74-
$output = $this->unpackPath($marker, $msg, $result);
75-
if ($result) {
76-
return $output;
77-
}
78-
$output = $this->unpackUnboundRelationship($marker, $msg, $result);
79-
if ($result) {
80-
return $output;
64+
if ($structures) {
65+
$output = $this->unpackStruct($marker, $msg, $result);
66+
if ($result) {
67+
return $output;
68+
}
69+
$output = $this->unpackNode($marker, $msg, $result);
70+
if ($result) {
71+
return $output;
72+
}
73+
$output = $this->unpackRelationship($marker, $msg, $result);
74+
if ($result) {
75+
return $output;
76+
}
77+
$output = $this->unpackPath($marker, $msg, $result);
78+
if ($result) {
79+
return $output;
80+
}
81+
$output = $this->unpackUnboundRelationship($marker, $msg, $result);
82+
if ($result) {
83+
return $output;
84+
}
8185
}
86+
8287
$output = $this->unpackFloat($marker, $msg, $result);
8388
if ($result) {
8489
return $output;
@@ -327,9 +332,9 @@ private function unpackMap(int $marker, string &$msg, bool &$result = false): ar
327332
$key = null;
328333
for ($i = 0; $i < $size * 2; $i++) {
329334
if ($i % 2 == 0) {
330-
$key = $this->u($msg);
335+
$key = $this->u($msg, false);
331336
} else {
332-
$output[$key] = $this->u($msg);
337+
$output[$key] = $this->u($msg, false);
333338
}
334339
}
335340
$result = true;

0 commit comments

Comments
 (0)