-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented helpers for one-way object to string conversion
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
use Stringable; | ||
use Throwable; | ||
|
||
interface StringableInterface extends Stringable | ||
{ | ||
/** | ||
* @return string binary representation of this instance | ||
* | ||
* @throws Throwable | ||
*/ | ||
public function toBinary(): string; | ||
|
||
/** | ||
* @see self::toBinary() | ||
*/ | ||
public function __toString(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
trait StringableTrait | ||
{ | ||
public function __toString(): string | ||
{ | ||
/** @var StringableInterface $this */ | ||
return $this->toBinary(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class StringableTest extends TestCase | ||
{ | ||
public const DATA = b'data'; | ||
|
||
public function testToBinaryMethodWorks(): void | ||
{ | ||
self::assertSame( | ||
self::DATA, | ||
self::getStringableInstance()->toBinary(), | ||
); | ||
} | ||
|
||
public function testNativeConversionWorks(): void | ||
{ | ||
self::assertSame( | ||
self::DATA, | ||
(string) self::getStringableInstance(), | ||
); | ||
} | ||
|
||
private function getStringableInstance(): StringableInterface | ||
{ | ||
return new class () implements StringableInterface { | ||
use StringableTrait; | ||
|
||
public function toBinary(): string | ||
{ | ||
return StringableTest::DATA; | ||
} | ||
}; | ||
} | ||
} |