Skip to content

Commit 766a5d2

Browse files
committed
Merge remote-tracking branch 'origin/main' into updated-shorts
2 parents 70f9d51 + fd3ecd3 commit 766a5d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+482
-388
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 . .

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ See the examples below for more information, or check out [`Encoder`](./src/Enco
77
namespace PetrKnap\Binary;
88

99
$data = base64_decode('hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==');
10-
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->getData();
11-
$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->getData();
10+
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->data;
11+
$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->data;
1212

1313
printf('Data was coded into `%s` %s.', $encoded, $decoded === $data ? 'successfully' : 'unsuccessfully');
1414
```

composer.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"allow-plugins": false,
1414
"sort-packages": true
1515
},
16+
"conflict": {
17+
"petrknap/xz-utils": "<1|>=2"
18+
},
1619
"description": "Library for work with binary data and objects",
1720
"funding": [
1821
{
@@ -33,24 +36,28 @@
3336
"hexadecimal",
3437
"igbinary",
3538
"serializer",
36-
"zlib"
39+
"zlib",
40+
"xz"
3741
],
3842
"license": "LGPL-3.0-or-later",
3943
"name": "petrknap/binary",
4044
"require": {
4145
"php": ">=8.1",
42-
"petrknap/shorts": "^2.0|^3.0"
46+
"petrknap/optional": "^3.1",
47+
"petrknap/shorts": "^3.0"
4348
},
4449
"require-dev": {
50+
"ext-igbinary": "*",
4551
"ext-mbstring": "*",
4652
"ext-zlib": "*",
4753
"nunomaduro/phpinsights": "^2.11",
54+
"petrknap/xz-utils": "*",
4855
"phpstan/phpstan": "^1.12",
4956
"phpunit/phpunit": "^10.5",
5057
"squizlabs/php_codesniffer": "^3.7"
5158
},
5259
"scripts": {
53-
"test": "phpunit --colors=always --testdox tests",
60+
"test": "@test-implementation",
5461
"ci-script": [
5562
"@check-implementation",
5663
"@check-requirements",
@@ -67,12 +74,13 @@
6774
"composer outdated \"petrknap/*\" --major-only --strict --ansi --no-interaction"
6875
],
6976
"test-implementation": [
70-
"@test"
77+
"phpunit --colors=always --testdox tests"
7178
]
7279
},
7380
"suggest": {
7481
"ext-igbinary": "Required to serialize data via igbinary",
7582
"ext-mbstring": "Required to bite bytes",
76-
"ext-zlib": "Required to compress data"
83+
"ext-zlib": "Required to compress data",
84+
"petrknap/xz-utils": "Required to compress data"
7785
}
7886
}

src/Binary.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
final class Binary
88
{
9-
public static function encode(string $data): EncoderInterface
9+
public static function encode(string $data): Encoder
1010
{
1111
return new Encoder($data);
1212
}
1313

14-
public static function decode(string $data): DecoderInterface
14+
public static function decode(string $data): Decoder
1515
{
1616
return new Decoder($data);
1717
}

src/Byter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PetrKnap\Shorts\HasRequirements;
88
use RuntimeException;
99

10-
class Byter
10+
final class Byter
1111
{
1212
use HasRequirements;
1313

@@ -28,15 +28,15 @@ functions: [
2828
*
2929
* @return array<string> bites of specified sizes; and remains, if any
3030
*
31-
* @throws Exception\CouldNotBiteData
31+
* @throws Exception\ByterCouldNotBiteData
3232
*/
3333
public function bite(string $data, int $size1, int ...$sizeN): array
3434
{
3535
$remains = $data;
3636
$bites = [];
3737
foreach ([$size1, ...$sizeN] as $size) {
3838
if (abs($size) > $this->size($remains)) {
39-
throw new Exception\CouldNotBiteData(__METHOD__, $data, new RuntimeException(
39+
throw new Exception\ByterCouldNotBiteData(__METHOD__, $data, new RuntimeException(
4040
'Remains are smaller than bite',
4141
));
4242
}

src/Coder.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,61 @@
44

55
namespace PetrKnap\Binary;
66

7-
use PetrKnap\Shorts\Exception;
8-
97
/**
10-
* @internal please use subclass
11-
*
12-
* @phpstan-consistent-constructor override {@see self::create()} if not
13-
*
14-
* @implements CoderInterface<Exception\CouldNotProcessData>
8+
* @internal shared logic
159
*/
16-
abstract class Coder implements CoderInterface
10+
abstract class Coder
1711
{
18-
public function __construct(
19-
protected readonly string $data = '',
12+
final public function __construct(
13+
public readonly string $data = '',
2014
) {
2115
}
2216

23-
public function withData(string $data): static
17+
final public function withData(string $data): static
2418
{
25-
return static::create($this, $data);
19+
return new static($data);
2620
}
2721

28-
public function getData(): string
22+
/**
23+
* @deprecated use readonly property $data
24+
*/
25+
final public function getData(): string
2926
{
3027
return $this->data;
3128
}
3229

33-
protected static function create(self $coder, string $data): static
34-
{
35-
return new static($data);
36-
}
30+
/**
31+
* @see Coder\Base64
32+
*
33+
* @throws Coder\Exception\CoderException
34+
*/
35+
abstract public function base64(): static;
36+
37+
/**
38+
* @see Coder\Checksum
39+
*
40+
* @throws Coder\Exception\CoderException
41+
*/
42+
abstract public function checksum(string|null $algorithm = null): static;
43+
44+
/**
45+
* @see Coder\Hex
46+
*
47+
* @throws Coder\Exception\CoderException
48+
*/
49+
abstract public function hex(): static;
50+
51+
/**
52+
* @see Coder\Xz
53+
*
54+
* @throws Coder\Exception\CoderException
55+
*/
56+
abstract public function xz(): static;
57+
58+
/**
59+
* @see Coder\zlib
60+
*
61+
* @throws Coder\Exception\CoderException
62+
*/
63+
abstract public function zlib(): static;
3764
}

src/Coder/Base64.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
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
{
13-
public const URL_SAFE = false;
1415
private const URL_REPLACE_TABLE = [
1516
['+', '/', '='],
1617
['-', '_', ''],
1718
];
1819

1920
private bool $urlSafe;
2021

21-
public function encode(string $decoded, ?bool $urlSafe = null): string
22+
public function encode(string $decoded, bool|null $urlSafe = null): string
2223
{
23-
$this->urlSafe = $urlSafe ?? self::URL_SAFE;
24+
$this->urlSafe = $urlSafe ?? false;
2425
return parent::encode($decoded);
2526
}
2627

@@ -35,13 +36,11 @@ protected function doEncode(string $decoded): string
3536

3637
protected function doDecode(string $encoded): string
3738
{
38-
$decoded = base64_decode(
39+
return OptionalString::ofFalsable(base64_decode(
3940
str_replace(self::URL_REPLACE_TABLE[1], self::URL_REPLACE_TABLE[0], $encoded),
4041
strict: true,
42+
))->orElseThrow(
43+
static fn () => new Exception\CoderCouldNotDecodeData(__METHOD__, $encoded),
4144
);
42-
if ($decoded === false) {
43-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
44-
}
45-
return $decoded;
4645
}
4746
}

src/Coder/Checksum.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
*/
1414
final class Checksum extends Coder
1515
{
16-
public const ALGORITHM = 'crc32';
16+
/**
17+
* @internal public for testing purposes only
18+
*/
19+
public const DEFAULT_ALGORITHM = 'crc32';
1720

1821
private string $algorithm;
1922
private readonly Byter $byter;
@@ -23,15 +26,15 @@ public function __construct()
2326
$this->byter = new Byter();
2427
}
2528

26-
public function encode(string $decoded, ?string $algorithm = null): string
29+
public function encode(string $decoded, string|null $algorithm = null): string
2730
{
28-
$this->algorithm = $algorithm ?? self::ALGORITHM;
31+
$this->algorithm = $algorithm ?? self::DEFAULT_ALGORITHM;
2932
return parent::encode($decoded);
3033
}
3134

32-
public function decode(string $encoded, ?string $algorithm = null): string
35+
public function decode(string $encoded, string|null $algorithm = null): string
3336
{
34-
$this->algorithm = $algorithm ?? self::ALGORITHM;
37+
$this->algorithm = $algorithm ?? self::DEFAULT_ALGORITHM;
3538
return parent::decode($encoded);
3639
}
3740

@@ -46,7 +49,7 @@ protected function doDecode(string $encoded): string
4649
$checksumLength = $this->byter->size(hash($this->algorithm, '', binary: true));
4750
[,$decoded] = $this->byter->bite($encoded, -$checksumLength);
4851
if ($this->doEncode($decoded) !== $encoded) {
49-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
52+
throw new Exception\CoderCouldNotDecodeData(__METHOD__, $encoded);
5053
}
5154
return $decoded;
5255
}

src/Coder/Coder.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66

77
use Throwable;
88

9+
/**
10+
* @internal shared logic
11+
*/
912
abstract class Coder implements CoderInterface
1013
{
1114
public function encode(string $decoded): string
1215
{
1316
try {
1417
return $this->doEncode($decoded);
18+
} catch (Exception\CoderCouldNotEncodeData $exception) {
19+
throw $exception;
1520
} catch (Throwable $reason) {
16-
if ($reason instanceof Exception\CouldNotEncodeData) {
17-
throw $reason;
18-
}
19-
throw new Exception\CouldNotEncodeData(__METHOD__, $decoded, $reason);
21+
throw new Exception\CoderCouldNotEncodeData(__METHOD__, $decoded, $reason);
2022
}
2123
}
2224

2325
public function decode(string $encoded): string
2426
{
2527
try {
2628
return $this->doDecode($encoded);
29+
} catch (Exception\CoderCouldNotDecodeData $exception) {
30+
throw $exception;
2731
} catch (Throwable $reason) {
28-
if ($reason instanceof Exception\CouldNotDecodeData) {
29-
throw $reason;
30-
}
31-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded, $reason);
32+
throw new Exception\CoderCouldNotDecodeData(__METHOD__, $encoded, $reason);
3233
}
3334
}
3435

src/Coder/CoderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
interface CoderInterface
88
{
99
/**
10-
* @throws Exception\CouldNotEncodeData
10+
* @throws Exception\CoderCouldNotEncodeData
1111
*/
1212
public function encode(string $decoded): string;
1313

1414
/**
15-
* @throws Exception\CouldNotDecodeData
15+
* @throws Exception\CoderCouldNotDecodeData
1616
*/
1717
public function decode(string $encoded): string;
1818
}

0 commit comments

Comments
 (0)