Skip to content

Commit 96d5192

Browse files
authored
Merge pull request #13 from petrknap/binariable-helper
Added `Binary::asBinary`
2 parents 3cde0d0 + b8c54eb commit 96d5192

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Simple library for work with binary data and objects in PHP.
44
See the examples below for more information, or check out [`Encoder`](./src/Encoder.php), [`Decoder`](./src/Decoder.php), [`Serializer`](./src/Serializer.php) and [`Byter`](./src/Byter.php).
55

66
```php
7-
use PetrKnap\Binary\Binary;
7+
namespace PetrKnap\Binary;
88

99
$data = base64_decode('hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==');
1010
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->getData();
@@ -14,7 +14,7 @@ printf('Data was coded into `%s` %s.', $encoded, $decoded === $data ? 'successfu
1414
```
1515

1616
```php
17-
use PetrKnap\Binary\Binary;
17+
namespace PetrKnap\Binary;
1818

1919
$data = [
2020
'type' => 'image/png',
@@ -27,7 +27,35 @@ printf('Data was serialized into `%s` %s.', base64_encode($serialized), $unseria
2727
```
2828

2929
```php
30-
use PetrKnap\Binary\Binary;
30+
namespace PetrKnap\Binary;
31+
32+
class DataObject implements Serializer\SelfSerializerInterface
33+
{
34+
use Serializer\SelfSerializerTrait;
35+
36+
public function __construct(
37+
public string $data,
38+
) {
39+
$this->referencesToConstructorArgs = [
40+
&$this->data,
41+
];
42+
}
43+
}
44+
45+
$instance = new DataObject('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
46+
$instance->data .= ' Duis venenatis ultricies elementum.';
47+
$binary = $instance->toBinary();
48+
$binaryFromHelper = Binary::asBinary($instance);
49+
50+
printf(
51+
'Data object was serialized into `%s` %s.',
52+
base64_encode($binary),
53+
$binary === $binaryFromHelper && $instance == DataObject::fromBinary($binary) ? 'successfully' : 'unsuccessfully',
54+
);
55+
```
56+
57+
```php
58+
namespace PetrKnap\Binary;
3159

3260
$data = base64_decode('hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==');
3361
$sha1 = sha1($data, binary: true);

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 asBinary(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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
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
11-
* accept it as an union type `YourClass|string` and call {@see SelfSerializerInterface::fromBinary()} if it is a string.
11+
* accept it as a union type `YourClass|string` and call {@see SelfSerializerInterface::fromBinary()} if it is a string.
1212
*/
1313
trait SelfSerializerTrait
1414
{
@@ -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::asBinary($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 {

tests/ReadmeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static function getExpectedOutputsOfPhpExamples(): iterable
2222
return [
2323
'coder' => 'Data was coded into `a8vMFCssyD2Rs5BB0Evt6tJv10J_b2Aoui0tcXT69aaPP9oIyB-fLeAHAA` successfully.',
2424
'serializer' => 'Data was serialized into `S7QysqoutjKxUiqpLEhVsi62srRSysxNTE/VL8hLB/GBUimJJYkgpoWxlVJngJ87L5cUFwMDA6+nh0sQkGYEYQ42ICkveqQTxCkOcndiWHdO5iVYlYtjiER48o/9Ux7aM7C9Z1qixnnFBCjB4Onq57LOKaFJyboWAA==` successfully.',
25+
'self-serializer' => 'Data object was serialized into `DckxCsMwDAXQq4jMJbTd6qwdewnjfMoHSw6W1KX07s324NVyK1+W6+blcS/La0yo8PBU2UcfU5whVREXacMcLRA5pe486I32FnTGKs+kywcGq3Eqe0w2ws+GwiJ1XbbfHw==` successfully.',
2526
'byter' => 'Hashes and data was unbitten into `IoPwxcGHZQM0gfF966vHI3kleehoRKHtC32Xh30RDlg5E026hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==` successfully.',
2627
];
2728
}

0 commit comments

Comments
 (0)