From 8b63c5debd9ae71537879ee207521c6998f5e74e Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sun, 27 Apr 2025 20:08:27 +0200 Subject: [PATCH] feat: stringable coder --- README.md | 4 ++-- src/Coder.php | 23 +++++++++++++++++++---- tests/DecoderTest.php | 8 ++++---- tests/EncoderTest.php | 8 ++++---- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f420ade..5a64616 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ See the examples below for more information, or check out [`Encoder`](./src/Enco namespace PetrKnap\Binary; $data = base64_decode('hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A=='); -$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->getData(); -$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->getData(); +$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->data; +$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->data; printf('Data was coded into `%s` %s.', $encoded, $decoded === $data ? 'successfully' : 'unsuccessfully'); ``` diff --git a/src/Coder.php b/src/Coder.php index ea14e4e..b70ea98 100644 --- a/src/Coder.php +++ b/src/Coder.php @@ -5,6 +5,7 @@ namespace PetrKnap\Binary; use PetrKnap\Shorts\Exception; +use Stringable; /** * @internal please use subclass @@ -13,10 +14,13 @@ * * @implements CoderInterface */ -abstract class Coder implements CoderInterface +abstract class Coder implements CoderInterface, Stringable { - public function __construct( - protected readonly string $data = '', + /** + * @param string $data may contain binary data + */ + final public function __construct( + public readonly string $data = '', ) { } @@ -25,7 +29,18 @@ public function withData(string $data): static return static::create($this, $data); } - public function getData(): string + /** + * @deprecated use readonly property {@see self::$data} + */ + final public function getData(): string + { + return $this->data; + } + + /** + * @note this is just a helper, this class is not supposed to implement {@see BinariableInterface} + */ + public function __toString(): string { return $this->data; } diff --git a/tests/DecoderTest.php b/tests/DecoderTest.php index 57ea075..4f69a11 100644 --- a/tests/DecoderTest.php +++ b/tests/DecoderTest.php @@ -12,7 +12,7 @@ public function testDecodesBase64(): void { self::assertSame( Coder\Base64Test::getDecodedData(), - (new Decoder(Coder\Base64Test::getEncodedData()))->base64()->getData(), + (new Decoder(Coder\Base64Test::getEncodedData()))->base64()->data, ); } @@ -20,7 +20,7 @@ public function testDecodesChecksum(): void { self::assertSame( Coder\ChecksumTest::getDecodedData(), - (new Decoder(Coder\ChecksumTest::getEncodedData()))->checksum()->getData(), + (new Decoder(Coder\ChecksumTest::getEncodedData()))->checksum()->data, ); } @@ -28,7 +28,7 @@ public function testDecodesHex(): void { self::assertSame( Coder\HexTest::getDecodedData(), - (new Decoder(Coder\HexTest::getEncodedData()))->hex()->getData(), + (new Decoder(Coder\HexTest::getEncodedData()))->hex()->data, ); } @@ -36,7 +36,7 @@ public function testDecodesZlib(): void { self::assertSame( Coder\ZlibTest::getDecodedData(), - (new Decoder(Coder\ZlibTest::getEncodedData()))->zlib()->getData(), + (new Decoder(Coder\ZlibTest::getEncodedData()))->zlib()->data, ); } } diff --git a/tests/EncoderTest.php b/tests/EncoderTest.php index cc1be0a..ce1192a 100644 --- a/tests/EncoderTest.php +++ b/tests/EncoderTest.php @@ -12,7 +12,7 @@ public function testEncodesBase64(): void { self::assertSame( Coder\Base64Test::getEncodedData(), - (new Encoder(Coder\Base64Test::getDecodedData()))->base64()->getData(), + (new Encoder(Coder\Base64Test::getDecodedData()))->base64()->data, ); } @@ -20,7 +20,7 @@ public function testEncodesChecksum(): void { self::assertSame( Coder\ChecksumTest::getEncodedData(), - (new Encoder(Coder\ChecksumTest::getDecodedData()))->checksum()->getData(), + (new Encoder(Coder\ChecksumTest::getDecodedData()))->checksum()->data, ); } @@ -28,7 +28,7 @@ public function testEncodesHex(): void { self::assertSame( Coder\HexTest::getEncodedData(), - (new Encoder(Coder\HexTest::getDecodedData()))->hex()->getData(), + (new Encoder(Coder\HexTest::getDecodedData()))->hex()->data, ); } @@ -36,7 +36,7 @@ public function testEncodesZlib(): void { self::assertSame( Coder\ZlibTest::getEncodedData(), - (new Encoder(Coder\ZlibTest::getDecodedData()))->zlib()->getData(), + (new Encoder(Coder\ZlibTest::getDecodedData()))->zlib()->data, ); } }