22
33namespace Bolt ;
44
5- use Bolt \error \{ConnectException , PackException , UnpackException };
6- use Exception ;
7- use Bolt \packstream \{IPacker , IUnpacker };
5+ use Bolt \error \ConnectException ;
6+ use Bolt \error \BoltException ;
87use Bolt \protocol \{AProtocol , ServerState };
98use Bolt \connection \IConnection ;
109
1918 */
2019final class Bolt
2120{
22- private IPacker $ packer ;
23- private IUnpacker $ unpacker ;
24- private IConnection $ connection ;
25- private array $ versions = [];
21+ private array $ protocolVersions = [];
22+ private int $ packStreamVersion = 1 ;
23+
2624 public static bool $ debug = false ;
2725 public ServerState $ serverState ;
2826
29- /**
30- * Bolt constructor
31- * @param IConnection $connection
32- * @throws Exception
33- */
34- public function __construct (IConnection $ connection )
27+ public function __construct (private IConnection $ connection )
3528 {
36- $ this ->connection = $ connection ;
3729 $ this ->setProtocolVersions (5 , 4.4 , 4.3 );
38- $ this ->setPackStreamVersion ();
3930 }
4031
4132 /**
4233 * Connect via Connection, execute handshake on it, create and return protocol version class
43- * @return AProtocol
44- * @throws Exception
34+ * @throws BoltException
4535 */
4636 public function build (): AProtocol
4737 {
@@ -65,47 +55,32 @@ public function build(): AProtocol
6555 }
6656
6757 $ this ->serverState ->set (ServerState::CONNECTED );
68- return new $ protocolClass ($ this ->packer , $ this -> unpacker , $ this ->connection , $ this ->serverState );
58+ return new $ protocolClass ($ this ->packStreamVersion , $ this ->connection , $ this ->serverState );
6959 }
7060
71- /**
72- * @param int|float|string ...$v
73- * @return Bolt
74- */
75- public function setProtocolVersions (...$ v ): Bolt
61+ public function setProtocolVersions (int |float |string ...$ v ): Bolt
7662 {
77- $ this ->versions = array_slice ($ v , 0 , 4 );
78- while (count ($ this ->versions ) < 4 )
79- $ this ->versions [] = 0 ;
63+ $ this ->protocolVersions = array_slice ($ v , 0 , 4 );
64+ while (count ($ this ->protocolVersions ) < 4 )
65+ $ this ->protocolVersions [] = 0 ;
8066 return $ this ;
8167 }
8268
83- /**
84- * @param int $version
85- * @return Bolt
86- * @throws Exception
87- */
8869 public function setPackStreamVersion (int $ version = 1 ): Bolt
8970 {
90- $ packerClass = "\\Bolt \\packstream \\v " . $ version . "\\Packer " ;
91- if (!class_exists ($ packerClass )) {
92- throw new PackException ('Requested PackStream version ( ' . $ version . ') not yet implemented ' );
93- }
94- $ this ->packer = new $ packerClass ();
95-
96- $ unpackerClass = "\\Bolt \\packstream \\v " . $ version . "\\Unpacker " ;
97- if (!class_exists ($ unpackerClass )) {
98- throw new UnpackException ('Requested PackStream version ( ' . $ version . ') not yet implemented ' );
99- }
100- $ this ->unpacker = new $ unpackerClass ();
71+ $ this ->packStreamVersion = $ version ;
72+ return $ this ;
73+ }
10174
75+ public function setConnection (IConnection $ connection ): Bolt
76+ {
77+ $ this ->connection = $ connection ;
10278 return $ this ;
10379 }
10480
10581 /**
10682 * @link https://www.neo4j.com/docs/bolt/current/bolt/handshake/
107- * @return string
108- * @throws Exception
83+ * @throws BoltException
10984 */
11085 private function handshake (): string
11186 {
@@ -126,8 +101,6 @@ private function handshake(): string
126101
127102 /**
128103 * Read and compose selected protocol version
129- * @param string $bytes
130- * @return string|null
131104 */
132105 private function unpackProtocolVersion (string $ bytes ): ?string
133106 {
@@ -142,18 +115,17 @@ private function unpackProtocolVersion(string $bytes): ?string
142115 }
143116
144117 $ version = implode ('. ' , array_reverse ($ result ));
145- return in_array ($ version , $ this ->versions ) ? $ version : null ;
118+ return in_array ($ version , $ this ->protocolVersions ) ? $ version : null ;
146119 }
147120
148121 /**
149122 * Pack requested protocol versions
150- * @return string
151123 */
152124 private function packProtocolVersions (): string
153125 {
154126 $ versions = [];
155127
156- foreach ($ this ->versions as $ v ) {
128+ foreach ($ this ->protocolVersions as $ v ) {
157129 if (is_int ($ v ))
158130 $ versions [] = pack ('N ' , $ v );
159131 else {
0 commit comments