File tree 3 files changed +60
-8
lines changed 3 files changed +60
-8
lines changed Original file line number Diff line number Diff line change 13
13
],
14
14
"license" : " GPL-3.0-or-later" ,
15
15
"require" : {
16
- "php" : " ^7||^8" ,
16
+ "php" : " ^7||^8"
17
17
},
18
18
"require-dev" : {
19
19
"phpunit/phpunit" : " ^7||^8||^9"
Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace YOCLIB \Netstring ;
3
3
4
+ use Exception ;
5
+
4
6
class Netstring{
5
7
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
+ }
7
27
8
28
/**
9
29
* @return string
30
+ * @throws Exception
10
31
*/
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 ;
13
47
}
14
48
15
49
/**
16
- * @param string string
50
+ * @return string
51
+ * @throws Exception
17
52
*/
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 .', ' ;
20
58
}
21
59
22
60
}
Original file line number Diff line number Diff line change 7
7
8
8
class NetstringTest extends TestCase{
9
9
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
+ }
11
25
12
26
}
You can’t perform that action at this time.
0 commit comments