Skip to content

Commit 8cf713c

Browse files
committed
feat: added Binary::toBinary
chore: self-serializer now uses `Binary` helper instead of own instance of `Serializer`
1 parent 3cde0d0 commit 8cf713c

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/Binary.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,26 @@ public static function decode(string $data): DecoderInterface
2121
*/
2222
public static function serialize(mixed $data): string
2323
{
24-
return (new Serializer())->serialize(serializable: $data);
24+
return self::serializer()->serialize(serializable: $data);
2525
}
2626

2727
/**
2828
* @see Serializer::unserialize()
2929
*/
3030
public static function unserialize(string $data): mixed
3131
{
32-
return (new Serializer())->unserialize(serialized: $data);
32+
return self::serializer()->unserialize(serialized: $data);
33+
}
34+
35+
/**
36+
* @see Serializer\OneWaySelfSerializerInterface::toBinary()
37+
*/
38+
public static function toBinary(Serializer\OneWaySelfSerializerInterface|string $data): string
39+
{
40+
if ($data instanceof Serializer\OneWaySelfSerializerInterface) {
41+
return $data->toBinary();
42+
}
43+
return $data;
3344
}
3445

3546
/**
@@ -39,14 +50,26 @@ public static function unserialize(string $data): mixed
3950
*/
4051
public static function bite(string $data, int $size1, int ...$sizeN): array
4152
{
42-
return (new Byter())->bite($data, $size1, ...$sizeN);
53+
return self::byter()->bite($data, $size1, ...$sizeN);
4354
}
4455

4556
/**
4657
* @see Byter::unbite()
4758
*/
4859
public static function unbite(string $bite1, string ...$biteN): string
4960
{
50-
return (new Byter())->unbite($bite1, ...$biteN);
61+
return self::byter()->unbite($bite1, ...$biteN);
62+
}
63+
64+
private static function serializer(): Serializer
65+
{
66+
static $serializer;
67+
return $serializer ??= new Serializer();
68+
}
69+
70+
private static function byter(): Byter
71+
{
72+
static $byter;
73+
return $byter ??= new Byter();
5174
}
5275
}

src/Serializer/SelfSerializerTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PetrKnap\Binary\Serializer;
66

7-
use PetrKnap\Binary\Serializer;
7+
use PetrKnap\Binary\Binary;
88

99
/**
1010
* If your {@see self::__construct()} argument is an instance of {@see SelfSerializerInterface} then
@@ -19,7 +19,7 @@ trait SelfSerializerTrait
1919

2020
public function toBinary(): string
2121
{
22-
return (new Serializer())->serialize(array_map(
22+
return Binary::serialize(array_map(
2323
static fn (mixed $constructorArg): mixed => match ($constructorArg instanceof SelfSerializerInterface) {
2424
true => $constructorArg->toBinary(),
2525
false => $constructorArg,
@@ -30,6 +30,6 @@ public function toBinary(): string
3030

3131
public static function fromBinary(string $data): self
3232
{
33-
return new self(...(new Serializer())->unserialize($data));
33+
return new self(...Binary::unserialize($data));
3434
}
3535
}

tests/BinariableTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PetrKnap\Binary;
66

7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use PHPUnit\Framework\TestCase;
89

910
final class BinariableTest extends TestCase
@@ -23,16 +24,24 @@ public function testNativeComparisonWorks(): void
2324
self::assertTrue(self::BINARY == self::getInstance());
2425
}
2526

26-
public function testUnionTypingWorks(): void
27+
#[DataProvider('unionTypeDataProvider')]
28+
public function testConversionHelperWorks(BinariableInterface|string $data): void
2729
{
28-
$function = static fn (BinariableInterface|string $parameter): string => (string) $parameter;
29-
3030
self::assertSame(
3131
self::BINARY,
32-
$function(self::getInstance()),
32+
Binary::toBinary($data),
3333
);
3434
}
3535

36+
public static function unionTypeDataProvider(): array
37+
{
38+
$instance = self::getInstance();
39+
return [
40+
BinariableInterface::class => [$instance],
41+
'binary' => [$instance->toBinary()],
42+
];
43+
}
44+
3645
private static function getInstance(): BinariableInterface
3746
{
3847
return new class () implements BinariableInterface {

0 commit comments

Comments
 (0)