File tree Expand file tree Collapse file tree 3 files changed +60
-8
lines changed Expand file tree Collapse file tree 3 files changed +60
-8
lines changed Original file line number Diff line number Diff line change 1313 ],
1414 "license" : " GPL-3.0-or-later" ,
1515 "require" : {
16- "php" : " ^7||^8" ,
16+ "php" : " ^7||^8"
1717 },
1818 "require-dev" : {
1919 "phpunit/phpunit" : " ^7||^8||^9"
Original file line number Diff line number Diff line change 11<?php
22namespace YOCLIB \Netstring ;
33
4+ use Exception ;
5+
46class Netstring{
57
6- private $ string ;
8+ /**
9+ * @var int $maxLength
10+ */
11+ private static $ maxLength = 2000000 ;
12+
13+ /**
14+ * @return int
15+ */
16+ public static function getMaxLength (){
17+ return self ::$ maxLength ;
18+ }
19+
20+ /**
21+ * @param $maxLength
22+ * @return void
23+ */
24+ public static function setMaxLength ($ maxLength ){
25+ self ::$ maxLength = $ maxLength ;
26+ }
727
828 /**
929 * @return string
30+ * @throws Exception
1031 */
11- public function getString (){
12- return $ this ->string ;
32+ public static function decode ($ string ){
33+ $ length = explode (': ' ,$ string ,2 )[0 ] ?? null ;
34+ if ($ length ==null ){
35+ throw new Exception ('Netstring does not have a semicolon. ' );
36+ }
37+ $ semicolon = $ string [strlen ($ length )] ?? null ;
38+ if ($ semicolon !=': ' ){
39+ throw new Exception ('Expecting semicolon. Was \'' .$ semicolon .'\'. ' );
40+ }
41+ $ data = substr ($ string ,strlen ($ length )+1 ,intval ($ length ));
42+ $ comma = $ string [strlen ($ length )+1 +intval ($ length )] ?? null ;
43+ if ($ comma !=', ' ){
44+ throw new Exception ('Expecting comma. Was \'' .$ comma .'\'. ' );
45+ }
46+ return $ data ;
1347 }
1448
1549 /**
16- * @param string string
50+ * @return string
51+ * @throws Exception
1752 */
18- public function setString ($ string ): void {
19- $ this ->string = $ string ;
53+ public static function encode ($ string ){
54+ if (strlen ($ string )>self ::$ maxLength ){
55+ throw new Exception ('Length of ' .strlen ($ string ).' exceeding ' .self ::$ maxLength .' during encoding. ' );
56+ }
57+ return strlen ($ string ).': ' .$ string .', ' ;
2058 }
2159
2260}
Original file line number Diff line number Diff line change 77
88class NetstringTest extends TestCase{
99
10- //TODO Create tests
10+ public function testDecodingNetstring (){
11+ self ::assertEquals ('' ,Netstring::decode ('0:, ' ));
12+ self ::assertEquals ('a ' ,Netstring::decode ('1:a, ' ));
13+ self ::assertEquals ('ab ' ,Netstring::decode ('2:ab, ' ));
14+ self ::assertEquals ('abc ' ,Netstring::decode ('3:abc, ' ));
15+ self ::assertEquals ('abcd ' ,Netstring::decode ('4:abcd, ' ));
16+ }
17+
18+ public function testEncodingNetstring (){
19+ self ::assertEquals ('0:, ' ,Netstring::encode ('' ));
20+ self ::assertEquals ('1:a, ' ,Netstring::encode ('a ' ));
21+ self ::assertEquals ('2:ab, ' ,Netstring::encode ('ab ' ));
22+ self ::assertEquals ('3:abc, ' ,Netstring::encode ('abc ' ));
23+ self ::assertEquals ('4:abcd, ' ,Netstring::encode ('abcd ' ));
24+ }
1125
1226}
You can’t perform that action at this time.
0 commit comments