Skip to content

Commit 39adf3d

Browse files
authored
Merge pull request #4 from petrknap/serializer
Implemented `IgbinarySerializer`
2 parents 306982e + 1abbb42 commit 39adf3d

File tree

4 files changed

+98
-7
lines changed

4 files changed

+98
-7
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
"license": "LGPL-3.0-or-later",
3737
"name": "petrknap/binary",
3838
"require": {
39+
"petrknap/shorts": "^1.4",
3940
"php": ">=8.1"
4041
},
4142
"require-dev": {
4243
"ext-mbstring": "*",
4344
"ext-zlib": "*",
4445
"nunomaduro/phpinsights": "^2.11",
45-
"petrknap/shorts": "^1.3",
4646
"phpstan/phpstan": "^1.10",
4747
"phpunit/phpunit": "^10.5",
4848
"squizlabs/php_codesniffer": "^3.7"
@@ -61,6 +61,7 @@
6161
]
6262
},
6363
"suggest": {
64+
"ext-igbinary": "Required to serialize data via igbinary",
6465
"ext-mbstring": "Required to check checksum",
6566
"ext-zlib": "Required to compress data"
6667
}

src/IgbinarySerializer.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PetrKnap\Binary;
6+
7+
use PetrKnap\Shorts\Exception\MissingRequirement;
8+
use PetrKnap\Shorts\HasRequirements;
9+
10+
/**
11+
* @link https://www.php.net/manual/en/book.igbinary.php
12+
*/
13+
final class IgbinarySerializer extends Serializer implements HasRequirements
14+
{
15+
public function __construct(EncoderInterface $encoder, DecoderInterface $decoder)
16+
{
17+
self::checkRequirements();
18+
parent::__construct($encoder, $decoder);
19+
}
20+
21+
protected function doSerialize(mixed $serializable): string
22+
{
23+
$serialized = igbinary_serialize($serializable);
24+
if ($serialized === null) {
25+
throw new Exception\CouldNotSerializeData($this, $serializable);
26+
}
27+
return $serialized;
28+
}
29+
30+
protected function doUnserialize(string $serialized): mixed
31+
{
32+
$serializable = igbinary_unserialize($serialized);
33+
if ($serializable === false) {
34+
throw new Exception\CouldNotUnserializeData($this, $serialized);
35+
}
36+
return $serializable;
37+
}
38+
39+
public static function checkRequirements(): void
40+
{
41+
$functions = [
42+
'igbinary_serialize',
43+
'igbinary_unserialize',
44+
];
45+
foreach ($functions as $function) {
46+
if (!function_exists($function)) {
47+
throw new MissingRequirement(self::class, 'function', $function);
48+
}
49+
}
50+
}
51+
}

tests/IgbinarySerializerTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PetrKnap\Binary;
4+
5+
use PetrKnap\Shorts\Exception\MissingRequirement;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* @todo add php-igbinary to Dockerfile
10+
* @todo add ext-igbinary to composer.json#require-dev
11+
*/
12+
final class IgbinarySerializerTest extends TestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
try {
18+
IgbinarySerializer::checkRequirements();
19+
} catch (MissingRequirement $reason) {
20+
self::markTestSkipped($reason->getMessage());
21+
}
22+
}
23+
24+
public function testSerializesData(): void
25+
{
26+
SerializerTest::doTestSerializesData(
27+
new IgbinarySerializer(
28+
new Encoder(),
29+
new Decoder(),
30+
),
31+
);
32+
}
33+
}

tests/SerializerTest.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class SerializerTest extends TestCase
1313
private SerializerInterface&MockObject $internalSerializer;
1414
private Serializer $serializer;
1515

16-
public function setUp(): void
16+
protected function setUp(): void
1717
{
1818
parent::setUp();
1919

@@ -44,7 +44,7 @@ protected function doUnserialize(string $serialized): mixed
4444
};
4545
}
4646

47-
public function testSerializesData(): void
47+
public static function doTestSerializesData(SerializerInterface $serializer): void
4848
{
4949
$data = new stdClass();
5050
$data->array = [];
@@ -53,10 +53,6 @@ public function testSerializesData(): void
5353
$data->int = 0;
5454
$data->null = null;
5555
$data->string = '';
56-
$serializer = new Serializer(
57-
new Encoder(),
58-
new Decoder(),
59-
);
6056

6157
self::assertEquals(
6258
$data,
@@ -68,6 +64,16 @@ public function testSerializesData(): void
6864
);
6965
}
7066

67+
public function testSerializesData(): void
68+
{
69+
self::doTestSerializesData(
70+
new Serializer(
71+
new Encoder(),
72+
new Decoder(),
73+
),
74+
);
75+
}
76+
7177
public function testCallsDoSerializeAndUsesEncoder(): void
7278
{
7379
$serializable = (string) 0b01;

0 commit comments

Comments
 (0)