-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
use PetrKnap\Shorts\HasRequirements; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
abstract class ExternalCoder extends Coder | ||
{ | ||
use HasRequirements; | ||
|
||
/** | ||
* Must be set in {@see self::encode()} | ||
* | ||
* @var array<string> | ||
*/ | ||
protected array $encodeCommand; | ||
|
||
/** | ||
* Must be set in {@see self::encode()} | ||
* | ||
* @var array<string> | ||
*/ | ||
protected array $decodeCommand; | ||
|
||
public function __construct() | ||
{ | ||
self::checkRequirements( | ||
classes: [ | ||
Process::class, | ||
ProcessFailedException::class, | ||
], | ||
); | ||
} | ||
|
||
protected function doEncode(string $decoded): string | ||
{ | ||
$encoder = new Process($this->encodeCommand); | ||
$encoder->setInput($decoded); | ||
$encoder->run(); | ||
if (!$encoder->isSuccessful()) { | ||
throw new ProcessFailedException($encoder); | ||
} | ||
return $encoder->getOutput(); | ||
} | ||
|
||
protected function doDecode(string $encoded): string | ||
{ | ||
$decoder = new Process($this->decodeCommand); | ||
$decoder->setInput($encoded); | ||
$decoder->run(); | ||
if (!$decoder->isSuccessful()) { | ||
throw new ProcessFailedException($decoder); | ||
} | ||
return $decoder->getOutput(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
final class Xz extends ExternalCoder | ||
{ | ||
public function encode(string $decoded, int|null $level = null): string | ||
{ | ||
$this->encodeCommand = ['xz', '--compress', '--stdout']; | ||
if ($level !== null) { | ||
$this->encodeCommand[] = "-{$level}"; | ||
} | ||
return parent::encode($decoded); | ||
} | ||
public function decode(string $encoded): string | ||
{ | ||
$this->decodeCommand = ['xz', '--decompress', '--stdout']; | ||
return parent::decode($encoded); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 preset' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), null], | ||
'preset 0' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhAQwAAACPmEGc4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), 0], | ||
'preset 9' => [$data, base64_decode('/Td6WFoAAATm1rRGAgAhARwAAAAQz1jM4AA7ABxdAG0OUG7jCUfPj0z08gJbP9KgthIHS/jgLMk44AAAwVvY3vTzsZsAATg8V3V2GB+2830BAAAAAARZWg=='), 9], | ||
]; | ||
} | ||
|
||
#[DataProvider('data')] | ||
public function testEncodes(string $decoded, string $encoded, int|null $level): void | ||
{ | ||
self::assertBinarySame( | ||
$encoded, | ||
(new Xz())->encode( | ||
$decoded, | ||
level: $level, | ||
), | ||
); | ||
} | ||
|
||
#[DataProvider('data')] | ||
public function testDecodes(string $decoded, string $encoded): void | ||
{ | ||
self::assertBinarySame( | ||
$decoded, | ||
(new Xz())->decode( | ||
$encoded, | ||
), | ||
); | ||
} | ||
} |