File tree 8 files changed +121
-0
lines changed 8 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 31
31
" decompression" ,
32
32
" encoder" ,
33
33
" helper" ,
34
+ " hexadecimal" ,
34
35
" igbinary" ,
35
36
" serializer" ,
36
37
" zlib"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -31,6 +31,13 @@ public function base64(): static;
31
31
*/
32
32
public function checksum (?string $ algorithm = null ): static ;
33
33
34
+ /**
35
+ * @see Coder\Hex
36
+ *
37
+ * @throws TExceptionCouldNotProcessData
38
+ */
39
+ public function hex (): static ;
40
+
34
41
/**
35
42
* @see Coder\zlib
36
43
*
Original file line number Diff line number Diff line change @@ -21,6 +21,13 @@ public function checksum(?string $algorithm = null): static
21
21
));
22
22
}
23
23
24
+ public function hex (): static
25
+ {
26
+ return static ::create ($ this , (new Coder \Hex ())->decode (
27
+ $ this ->data ,
28
+ ));
29
+ }
30
+
24
31
public function zlib (?int $ maxLength = null ): static
25
32
{
26
33
return static ::create ($ this , (new Coder \Zlib ())->decode (
Original file line number Diff line number Diff line change @@ -22,6 +22,13 @@ public function checksum(?string $algorithm = null): static
22
22
));
23
23
}
24
24
25
+ public function hex (): static
26
+ {
27
+ return static ::create ($ this , (new Coder \Hex ())->encode (
28
+ $ this ->data ,
29
+ ));
30
+ }
31
+
25
32
public function zlib (?int $ encoding = null , ?int $ level = null ): static
26
33
{
27
34
return static ::create ($ this , (new Coder \Zlib ())->encode (
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -22,6 +22,14 @@ public function testDecodesChecksum(): void
22
22
);
23
23
}
24
24
25
+ public function testDecodesHex (): void
26
+ {
27
+ self ::assertSame (
28
+ Coder \HexTest::getDecodedData (),
29
+ (new Decoder (Coder \HexTest::getEncodedData ()))->hex ()->getData (),
30
+ );
31
+ }
32
+
25
33
public function testDecodesZlib (): void
26
34
{
27
35
self ::assertSame (
Original file line number Diff line number Diff line change @@ -22,6 +22,14 @@ public function testEncodesChecksum(): void
22
22
);
23
23
}
24
24
25
+ public function testEncodesHex (): void
26
+ {
27
+ self ::assertSame (
28
+ Coder \HexTest::getEncodedData (),
29
+ (new Encoder (Coder \HexTest::getDecodedData ()))->hex ()->getData (),
30
+ );
31
+ }
32
+
25
33
public function testEncodesZlib (): void
26
34
{
27
35
self ::assertSame (
You can’t perform that action at this time.
0 commit comments