Skip to content

Commit 5a9b322

Browse files
authored
Merge pull request #6 from petrknap/hex-coder
Implemented hexadecimal coder
2 parents 7ca302c + 86c7f34 commit 5a9b322

File tree

8 files changed

+121
-0
lines changed

8 files changed

+121
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"decompression",
3232
"encoder",
3333
"helper",
34+
"hexadecimal",
3435
"igbinary",
3536
"serializer",
3637
"zlib"

src/Coder/Hex.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary\Coder;
6+
7+
/**
8+
* @see bin2hex()
9+
* @see hex2bin()
10+
*/
11+
final class Hex extends Coder
12+
{
13+
protected function doEncode(string $decoded): string
14+
{
15+
return bin2hex($decoded);
16+
}
17+
18+
protected function doDecode(string $encoded): string
19+
{
20+
$decoded = hex2bin($encoded);
21+
if ($decoded === false) {
22+
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
23+
}
24+
return $decoded;
25+
}
26+
}

src/CoderInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public function base64(): static;
3131
*/
3232
public function checksum(?string $algorithm = null): static;
3333

34+
/**
35+
* @see Coder\Hex
36+
*
37+
* @throws TExceptionCouldNotProcessData
38+
*/
39+
public function hex(): static;
40+
3441
/**
3542
* @see Coder\zlib
3643
*

src/Decoder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public function checksum(?string $algorithm = null): static
2121
));
2222
}
2323

24+
public function hex(): static
25+
{
26+
return static::create($this, (new Coder\Hex())->decode(
27+
$this->data,
28+
));
29+
}
30+
2431
public function zlib(?int $maxLength = null): static
2532
{
2633
return static::create($this, (new Coder\Zlib())->decode(

src/Encoder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public function checksum(?string $algorithm = null): static
2222
));
2323
}
2424

25+
public function hex(): static
26+
{
27+
return static::create($this, (new Coder\Hex())->encode(
28+
$this->data,
29+
));
30+
}
31+
2532
public function zlib(?int $encoding = null, ?int $level = null): static
2633
{
2734
return static::create($this, (new Coder\Zlib())->encode(

tests/Coder/HexTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PetrKnap\Binary\Coder;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
7+
final class HexTest extends CoderTestCase
8+
{
9+
public static function data(): array
10+
{
11+
return [
12+
[
13+
self::getDecodedData(),
14+
'da39a3ee5e6b4b0d3255bfef95601890afd80709da39a3ee5e6b4b0d3255bfef95601890afd80709da39a3ee5e6b4b0d3255bfef95601890afd80709',
15+
],
16+
];
17+
}
18+
19+
#[DataProvider('data')]
20+
public function testEncodes(string $decoded, string $encoded): void
21+
{
22+
self::assertSame(
23+
$encoded,
24+
(new Hex())->encode(
25+
$decoded,
26+
),
27+
);
28+
}
29+
30+
#[DataProvider('data')]
31+
public function testDecodes(string $decoded, string $encoded): void
32+
{
33+
self::assertSame(
34+
$decoded,
35+
(new Hex())->decode(
36+
$encoded,
37+
),
38+
);
39+
}
40+
41+
#[DataProvider('dataDecodeThrows')]
42+
public function testDecodeThrows(string $data): void
43+
{
44+
self::expectException(Exception\CouldNotDecodeData::class);
45+
46+
(new Hex())->decode(
47+
$data,
48+
);
49+
}
50+
51+
public static function dataDecodeThrows(): array
52+
{
53+
return [
54+
'wrong data' => ['?'],
55+
];
56+
}
57+
}

tests/DecoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public function testDecodesChecksum(): void
2222
);
2323
}
2424

25+
public function testDecodesHex(): void
26+
{
27+
self::assertSame(
28+
Coder\HexTest::getDecodedData(),
29+
(new Decoder(Coder\HexTest::getEncodedData()))->hex()->getData(),
30+
);
31+
}
32+
2533
public function testDecodesZlib(): void
2634
{
2735
self::assertSame(

tests/EncoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public function testEncodesChecksum(): void
2222
);
2323
}
2424

25+
public function testEncodesHex(): void
26+
{
27+
self::assertSame(
28+
Coder\HexTest::getEncodedData(),
29+
(new Encoder(Coder\HexTest::getDecodedData()))->hex()->getData(),
30+
);
31+
}
32+
2533
public function testEncodesZlib(): void
2634
{
2735
self::assertSame(

0 commit comments

Comments
 (0)