Skip to content

Commit

Permalink
Merge pull request #6 from petrknap/hex-coder
Browse files Browse the repository at this point in the history
Implemented hexadecimal coder
  • Loading branch information
petrknap committed Apr 3, 2024
2 parents 7ca302c + 86c7f34 commit 5a9b322
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"decompression",
"encoder",
"helper",
"hexadecimal",
"igbinary",
"serializer",
"zlib"
Expand Down
26 changes: 26 additions & 0 deletions src/Coder/Hex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary\Coder;

/**
* @see bin2hex()
* @see hex2bin()
*/
final class Hex extends Coder
{
protected function doEncode(string $decoded): string
{
return bin2hex($decoded);
}

protected function doDecode(string $encoded): string
{
$decoded = hex2bin($encoded);
if ($decoded === false) {
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
}
return $decoded;
}
}
7 changes: 7 additions & 0 deletions src/CoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function base64(): static;
*/
public function checksum(?string $algorithm = null): static;

/**
* @see Coder\Hex
*
* @throws TExceptionCouldNotProcessData
*/
public function hex(): static;

/**
* @see Coder\zlib
*
Expand Down
7 changes: 7 additions & 0 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public function checksum(?string $algorithm = null): static
));
}

public function hex(): static
{
return static::create($this, (new Coder\Hex())->decode(
$this->data,
));
}

public function zlib(?int $maxLength = null): static
{
return static::create($this, (new Coder\Zlib())->decode(
Expand Down
7 changes: 7 additions & 0 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public function checksum(?string $algorithm = null): static
));
}

public function hex(): static
{
return static::create($this, (new Coder\Hex())->encode(
$this->data,
));
}

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

namespace PetrKnap\Binary\Coder;

use PHPUnit\Framework\Attributes\DataProvider;

final class HexTest extends CoderTestCase
{
public static function data(): array
{
return [
[
self::getDecodedData(),
'da39a3ee5e6b4b0d3255bfef95601890afd80709da39a3ee5e6b4b0d3255bfef95601890afd80709da39a3ee5e6b4b0d3255bfef95601890afd80709',
],
];
}

#[DataProvider('data')]
public function testEncodes(string $decoded, string $encoded): void
{
self::assertSame(
$encoded,
(new Hex())->encode(
$decoded,
),
);
}

#[DataProvider('data')]
public function testDecodes(string $decoded, string $encoded): void
{
self::assertSame(
$decoded,
(new Hex())->decode(
$encoded,
),
);
}

#[DataProvider('dataDecodeThrows')]
public function testDecodeThrows(string $data): void
{
self::expectException(Exception\CouldNotDecodeData::class);

(new Hex())->decode(
$data,
);
}

public static function dataDecodeThrows(): array
{
return [
'wrong data' => ['?'],
];
}
}
8 changes: 8 additions & 0 deletions tests/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function testDecodesChecksum(): void
);
}

public function testDecodesHex(): void
{
self::assertSame(
Coder\HexTest::getDecodedData(),
(new Decoder(Coder\HexTest::getEncodedData()))->hex()->getData(),
);
}

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

public function testEncodesHex(): void
{
self::assertSame(
Coder\HexTest::getEncodedData(),
(new Encoder(Coder\HexTest::getDecodedData()))->hex()->getData(),
);
}

public function testEncodesZlib(): void
{
self::assertSame(
Expand Down

0 comments on commit 5a9b322

Please sign in to comment.