Skip to content

Commit 81c230b

Browse files
committed
feat(CBOR): Add decode support for TextString and UnsignedInteger with validation
- Implemented `decode` method in `CBOR` class to support both `TextString` and `UnsignedInteger` types. - Added `validate` method to `TextString` and `UnsignedInteger` classes for type-specific validation. - Updated `decode` methods in `TextString` and `UnsignedInteger` to use their respective `validate` methods. - Enhanced `CBORTest` to include tests for decoding mixed types (`TextString` and `UnsignedInteger`). - Fixed and refactored exception messages for better consistency and readability in `UnsignedInteger`.
1 parent 28ca64d commit 81c230b

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

src/CBOR/CBOR.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

src/CBOR/MajorTypes/TextString.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff 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
}

src/CBOR/MajorTypes/UnsignedInteger.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff 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
}

tests/Unit/CBOR/CBORTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
class 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

tests/Unit/CBOR/MajorTypes/UnsignedIntegerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)