Skip to content

Commit e184ef7

Browse files
authored
Merge pull request #15 from petrknap/optionals
Now uses `petrknap/optional`
2 parents 2227dd1 + 9521f53 commit e184ef7

File tree

8 files changed

+46
-45
lines changed

8 files changed

+46
-45
lines changed

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ RUN apt-get update \
1212
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
1313
# endregion
1414

15+
# region included igbinary
16+
# hadolint ignore=DL3008
17+
RUN pecl install -o -f \
18+
igbinary \
19+
&& docker-php-ext-enable \
20+
igbinary \
21+
;
22+
# endregion
23+
1524
# region included composer-library
1625
WORKDIR /app
1726
COPY . .

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
"name": "petrknap/binary",
4141
"require": {
4242
"php": ">=8.1",
43+
"petrknap/optional": "^3.1",
4344
"petrknap/shorts": "^2.0"
4445
},
4546
"require-dev": {
47+
"ext-igbinary": "*",
4648
"ext-mbstring": "*",
4749
"ext-zlib": "*",
4850
"nunomaduro/phpinsights": "^2.11",
@@ -51,7 +53,7 @@
5153
"squizlabs/php_codesniffer": "^3.7"
5254
},
5355
"scripts": {
54-
"test": "phpunit --colors=always --testdox tests",
56+
"test": "@test-implementation",
5557
"ci-script": [
5658
"@check-implementation",
5759
"@check-requirements",
@@ -68,7 +70,7 @@
6870
"composer outdated \"petrknap/*\" --major-only --strict --ansi --no-interaction"
6971
],
7072
"test-implementation": [
71-
"@test"
73+
"phpunit --colors=always --testdox tests"
7274
]
7375
},
7476
"suggest": {

src/Coder/Base64.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace PetrKnap\Binary\Coder;
66

7+
use PetrKnap\Optional\OptionalString;
8+
79
/**
810
* @see base64_encode()
911
* @see base64_decode()
1012
*/
1113
final class Base64 extends Coder
1214
{
1315
public const URL_SAFE = false;
16+
1417
private const URL_REPLACE_TABLE = [
1518
['+', '/', '='],
1619
['-', '_', ''],
@@ -35,13 +38,11 @@ protected function doEncode(string $decoded): string
3538

3639
protected function doDecode(string $encoded): string
3740
{
38-
$decoded = base64_decode(
41+
return OptionalString::ofFalsable(base64_decode(
3942
str_replace(self::URL_REPLACE_TABLE[1], self::URL_REPLACE_TABLE[0], $encoded),
4043
strict: true,
44+
))->orElseThrow(
45+
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
4146
);
42-
if ($decoded === false) {
43-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
44-
}
45-
return $decoded;
4647
}
4748
}

src/Coder/Hex.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PetrKnap\Binary\Coder;
66

7+
use PetrKnap\Optional\OptionalString;
8+
79
/**
810
* @see bin2hex()
911
* @see hex2bin()
@@ -17,10 +19,8 @@ protected function doEncode(string $decoded): string
1719

1820
protected function doDecode(string $encoded): string
1921
{
20-
$decoded = hex2bin($encoded);
21-
if ($decoded === false) {
22-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
23-
}
24-
return $decoded;
22+
return OptionalString::ofFalsable(hex2bin($encoded))->orElseThrow(
23+
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
24+
);
2525
}
2626
}

src/Coder/Zlib.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PetrKnap\Binary\Coder;
66

7+
use PetrKnap\Optional\OptionalString;
78
use PetrKnap\Shorts\HasRequirements;
89

910
/**
@@ -50,19 +51,15 @@ public function decode(string $encoded, ?int $maxLength = null): string
5051

5152
protected function doEncode(string $decoded): string
5253
{
53-
$encoded = zlib_encode($decoded, $this->encoding, $this->level);
54-
if ($encoded === false) {
55-
throw new Exception\CouldNotEncodeData(__METHOD__, $decoded);
56-
}
57-
return $encoded;
54+
return OptionalString::ofFalsable(zlib_encode($decoded, $this->encoding, $this->level))->orElseThrow(
55+
static fn () => new Exception\CouldNotEncodeData(__METHOD__, $decoded),
56+
);
5857
}
5958

6059
protected function doDecode(string $encoded): string
6160
{
62-
$decoded = zlib_decode($encoded, $this->maxLength);
63-
if ($decoded === false) {
64-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
65-
}
66-
return $decoded;
61+
return OptionalString::ofFalsable(zlib_decode($encoded, $this->maxLength))->orElseThrow(
62+
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
63+
);
6764
}
6865
}

src/Serializer/Igbinary.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PetrKnap\Binary\Serializer;
66

7+
use PetrKnap\Optional\Optional;
8+
use PetrKnap\Optional\OptionalString;
79
use PetrKnap\Shorts\HasRequirements;
810

911
/**
@@ -26,19 +28,15 @@ functions: [
2628

2729
protected function doSerialize(mixed $serializable): string
2830
{
29-
$serialized = igbinary_serialize($serializable);
30-
if ($serialized === null) {
31-
throw new Exception\CouldNotSerializeData(__METHOD__, $serializable);
32-
}
33-
return $serialized;
31+
return OptionalString::ofFalsable(igbinary_serialize($serializable) ?? false)->orElseThrow(
32+
static fn () => new Exception\CouldNotSerializeData(__METHOD__, $serializable),
33+
);
3434
}
3535

3636
protected function doUnserialize(string $serialized): mixed
3737
{
38-
$serializable = igbinary_unserialize($serialized);
39-
if ($serializable === null || $serializable === false) {
40-
throw new Exception\CouldNotUnserializeData(__METHOD__, $serialized);
41-
}
42-
return $serializable;
38+
return Optional::ofFalsable(igbinary_unserialize($serialized) ?? false)->orElseThrow(
39+
static fn () => new Exception\CouldNotUnserializeData(__METHOD__, $serialized),
40+
);
4341
}
4442
}

src/Serializer/Php.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PetrKnap\Binary\Serializer;
66

7+
use PetrKnap\Optional\Optional;
8+
79
/**
810
* @see serialize()
911
* @see unserialize()
@@ -17,10 +19,8 @@ protected function doSerialize(mixed $serializable): string
1719

1820
protected function doUnserialize(string $serialized): mixed
1921
{
20-
$serializable = unserialize($serialized);
21-
if ($serializable === false) {
22-
throw throw new Exception\CouldNotUnserializeData(__METHOD__, $serialized);
23-
}
24-
return $serializable;
22+
return Optional::ofFalsable(unserialize($serialized))->orElseThrow(
23+
static fn () => new Exception\CouldNotUnserializeData(__METHOD__, $serialized),
24+
);
2525
}
2626
}

tests/Serializer/IgbinaryTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44

55
namespace PetrKnap\Binary\Serializer;
66

7-
use PetrKnap\Shorts\Exception\MissingRequirement;
8-
97
final class IgbinaryTest extends SerializerTestCase
108
{
119
public static function getSerialized(): string
1210
{
13-
return base64_decode(''); // @TODO
11+
return base64_decode('AAAAAhcIc3RkQ2xhc3MUBhEFYXJyYXkUABEGYmluYXJ5BgARBWZsb2F0DAAAAAAAAAAAEQNpbnQGABEEbnVsbAARBnN0cmluZw0=');
1412
}
1513

1614
public static function getSerializer(): SerializerInterface
1715
{
18-
try {
19-
return new Igbinary();
20-
} catch (MissingRequirement $reason) {
21-
self::markTestSkipped($reason->getMessage());
22-
}
16+
return new Igbinary();
2317
}
2418
}

0 commit comments

Comments
 (0)