From 86c7f34bf048154065d1b5ec6aa4f8fa2d5d1218 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:18:38 +0200 Subject: [PATCH] feat: implemented hexadecimal coder --- composer.json | 1 + src/Coder/Hex.php | 26 +++++++++++++++++++ src/CoderInterface.php | 7 +++++ src/Decoder.php | 7 +++++ src/Encoder.php | 7 +++++ tests/Coder/HexTest.php | 57 +++++++++++++++++++++++++++++++++++++++++ tests/DecoderTest.php | 8 ++++++ tests/EncoderTest.php | 8 ++++++ 8 files changed, 121 insertions(+) create mode 100644 src/Coder/Hex.php create mode 100644 tests/Coder/HexTest.php diff --git a/composer.json b/composer.json index a7a3d11..dcbc73f 100755 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "decompression", "encoder", "helper", + "hexadecimal", "igbinary", "serializer", "zlib" diff --git a/src/Coder/Hex.php b/src/Coder/Hex.php new file mode 100644 index 0000000..daf16a6 --- /dev/null +++ b/src/Coder/Hex.php @@ -0,0 +1,26 @@ +decode( + $this->data, + )); + } + public function zlib(?int $maxLength = null): static { return static::create($this, (new Coder\Zlib())->decode( diff --git a/src/Encoder.php b/src/Encoder.php index 914ff51..3575691 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -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( diff --git a/tests/Coder/HexTest.php b/tests/Coder/HexTest.php new file mode 100644 index 0000000..70a0f6f --- /dev/null +++ b/tests/Coder/HexTest.php @@ -0,0 +1,57 @@ +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' => ['?'], + ]; + } +} diff --git a/tests/DecoderTest.php b/tests/DecoderTest.php index ff69e3a..65e87a9 100644 --- a/tests/DecoderTest.php +++ b/tests/DecoderTest.php @@ -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( diff --git a/tests/EncoderTest.php b/tests/EncoderTest.php index 21574e6..470f242 100644 --- a/tests/EncoderTest.php +++ b/tests/EncoderTest.php @@ -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(