1111
1212abstract class ControlPacket {
1313
14- protected $ command ;
15-
1614 /** @var $version Version */
1715 protected $ version ;
1816
19-
20- protected $ variableHeader ;
21-
2217 protected $ payload = '' ;
2318
24- protected $ useVariableHeader = false ;
25- protected $ useFixedHeader = true ;
26-
2719 protected $ identifier ;
2820
2921 public function __construct (Version $ version )
@@ -38,9 +30,9 @@ public function __construct(Version $version)
3830 */
3931 public static function parse (Version $ version , $ rawInput )
4032 {
41- $ packet = new static ($ version );
4233 static ::checkRawInputValidControlPackageType ($ rawInput );
43- return $ packet ;
34+
35+ return new static ($ version );
4436 }
4537
4638 protected static function checkRawInputValidControlPackageType ($ rawInput )
@@ -51,9 +43,9 @@ protected static function checkRawInputValidControlPackageType($rawInput)
5143 }
5244 }
5345
54- /** @return null */
46+ /** @return int */
5547 public static function getControlPacketType () {
56- throw new \RuntimeException ('you should overwrite getControlPacketType() ' );
48+ throw new \RuntimeException ('you must overwrite getControlPacketType() ' );
5749 }
5850
5951 protected function getPayloadLength ()
@@ -91,7 +83,7 @@ protected function getFixedHeader()
9183 */
9284 protected function getVariableHeader ()
9385 {
94- return null ;
86+ return '' ;
9587 }
9688
9789 /**
@@ -119,27 +111,15 @@ public function getLengthPrefixField($fieldPayload)
119111 $ return = chr ($ msb );
120112 $ return .= chr ($ lsb );
121113 $ return .= $ fieldPayload ;
114+
122115 return $ return ;
123116 }
124117
125118 public function get ()
126119 {
127- $ fullMessage = '' ;
128-
129- // add fixed header
130- if ($ this ->useFixedHeader ) {
131- $ fullMessage .= $ this ->getFixedHeader ();
132- }
133-
134- // add variable header
135- if ($ this ->useVariableHeader ) {
136- $ fullMessage .= $ this ->getVariableHeader ();
137- }
138-
139- // add payload
140- $ fullMessage .= $ this ->getPayload ();
141-
142- return $ fullMessage ;
120+ return $ this ->getFixedHeader () .
121+ $ this ->getVariableHeader () .
122+ $ this ->getPayload ();
143123 }
144124
145125 /**
@@ -161,8 +141,7 @@ protected static function getPayloadLengthPrefixFieldInRawInput($startIndex, $ra
161141 $ headerLength = 2 ;
162142 $ header = substr ($ rawInput , $ startIndex , $ headerLength );
163143 $ lengthOfMessage = ord($ header {1 });
144+
164145 return substr ($ rawInput , $ startIndex + $ headerLength , $ lengthOfMessage );
165146 }
166-
167-
168147}
0 commit comments