Skip to content

Commit a2dc3ab

Browse files
committed
(wip|[skip ci]): Add initial implementation for TextString encoding and tests
- Implement `TextString` class with an `encode` method for CBOR text string encoding based on the length. - Placeholder added for the `decode` method (not yet implemented). - Create `TextStringTest` to validate encoding functionality with various test cases. - Include a data provider for encoding tests, covering short and long strings. Note: Decoding functionality is still pending implementation.
1 parent 54cb75c commit a2dc3ab

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/CBOR/MajorTypes/TextString.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the ATProto Core package.
5+
*
6+
* (c) Core Branch
7+
*
8+
* This source code is licensed under the MIT license found in the
9+
* LICENSE file in the root directory of this source tree.
10+
*/
11+
12+
namespace ATProto\Core\CBOR\MajorTypes;
13+
14+
class TextString
15+
{
16+
public static function encode(string $input): string
17+
{
18+
$length = strlen($input);
19+
20+
if ($length <= 23) {
21+
$header = chr((0x03 << 5) | $length);
22+
} elseif ($length <= 0xFF) {
23+
$header = chr((0x03 << 5) | 24) . chr($length);
24+
} elseif ($length <= 0xFFFF) {
25+
$header = chr((0x03 << 5) | 25) . pack('n', $length);
26+
} elseif ($length <= 0xFFFFFFFF) {
27+
$header = chr((0x03 << 5) | 26) . pack('N', $length);
28+
} else {
29+
$header = chr((0x03 << 5) | 27) . pack('J', $length);
30+
}
31+
32+
return $header . $input;
33+
}
34+
35+
public static function decode(string $input): string
36+
{
37+
// TODO
38+
}
39+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the ATProto Core package.
5+
*
6+
* (c) Core Branch
7+
*
8+
* This source code is licensed under the MIT license found in the
9+
* LICENSE file in the root directory of this source tree.
10+
*/
11+
12+
namespace Tests\Unit\CBOR\MajorTypes;
13+
14+
use ATProto\Core\CBOR\MajorTypes\TextString;
15+
use PHPUnit\Framework\Attributes\DataProvider;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class TextStringTest extends TestCase
19+
{
20+
#[DataProvider('provideCases')]
21+
public function testItCanEncodeCorrectly(string $case, string $header): void
22+
{
23+
$actual = bin2hex(TextString::encode($case));
24+
$expected = bin2hex($header . $case);
25+
26+
$this->assertSame($expected, $actual);
27+
}
28+
29+
#[DataProvider('provideCases')]
30+
public function testItCanDecodeCorrectly(string $case, string $header): void
31+
{
32+
$target = $header . $case;
33+
34+
$actual = TextString::decode($target);
35+
$expected = $case;
36+
37+
$this->assertSame($expected, $actual);
38+
}
39+
40+
public static function provideCases(): array
41+
{
42+
return [
43+
["f", "\x61"],
44+
["fo", "\x62"],
45+
["foo", "\x63"],
46+
["foob", "\x64"],
47+
["fooba", "\x65"],
48+
["foobar", "\x66"],
49+
[
50+
"This is a longer string. This is a longer string. This is a longer string. This is a longer string.
51+
This is a longer string. This is a longer string. This is a longer string. This is a longer string.
52+
This is a longer string. This is a longer string. This is a longer string. This is a longer string.",
53+
"\x79\x01\x4D"
54+
]
55+
];
56+
}
57+
}

0 commit comments

Comments
 (0)