File tree Expand file tree Collapse file tree 5 files changed +31
-18
lines changed Expand file tree Collapse file tree 5 files changed +31
-18
lines changed Original file line number Diff line number Diff line change @@ -30,8 +30,16 @@ public static function encode(string|int|array $data): string
3030 throw new \ValueError ("Unsupported type: " . gettype ($ data ));
3131 }
3232
33- public static function decode (string $ data ): int
33+ public static function decode (string $ data ): int | string
3434 {
35- // TODO
35+ if (TextString::validate ($ data )) {
36+ return TextString::decode ($ data );
37+ }
38+
39+ if (UnsignedInteger::validate ($ data )) {
40+ return UnsignedInteger::decode ($ data );
41+ }
42+
43+ throw new \ValueError ("Unsupported type " );
3644 }
3745}
Original file line number Diff line number Diff line change @@ -34,14 +34,11 @@ public static function encode(string $input): string
3434
3535 public static function decode (string $ input ): string
3636 {
37- $ initialByte = ord ($ input [0 ]);
38- $ majorType = ($ initialByte >> 5 ) & 0x07 ;
39-
40- if ($ majorType !== 0x03 ) {
37+ if (! self ::validate ($ input )) {
4138 throw new \ValueError ('Invalid CBOR TextString major type. ' );
4239 }
4340
44- $ additionalInfo = $ initialByte & 0x1F ;
41+ $ additionalInfo = ord ( $ input [ 0 ]) & 0x1F ;
4542 $ offset = 1 ;
4643
4744 if ($ additionalInfo <= 23 ) {
@@ -70,4 +67,9 @@ public static function decode(string $input): string
7067
7168 return $ text ;
7269 }
70+
71+ public static function validate (string $ input ): bool
72+ {
73+ return ((ord ($ input [0 ]) >> 5 ) & 0x07 ) === 0x03 ;
74+ }
7375}
Original file line number Diff line number Diff line change @@ -17,14 +17,12 @@ class UnsignedInteger
1717{
1818 public static function decode (string $ data ): int
1919 {
20- $ firstByte = ord ($ data [0 ]);
21- $ majorType = ($ firstByte >> 5 ) & 0x07 ;
22- $ additionalInfo = $ firstByte & 0x1F ;
23-
24- if ($ majorType !== 0 ) {
25- throw new ValueError ("Invalid major type for unsigned integer: $ majorType " );
20+ if (! self ::validate ($ data )) {
21+ throw new ValueError ("Invalid major type for unsigned integer. " );
2622 }
2723
24+ $ additionalInfo = ord ($ data [0 ]) & 0x1F ;
25+
2826 if ($ additionalInfo <= 23 ) {
2927 $ value = $ additionalInfo ;
3028 } elseif ($ additionalInfo === 24 ) {
@@ -73,4 +71,9 @@ public static function encode(int $value): string
7371
7472 return $ prefixedPack ('J ' , "\x1B" );
7573 }
74+
75+ public static function validate (string $ input ): bool
76+ {
77+ return ((ord ($ input [0 ]) >> 5 ) & 0x07 ) === 0x00 ;
78+ }
7679}
Original file line number Diff line number Diff line change 1818class CBORTest extends TestCase
1919{
2020 #[DataProvider('validCases ' )]
21- public function testEncode (int $ data , string $ expected ): void
21+ public function testEncode (int | string $ data , string $ expected ): void
2222 {
2323 $ encoded = CBOR ::encode ($ data );
2424 $ this ->assertSame ($ expected , $ encoded );
2525 }
2626
2727 #[DataProvider('validCases ' )]
28- public function testDecode (int $ expected , string $ data ): void
28+ public function testDecode (int | string $ expected , string $ data ): void
2929 {
3030 $ actual = CBOR ::decode ($ data );
3131
@@ -42,8 +42,8 @@ public static function validCases(): array
4242 [1 , hex2bin ('01 ' )], // 1 encoded as CBOR unsigned integer
4343 [10 , hex2bin ('0a ' )], // 10 encoded as CBOR unsigned integer
4444
45- // // String test cases
46- // [[ 'hello'] , hex2bin('6568656c6c6f ')], // "hello" encoded as CBOR text string
45+ // String test cases
46+ ['hello ' , hex2bin ('6568656C6C6F ' )], // "hello" encoded as CBOR text string
4747//
4848// // Boolean test cases
4949// [[true], hex2bin('f5')], // true encoded as CBOR special type
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ public function testEncodeThrowsAnExceptionForValueGreaterThanIntMax(): void
4747 public function testDecodeThrowsAnExceptionWhenPassedInvalidValue (string $ case ): void
4848 {
4949 $ this ->expectException (\ValueError::class);
50- $ this ->expectExceptionMessage ("Invalid major type for unsigned integer: " );
50+ $ this ->expectExceptionMessage ("Invalid major type for unsigned integer. " );
5151
5252 UnsignedInteger::decode ($ case );
5353 }
You can’t perform that action at this time.
0 commit comments