Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"allow-plugins": false,
"sort-packages": true
},
"conflict": {
"petrknap/xz-utils": "<1|>=2"
},
"description": "Library for work with binary data and objects",
"funding": [
{
Expand All @@ -34,7 +37,8 @@
"hexadecimal",
"igbinary",
"serializer",
"zlib"
"zlib",
"xz"
],
"license": "LGPL-3.0-or-later",
"name": "petrknap/binary",
Expand All @@ -48,6 +52,7 @@
"ext-mbstring": "*",
"ext-zlib": "*",
"nunomaduro/phpinsights": "^2.11",
"petrknap/xz-utils": "*",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^10.5",
"squizlabs/php_codesniffer": "^3.7"
Expand Down Expand Up @@ -76,6 +81,7 @@
"suggest": {
"ext-igbinary": "Required to serialize data via igbinary",
"ext-mbstring": "Required to bite bytes",
"ext-zlib": "Required to compress data"
"ext-zlib": "Required to compress data",
"petrknap/xz-utils": "Required to compress data"
}
}
7 changes: 7 additions & 0 deletions src/Coder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ abstract public function checksum(string|null $algorithm = null): static;
*/
abstract public function hex(): static;

/**
* @see Coder\Xz
*
* @throws Coder\Exception\CoderException
*/
abstract public function xz(): static;

/**
* @see Coder\zlib
*
Expand Down
45 changes: 45 additions & 0 deletions src/Coder/Xz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary\Coder;

use PetrKnap\Shorts\HasRequirements;
use PetrKnap\XzUtils\Xz as Inner;

final class Xz extends Coder
{
use HasRequirements;

private int|null $compressionPreset;

public function __construct()
{
self::checkRequirements(
classes: [
Inner::class,
],
);
}

public function encode(string $decoded, int|null $compressionPreset = null): string
{
$this->compressionPreset = $compressionPreset;
return parent::encode($decoded);
}

protected function doEncode(string $decoded): string
{
return (new Inner())->compress(
data: $decoded,
compressionPreset: $this->compressionPreset,
);
}

protected function doDecode(string $encoded): string
{
return (new Inner())->decompress(
data: $encoded,
);
}
}
7 changes: 7 additions & 0 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public function hex(): static
));
}

public function xz(): static
{
return $this->withData((new Coder\Xz())->decode(
$this->data,
));
}

public function zlib(int|null $maxLength = null): static
{
return $this->withData((new Coder\Zlib())->decode(
Expand Down
8 changes: 8 additions & 0 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function hex(): static
));
}

public function xz(int|null $compressionPreset = null): static
{
return $this->withData((new Coder\Xz())->encode(
$this->data,
compressionPreset: $compressionPreset,
));
}

public function zlib(int|null $encoding = null, int|null $level = null): static
{
return $this->withData((new Coder\Zlib())->encode(
Expand Down
43 changes: 43 additions & 0 deletions tests/Coder/XzTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary\Coder;

use PHPUnit\Framework\Attributes\DataProvider;

final class XzTest extends CoderTestCase
{
public static function data(): array
{
$data = self::getDecodedData();
return [
'default compression preset' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), null],
'compression preset 0' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhAQwAAACPmEGc4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), 0],
'compression preset 9' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhARwAAAAQz1jM4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), 9],
];
}

#[DataProvider('data')]
public function testEncodes(string $decoded, string $encoded, int|null $compressionPreset): void
{
self::assertBinarySame(
$encoded,
(new Xz())->encode(
$decoded,
compressionPreset: $compressionPreset,
),
);
}

#[DataProvider('data')]
public function testDecodes(string $decoded, string $encoded): void
{
self::assertBinarySame(
$decoded,
(new Xz())->decode(
$encoded,
),
);
}
}
8 changes: 8 additions & 0 deletions tests/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function testDecodesHex(): void
);
}

public function testDecodesXz(): void
{
self::assertBinarySame(
Coder\XzTest::getDecodedData(),
(new Decoder(Coder\XzTest::getEncodedData()))->xz()->data,
);
}

public function testDecodesZlib(): void
{
self::assertBinarySame(
Expand Down
8 changes: 8 additions & 0 deletions tests/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function testEncodesHex(): void
);
}

public function testEncodesXz(): void
{
self::assertBinarySame(
Coder\XzTest::getEncodedData(),
(new Encoder(Coder\XzTest::getDecodedData()))->xz()->data,
);
}

public function testEncodesZlib(): void
{
self::assertBinarySame(
Expand Down
Loading