Skip to content

Commit

Permalink
Merge pull request #11 from petrknap/stringable
Browse files Browse the repository at this point in the history
Implemented helpers for one-way object to binary string conversion
  • Loading branch information
petrknap committed Apr 22, 2024
2 parents 12ab4a5 + 52d7147 commit b2c28ce
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/BinariableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary;

use Stringable;

interface BinariableInterface extends Stringable
{
/**
* @return string binary representation of this instance
*
* @throws Exception\CouldNotConvertToBinary
*/
public function toBinary(): string;
}
27 changes: 27 additions & 0 deletions src/BinariableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary;

use PetrKnap\Shorts\Exception\NotImplemented;

trait BinariableTrait
{
/**
* Unfortunately PHP uses string for binary data, so the magic clashes.
*/
public function __toString(): string
{
if (!($this instanceof BinariableInterface)) {
NotImplemented::throw(BinariableInterface::class);
}

$binary = $this->toBinary();
trigger_error(
'Returned binary string',
error_level: E_USER_NOTICE,
);
return $binary;
}
}
9 changes: 9 additions & 0 deletions src/Exception/BinariableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary\Exception;

interface BinariableException extends BinaryException
{
}
14 changes: 14 additions & 0 deletions src/Exception/CouldNotConvertToBinary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary\Exception;

use PetrKnap\Shorts\Exception\CouldNotProcessData;

/**
* @extends CouldNotProcessData<object>
*/
final class CouldNotConvertToBinary extends CouldNotProcessData implements BinariableException
{
}
55 changes: 55 additions & 0 deletions tests/BinariableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Binary;

use PHPUnit\Framework\TestCase;

final class BinariableTest extends TestCase
{
public const DATA = b'data';

public function testExplicitConversionWorks(): void
{
self::assertSame(
self::DATA,
self::getBinariable()->toBinary(),
);
}

public function testNativeConversionWorks(): void
{
self::assertSame(
self::DATA,
(string) self::getBinariable(),
);
}

public function testNativeComparisonWorks(): void
{
self::assertTrue(self::DATA == self::getBinariable());
}

public function testUnionTypingWorks(): void
{
$function = static fn (BinariableInterface|string $parameter): string => (string) $parameter;

self::assertSame(
self::DATA,
$function(self::getBinariable()),
);
}

private function getBinariable(): BinariableInterface
{
return new class () implements BinariableInterface {
use BinariableTrait;

public function toBinary(): string
{
return BinariableTest::DATA;
}
};
}
}

0 comments on commit b2c28ce

Please sign in to comment.