Skip to content

Commit fd3ecd3

Browse files
authored
Merge pull request #17 from petrknap/xz
Implemented xz coder
2 parents 3e53ee3 + 29b5fcc commit fd3ecd3

File tree

8 files changed

+134
-2
lines changed

8 files changed

+134
-2
lines changed

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"allow-plugins": false,
1515
"sort-packages": true
1616
},
17+
"conflict": {
18+
"petrknap/xz-utils": "<1|>=2"
19+
},
1720
"description": "Library for work with binary data and objects",
1821
"funding": [
1922
{
@@ -34,7 +37,8 @@
3437
"hexadecimal",
3538
"igbinary",
3639
"serializer",
37-
"zlib"
40+
"zlib",
41+
"xz"
3842
],
3943
"license": "LGPL-3.0-or-later",
4044
"name": "petrknap/binary",
@@ -48,6 +52,7 @@
4852
"ext-mbstring": "*",
4953
"ext-zlib": "*",
5054
"nunomaduro/phpinsights": "^2.11",
55+
"petrknap/xz-utils": "*",
5156
"phpstan/phpstan": "^1.12",
5257
"phpunit/phpunit": "^10.5",
5358
"squizlabs/php_codesniffer": "^3.7"
@@ -76,6 +81,7 @@
7681
"suggest": {
7782
"ext-igbinary": "Required to serialize data via igbinary",
7883
"ext-mbstring": "Required to bite bytes",
79-
"ext-zlib": "Required to compress data"
84+
"ext-zlib": "Required to compress data",
85+
"petrknap/xz-utils": "Required to compress data"
8086
}
8187
}

src/Coder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ abstract public function checksum(string|null $algorithm = null): static;
4848
*/
4949
abstract public function hex(): static;
5050

51+
/**
52+
* @see Coder\Xz
53+
*
54+
* @throws Coder\Exception\CoderException
55+
*/
56+
abstract public function xz(): static;
57+
5158
/**
5259
* @see Coder\zlib
5360
*

src/Coder/Xz.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary\Coder;
6+
7+
use PetrKnap\Shorts\HasRequirements;
8+
use PetrKnap\XzUtils\Xz as Inner;
9+
10+
final class Xz extends Coder
11+
{
12+
use HasRequirements;
13+
14+
private int|null $compressionPreset;
15+
16+
public function __construct()
17+
{
18+
self::checkRequirements(
19+
classes: [
20+
Inner::class,
21+
],
22+
);
23+
}
24+
25+
public function encode(string $decoded, int|null $compressionPreset = null): string
26+
{
27+
$this->compressionPreset = $compressionPreset;
28+
return parent::encode($decoded);
29+
}
30+
31+
protected function doEncode(string $decoded): string
32+
{
33+
return (new Inner())->compress(
34+
data: $decoded,
35+
compressionPreset: $this->compressionPreset,
36+
);
37+
}
38+
39+
protected function doDecode(string $encoded): string
40+
{
41+
return (new Inner())->decompress(
42+
data: $encoded,
43+
);
44+
}
45+
}

src/Decoder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function hex(): static
2828
));
2929
}
3030

31+
public function xz(): static
32+
{
33+
return $this->withData((new Coder\Xz())->decode(
34+
$this->data,
35+
));
36+
}
37+
3138
public function zlib(int|null $maxLength = null): static
3239
{
3340
return $this->withData((new Coder\Zlib())->decode(

src/Encoder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public function hex(): static
2929
));
3030
}
3131

32+
public function xz(int|null $compressionPreset = null): static
33+
{
34+
return $this->withData((new Coder\Xz())->encode(
35+
$this->data,
36+
compressionPreset: $compressionPreset,
37+
));
38+
}
39+
3240
public function zlib(int|null $encoding = null, int|null $level = null): static
3341
{
3442
return $this->withData((new Coder\Zlib())->encode(

tests/Coder/XzTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary\Coder;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
9+
final class XzTest extends CoderTestCase
10+
{
11+
public static function data(): array
12+
{
13+
$data = self::getDecodedData();
14+
return [
15+
'default compression preset' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), null],
16+
'compression preset 0' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhAQwAAACPmEGc4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), 0],
17+
'compression preset 9' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhARwAAAAQz1jM4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), 9],
18+
];
19+
}
20+
21+
#[DataProvider('data')]
22+
public function testEncodes(string $decoded, string $encoded, int|null $compressionPreset): void
23+
{
24+
self::assertBinarySame(
25+
$encoded,
26+
(new Xz())->encode(
27+
$decoded,
28+
compressionPreset: $compressionPreset,
29+
),
30+
);
31+
}
32+
33+
#[DataProvider('data')]
34+
public function testDecodes(string $decoded, string $encoded): void
35+
{
36+
self::assertBinarySame(
37+
$decoded,
38+
(new Xz())->decode(
39+
$encoded,
40+
),
41+
);
42+
}
43+
}

tests/DecoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function testDecodesHex(): void
3030
);
3131
}
3232

33+
public function testDecodesXz(): void
34+
{
35+
self::assertBinarySame(
36+
Coder\XzTest::getDecodedData(),
37+
(new Decoder(Coder\XzTest::getEncodedData()))->xz()->data,
38+
);
39+
}
40+
3341
public function testDecodesZlib(): void
3442
{
3543
self::assertBinarySame(

tests/EncoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function testEncodesHex(): void
3030
);
3131
}
3232

33+
public function testEncodesXz(): void
34+
{
35+
self::assertBinarySame(
36+
Coder\XzTest::getEncodedData(),
37+
(new Encoder(Coder\XzTest::getDecodedData()))->xz()->data,
38+
);
39+
}
40+
3341
public function testEncodesZlib(): void
3442
{
3543
self::assertBinarySame(

0 commit comments

Comments
 (0)