Skip to content

Commit 0aed8e2

Browse files
authored
Merge pull request #3 from beenotung/patch-1-fix-error-on-short-string
patch: fix "strict mode" error when getting prefix on short string
2 parents 61ccd36 + a942a90 commit 0aed8e2

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/Core/Value.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function isString(): bool
3434
public function getStringPrefix(): string
3535
{
3636
$str = $this->value;
37+
if (strlen($str) < 2) {
38+
return '';
39+
}
3740
return $str[0] . $str[1];
3841
}
3942

src/DataTypes/JsonString.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class JsonString
77
// encodeStr
88
public static function encodeString(string $str): string
99
{
10+
if (strlen($str) < 2) {
11+
return $str;
12+
}
1013
$prefix = $str[0] . $str[1];
1114
switch ($prefix) {
1215
case 'b|':
@@ -22,6 +25,9 @@ public static function encodeString(string $str): string
2225
// decodeStr
2326
public static function decodeString(string $str): string
2427
{
28+
if (strlen($str) < 2) {
29+
return $str;
30+
}
2531
$prefix = $str[0] . $str[1];
2632
return $prefix === 's|' ? substr($str, 2) : $str;
2733
}

tests/CompressJsonBaseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,21 @@ public function testNestedWithSameKey()
100100
// Assert
101101
$this->assertEquals($data, $decompressed);
102102
}
103+
104+
public function testShortString()
105+
{
106+
$data = [
107+
'',
108+
'p',
109+
'pp'
110+
];
111+
$compressed = Compressor::create()
112+
->compress($data);
113+
$compressedJson = $compressed
114+
->toJson();
115+
$decompressed = Compressor::create()
116+
->decompressJson($compressedJson);
117+
// Assert
118+
$this->assertEquals($data, $decompressed);
119+
}
103120
}

0 commit comments

Comments
 (0)