-
-
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.
- Loading branch information
Showing
11 changed files
with
332 additions
and
3 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
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
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
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
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Exception; | ||
|
||
use PetrKnap\Binary\SerializerInterface; | ||
use RuntimeException; | ||
use Throwable; | ||
|
||
final class CouldNotSerializeData extends RuntimeException implements SerializerException | ||
{ | ||
public function __construct( | ||
private readonly SerializerInterface $serializer, | ||
private readonly mixed $data, | ||
?Throwable $reason = null, | ||
) { | ||
parent::__construct( | ||
sprintf( | ||
'%s could not serialize %s', | ||
$serializer::class, | ||
is_object($data) ? $data::class : gettype($data), | ||
), | ||
previous: $reason, | ||
); | ||
} | ||
|
||
public function getSerializer(): SerializerInterface | ||
{ | ||
return $this->serializer; | ||
} | ||
|
||
public function getData(): mixed | ||
{ | ||
return $this->data; | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Exception; | ||
|
||
use PetrKnap\Binary\SerializerInterface; | ||
use RuntimeException; | ||
use Throwable; | ||
|
||
final class CouldNotUnserializeData extends RuntimeException implements SerializerException | ||
{ | ||
public function __construct( | ||
private readonly SerializerInterface $serializer, | ||
private readonly string $data, | ||
?Throwable $reason = null, | ||
) { | ||
parent::__construct( | ||
sprintf( | ||
'%s could not unserialize string(%d)', | ||
$serializer::class, | ||
strlen($data) | ||
), | ||
previous: $reason, | ||
); | ||
} | ||
|
||
public function getSerializer(): SerializerInterface | ||
{ | ||
return $this->serializer; | ||
} | ||
|
||
public function getData(): string | ||
{ | ||
return $this->data; | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Exception; | ||
|
||
use PetrKnap\Binary\SerializerInterface; | ||
|
||
interface SerializerException extends BinaryException | ||
{ | ||
public function getSerializer(): SerializerInterface; | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
use Throwable; | ||
|
||
class Serializer implements SerializerInterface | ||
{ | ||
public function __construct( | ||
protected readonly EncoderInterface $encoder, | ||
protected readonly DecoderInterface $decoder, | ||
) { | ||
} | ||
|
||
public function serialize(mixed $serializable): string | ||
{ | ||
try { | ||
$serialized = $this->doSerialize($serializable); | ||
return $this->encoder->withData($serialized)->zlib()->getData(); | ||
} catch (Throwable $reason) { | ||
throw new Exception\CouldNotSerializeData($this, $serializable, $reason); | ||
} | ||
} | ||
|
||
public function unserialize(string $serialized): mixed | ||
{ | ||
try { | ||
$serialized = $this->decoder->withData($serialized)->zlib()->getData(); | ||
return $this->doUnserialize($serialized); | ||
} catch (Throwable $reason) { | ||
throw new Exception\CouldNotUnserializeData($this, $serialized, $reason); | ||
} | ||
} | ||
|
||
/** | ||
* Alternative to {@see serialize()} | ||
* | ||
* @throws Throwable | ||
*/ | ||
protected function doSerialize(mixed $serializable): string | ||
{ | ||
return serialize($serializable); | ||
} | ||
|
||
/** | ||
* Alternative to {@see unserialize()} | ||
* | ||
* @throws Throwable | ||
*/ | ||
protected function doUnserialize(string $serialized): mixed | ||
{ | ||
return unserialize($serialized); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
interface SerializerInterface | ||
{ | ||
/** | ||
* {@see serialize()} the serializable | ||
* | ||
* @throws Exception\CouldNotSerializeData | ||
*/ | ||
public function serialize(mixed $serializable): string; | ||
|
||
/** | ||
* {@see unserialize()} the serialized | ||
* | ||
* @throws Exception\CouldNotUnserializeData | ||
*/ | ||
public function unserialize(string $serialized): mixed; | ||
} |
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
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,125 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class SerializerTest extends TestCase | ||
{ | ||
private EncoderInterface&MockObject $internalEncoder; | ||
private DecoderInterface&MockObject $internalDecoder; | ||
private SerializerInterface&MockObject $internalSerializer; | ||
private Serializer $serializer; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->internalEncoder = self::createMock(EncoderInterface::class); | ||
$this->internalDecoder = self::createMock(DecoderInterface::class); | ||
$this->internalSerializer = self::createMock(SerializerInterface::class); | ||
|
||
$this->serializer = new class ( | ||
$this->internalEncoder, | ||
$this->internalDecoder, | ||
$this->internalSerializer, | ||
) extends Serializer { | ||
public function __construct( | ||
EncoderInterface $encoder, | ||
DecoderInterface $decoder, | ||
private readonly SerializerInterface $serializer, | ||
) { | ||
parent::__construct($encoder, $decoder); | ||
} | ||
protected function doSerialize(mixed $serializable): string | ||
{ | ||
return $this->serializer->serialize($serializable); | ||
} | ||
protected function doUnserialize(string $serialized): mixed | ||
{ | ||
return $this->serializer->unserialize($serialized); | ||
} | ||
}; | ||
} | ||
|
||
public function testSerializesData(): void | ||
{ | ||
$data = new stdClass(); | ||
$data->array = []; | ||
$data->binary = 0b0; | ||
$data->float = .0; | ||
$data->int = 0; | ||
$data->null = null; | ||
$data->string = ''; | ||
$serializer = new Serializer( | ||
new Encoder(), | ||
new Decoder(), | ||
); | ||
|
||
self::assertEquals( | ||
$data, | ||
$serializer->unserialize( | ||
$serializer->serialize( | ||
$data, | ||
), | ||
), | ||
); | ||
} | ||
|
||
public function testCallsDoSerializeAndUsesEncoder(): void | ||
{ | ||
$serializable = (string) 0b01; | ||
$serialized = (string) 0b10; | ||
$encoded = (string) 0b11; | ||
|
||
$this->internalSerializer->expects(self::once()) | ||
->method('serialize') | ||
->willReturn($serializable) | ||
->willReturn($serialized); | ||
$this->internalEncoder->expects(self::once()) | ||
->method('withData') | ||
->with($serialized) | ||
->willReturn($this->internalEncoder); | ||
$this->internalEncoder->expects(self::once()) | ||
->method('zlib') | ||
->willReturn($this->internalEncoder); | ||
$this->internalEncoder->expects(self::once()) | ||
->method('getData') | ||
->willReturn($encoded); | ||
|
||
self::assertSame( | ||
$encoded, | ||
$this->serializer->serialize($encoded), | ||
); | ||
} | ||
|
||
public function testUsesDecoderAndCallsDoUnserialize(): void | ||
{ | ||
$serialized = (string) 0b01; | ||
$decoded = (string) 0b10; | ||
$serializable = (string) 0b11; | ||
|
||
$this->internalDecoder->expects(self::once()) | ||
->method('withData') | ||
->with($serialized) | ||
->willReturn($this->internalDecoder); | ||
$this->internalDecoder->expects(self::once()) | ||
->method('zlib') | ||
->willReturn($this->internalDecoder); | ||
$this->internalDecoder->expects(self::once()) | ||
->method('getData') | ||
->willReturn($decoded); | ||
$this->internalSerializer->expects(self::once()) | ||
->method('unserialize') | ||
->with($decoded) | ||
->willReturn($serializable); | ||
|
||
self::assertSame( | ||
$serializable, | ||
$this->serializer->unserialize($serialized), | ||
); | ||
} | ||
} |