Skip to content

Commit 8b63c5d

Browse files
committed
feat: stringable coder
1 parent 70f9d51 commit 8b63c5d

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ See the examples below for more information, or check out [`Encoder`](./src/Enco
77
namespace PetrKnap\Binary;
88

99
$data = base64_decode('hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==');
10-
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->getData();
11-
$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->getData();
10+
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->data;
11+
$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->data;
1212

1313
printf('Data was coded into `%s` %s.', $encoded, $decoded === $data ? 'successfully' : 'unsuccessfully');
1414
```

src/Coder.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PetrKnap\Binary;
66

77
use PetrKnap\Shorts\Exception;
8+
use Stringable;
89

910
/**
1011
* @internal please use subclass
@@ -13,10 +14,13 @@
1314
*
1415
* @implements CoderInterface<Exception\CouldNotProcessData>
1516
*/
16-
abstract class Coder implements CoderInterface
17+
abstract class Coder implements CoderInterface, Stringable
1718
{
18-
public function __construct(
19-
protected readonly string $data = '',
19+
/**
20+
* @param string $data may contain binary data
21+
*/
22+
final public function __construct(
23+
public readonly string $data = '',
2024
) {
2125
}
2226

@@ -25,7 +29,18 @@ public function withData(string $data): static
2529
return static::create($this, $data);
2630
}
2731

28-
public function getData(): string
32+
/**
33+
* @deprecated use readonly property {@see self::$data}
34+
*/
35+
final public function getData(): string
36+
{
37+
return $this->data;
38+
}
39+
40+
/**
41+
* @note this is just a helper, this class is not supposed to implement {@see BinariableInterface}
42+
*/
43+
public function __toString(): string
2944
{
3045
return $this->data;
3146
}

tests/DecoderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ public function testDecodesBase64(): void
1212
{
1313
self::assertSame(
1414
Coder\Base64Test::getDecodedData(),
15-
(new Decoder(Coder\Base64Test::getEncodedData()))->base64()->getData(),
15+
(new Decoder(Coder\Base64Test::getEncodedData()))->base64()->data,
1616
);
1717
}
1818

1919
public function testDecodesChecksum(): void
2020
{
2121
self::assertSame(
2222
Coder\ChecksumTest::getDecodedData(),
23-
(new Decoder(Coder\ChecksumTest::getEncodedData()))->checksum()->getData(),
23+
(new Decoder(Coder\ChecksumTest::getEncodedData()))->checksum()->data,
2424
);
2525
}
2626

2727
public function testDecodesHex(): void
2828
{
2929
self::assertSame(
3030
Coder\HexTest::getDecodedData(),
31-
(new Decoder(Coder\HexTest::getEncodedData()))->hex()->getData(),
31+
(new Decoder(Coder\HexTest::getEncodedData()))->hex()->data,
3232
);
3333
}
3434

3535
public function testDecodesZlib(): void
3636
{
3737
self::assertSame(
3838
Coder\ZlibTest::getDecodedData(),
39-
(new Decoder(Coder\ZlibTest::getEncodedData()))->zlib()->getData(),
39+
(new Decoder(Coder\ZlibTest::getEncodedData()))->zlib()->data,
4040
);
4141
}
4242
}

tests/EncoderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ public function testEncodesBase64(): void
1212
{
1313
self::assertSame(
1414
Coder\Base64Test::getEncodedData(),
15-
(new Encoder(Coder\Base64Test::getDecodedData()))->base64()->getData(),
15+
(new Encoder(Coder\Base64Test::getDecodedData()))->base64()->data,
1616
);
1717
}
1818

1919
public function testEncodesChecksum(): void
2020
{
2121
self::assertSame(
2222
Coder\ChecksumTest::getEncodedData(),
23-
(new Encoder(Coder\ChecksumTest::getDecodedData()))->checksum()->getData(),
23+
(new Encoder(Coder\ChecksumTest::getDecodedData()))->checksum()->data,
2424
);
2525
}
2626

2727
public function testEncodesHex(): void
2828
{
2929
self::assertSame(
3030
Coder\HexTest::getEncodedData(),
31-
(new Encoder(Coder\HexTest::getDecodedData()))->hex()->getData(),
31+
(new Encoder(Coder\HexTest::getDecodedData()))->hex()->data,
3232
);
3333
}
3434

3535
public function testEncodesZlib(): void
3636
{
3737
self::assertSame(
3838
Coder\ZlibTest::getEncodedData(),
39-
(new Encoder(Coder\ZlibTest::getDecodedData()))->zlib()->getData(),
39+
(new Encoder(Coder\ZlibTest::getDecodedData()))->zlib()->data,
4040
);
4141
}
4242
}

0 commit comments

Comments
 (0)