Skip to content

Commit 52d7147

Browse files
committed
feat: implemented helpers for one-way object to binary string conversion
1 parent 12ab4a5 commit 52d7147

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

src/BinariableInterface.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary;
6+
7+
use Stringable;
8+
9+
interface BinariableInterface extends Stringable
10+
{
11+
/**
12+
* @return string binary representation of this instance
13+
*
14+
* @throws Exception\CouldNotConvertToBinary
15+
*/
16+
public function toBinary(): string;
17+
}

src/BinariableTrait.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary;
6+
7+
use PetrKnap\Shorts\Exception\NotImplemented;
8+
9+
trait BinariableTrait
10+
{
11+
/**
12+
* Unfortunately PHP uses string for binary data, so the magic clashes.
13+
*/
14+
public function __toString(): string
15+
{
16+
if (!($this instanceof BinariableInterface)) {
17+
NotImplemented::throw(BinariableInterface::class);
18+
}
19+
20+
$binary = $this->toBinary();
21+
trigger_error(
22+
'Returned binary string',
23+
error_level: E_USER_NOTICE,
24+
);
25+
return $binary;
26+
}
27+
}

src/Exception/BinariableException.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary\Exception;
6+
7+
interface BinariableException extends BinaryException
8+
{
9+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary\Exception;
6+
7+
use PetrKnap\Shorts\Exception\CouldNotProcessData;
8+
9+
/**
10+
* @extends CouldNotProcessData<object>
11+
*/
12+
final class CouldNotConvertToBinary extends CouldNotProcessData implements BinariableException
13+
{
14+
}

tests/BinariableTest.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class BinariableTest extends TestCase
10+
{
11+
public const DATA = b'data';
12+
13+
public function testExplicitConversionWorks(): void
14+
{
15+
self::assertSame(
16+
self::DATA,
17+
self::getBinariable()->toBinary(),
18+
);
19+
}
20+
21+
public function testNativeConversionWorks(): void
22+
{
23+
self::assertSame(
24+
self::DATA,
25+
(string) self::getBinariable(),
26+
);
27+
}
28+
29+
public function testNativeComparisonWorks(): void
30+
{
31+
self::assertTrue(self::DATA == self::getBinariable());
32+
}
33+
34+
public function testUnionTypingWorks(): void
35+
{
36+
$function = static fn (BinariableInterface|string $parameter): string => (string) $parameter;
37+
38+
self::assertSame(
39+
self::DATA,
40+
$function(self::getBinariable()),
41+
);
42+
}
43+
44+
private function getBinariable(): BinariableInterface
45+
{
46+
return new class () implements BinariableInterface {
47+
use BinariableTrait;
48+
49+
public function toBinary(): string
50+
{
51+
return BinariableTest::DATA;
52+
}
53+
};
54+
}
55+
}

0 commit comments

Comments
 (0)