Skip to content

Commit

Permalink
refactor: now uses petrknap/optional
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Oct 29, 2024
1 parent 2227dd1 commit 9521f53
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 45 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ RUN apt-get update \
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
# endregion

# region included igbinary
# hadolint ignore=DL3008
RUN pecl install -o -f \
igbinary \
&& docker-php-ext-enable \
igbinary \
;
# endregion

# region included composer-library
WORKDIR /app
COPY . .
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
"name": "petrknap/binary",
"require": {
"php": ">=8.1",
"petrknap/optional": "^3.1",
"petrknap/shorts": "^2.0"
},
"require-dev": {
"ext-igbinary": "*",
"ext-mbstring": "*",
"ext-zlib": "*",
"nunomaduro/phpinsights": "^2.11",
Expand All @@ -51,7 +53,7 @@
"squizlabs/php_codesniffer": "^3.7"
},
"scripts": {
"test": "phpunit --colors=always --testdox tests",
"test": "@test-implementation",
"ci-script": [
"@check-implementation",
"@check-requirements",
Expand All @@ -68,7 +70,7 @@
"composer outdated \"petrknap/*\" --major-only --strict --ansi --no-interaction"
],
"test-implementation": [
"@test"
"phpunit --colors=always --testdox tests"
]
},
"suggest": {
Expand Down
11 changes: 6 additions & 5 deletions src/Coder/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace PetrKnap\Binary\Coder;

use PetrKnap\Optional\OptionalString;

/**
* @see base64_encode()
* @see base64_decode()
*/
final class Base64 extends Coder
{
public const URL_SAFE = false;

private const URL_REPLACE_TABLE = [
['+', '/', '='],
['-', '_', ''],
Expand All @@ -35,13 +38,11 @@ protected function doEncode(string $decoded): string

protected function doDecode(string $encoded): string
{
$decoded = base64_decode(
return OptionalString::ofFalsable(base64_decode(
str_replace(self::URL_REPLACE_TABLE[1], self::URL_REPLACE_TABLE[0], $encoded),
strict: true,
))->orElseThrow(
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
);
if ($decoded === false) {
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
}
return $decoded;
}
}
10 changes: 5 additions & 5 deletions src/Coder/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PetrKnap\Binary\Coder;

use PetrKnap\Optional\OptionalString;

/**
* @see bin2hex()
* @see hex2bin()
Expand All @@ -17,10 +19,8 @@ protected function doEncode(string $decoded): string

protected function doDecode(string $encoded): string
{
$decoded = hex2bin($encoded);
if ($decoded === false) {
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
}
return $decoded;
return OptionalString::ofFalsable(hex2bin($encoded))->orElseThrow(
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
);
}
}
17 changes: 7 additions & 10 deletions src/Coder/Zlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PetrKnap\Binary\Coder;

use PetrKnap\Optional\OptionalString;
use PetrKnap\Shorts\HasRequirements;

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

protected function doEncode(string $decoded): string
{
$encoded = zlib_encode($decoded, $this->encoding, $this->level);
if ($encoded === false) {
throw new Exception\CouldNotEncodeData(__METHOD__, $decoded);
}
return $encoded;
return OptionalString::ofFalsable(zlib_encode($decoded, $this->encoding, $this->level))->orElseThrow(
static fn () => new Exception\CouldNotEncodeData(__METHOD__, $decoded),
);
}

protected function doDecode(string $encoded): string
{
$decoded = zlib_decode($encoded, $this->maxLength);
if ($decoded === false) {
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
}
return $decoded;
return OptionalString::ofFalsable(zlib_decode($encoded, $this->maxLength))->orElseThrow(
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
);
}
}
18 changes: 8 additions & 10 deletions src/Serializer/Igbinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PetrKnap\Binary\Serializer;

use PetrKnap\Optional\Optional;
use PetrKnap\Optional\OptionalString;
use PetrKnap\Shorts\HasRequirements;

/**
Expand All @@ -26,19 +28,15 @@ functions: [

protected function doSerialize(mixed $serializable): string
{
$serialized = igbinary_serialize($serializable);
if ($serialized === null) {
throw new Exception\CouldNotSerializeData(__METHOD__, $serializable);
}
return $serialized;
return OptionalString::ofFalsable(igbinary_serialize($serializable) ?? false)->orElseThrow(
static fn () => new Exception\CouldNotSerializeData(__METHOD__, $serializable),
);
}

protected function doUnserialize(string $serialized): mixed
{
$serializable = igbinary_unserialize($serialized);
if ($serializable === null || $serializable === false) {
throw new Exception\CouldNotUnserializeData(__METHOD__, $serialized);
}
return $serializable;
return Optional::ofFalsable(igbinary_unserialize($serialized) ?? false)->orElseThrow(
static fn () => new Exception\CouldNotUnserializeData(__METHOD__, $serialized),
);
}
}
10 changes: 5 additions & 5 deletions src/Serializer/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PetrKnap\Binary\Serializer;

use PetrKnap\Optional\Optional;

/**
* @see serialize()
* @see unserialize()
Expand All @@ -17,10 +19,8 @@ protected function doSerialize(mixed $serializable): string

protected function doUnserialize(string $serialized): mixed
{
$serializable = unserialize($serialized);
if ($serializable === false) {
throw throw new Exception\CouldNotUnserializeData(__METHOD__, $serialized);
}
return $serializable;
return Optional::ofFalsable(unserialize($serialized))->orElseThrow(
static fn () => new Exception\CouldNotUnserializeData(__METHOD__, $serialized),
);
}
}
10 changes: 2 additions & 8 deletions tests/Serializer/IgbinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@

namespace PetrKnap\Binary\Serializer;

use PetrKnap\Shorts\Exception\MissingRequirement;

final class IgbinaryTest extends SerializerTestCase
{
public static function getSerialized(): string
{
return base64_decode(''); // @TODO
return base64_decode('AAAAAhcIc3RkQ2xhc3MUBhEFYXJyYXkUABEGYmluYXJ5BgARBWZsb2F0DAAAAAAAAAAAEQNpbnQGABEEbnVsbAARBnN0cmluZw0=');
}

public static function getSerializer(): SerializerInterface
{
try {
return new Igbinary();
} catch (MissingRequirement $reason) {
self::markTestSkipped($reason->getMessage());
}
return new Igbinary();
}
}

0 comments on commit 9521f53

Please sign in to comment.