-
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.
refactor: Adds improvements to decoding functionality. (#6)
- Loading branch information
1 parent
15dd108
commit 8dbd26f
Showing
13 changed files
with
300 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/tests export-ignore | ||
/vendor export-ignore | ||
|
||
/LICENSE export-ignore | ||
/Makefile export-ignore | ||
/README.md export-ignore | ||
/phpmd.xml export-ignore | ||
/phpunit.xml export-ignore | ||
/phpstan.neon.dist export-ignore | ||
/infection.json.dist export-ignore | ||
|
||
/.github export-ignore | ||
/.gitignore export-ignore | ||
/.gitattributes export-ignore |
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 |
---|---|---|
@@ -1,26 +1,24 @@ | ||
{ | ||
"timeout": 10, | ||
"testFramework": "phpunit", | ||
"tmpDir": "report/", | ||
"tmpDir": "report/infection/", | ||
"source": { | ||
"directories": [ | ||
"src" | ||
] | ||
}, | ||
"logs": { | ||
"text": "report/logs/infection-text.log", | ||
"summary": "report/logs/infection-summary.log" | ||
"text": "report/infection/logs/infection-text.log", | ||
"summary": "report/infection/logs/infection-summary.log" | ||
}, | ||
"mutators": { | ||
"@default": true, | ||
"Concat": false, | ||
"FalseValue": false, | ||
"IncrementInteger": false, | ||
"DecrementInteger": false, | ||
"ConcatOperandRemoval": false | ||
"UnwrapSubstr": false, | ||
"LogicalAndNegation": false, | ||
"LogicalAndAllSubExprNegation": false | ||
}, | ||
"phpUnit": { | ||
"configDir": "", | ||
"customPath": "./vendor/bin/phpunit" | ||
} | ||
} | ||
} |
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,7 @@ | ||
parameters: | ||
paths: | ||
- src | ||
level: 9 | ||
tmpDir: report/phpstan | ||
ignoreErrors: | ||
reportUnmatchedIgnoredErrors: false |
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 |
---|---|---|
@@ -1,25 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
bootstrap="vendor/autoload.php" | ||
cacheResultFile="report/.phpunit.result.cache" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
bootstrap="vendor/autoload.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
cacheDirectory=".phpunit.cache" | ||
beStrictAboutOutputDuringTests="true"> | ||
|
||
<source> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
|
||
<testsuites> | ||
<testsuite name="default"> | ||
<directory suffix="Test.php">tests</directory> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
<report> | ||
<text outputFile="report/coverage.txt"/> | ||
<html outputDirectory="report/html/"/> | ||
<clover outputFile="report/coverage-clover.xml"/> | ||
</report> | ||
</coverage> | ||
|
||
<logging> | ||
<junit outputFile="report/execution-result.xml"/> | ||
</logging> | ||
|
||
</phpunit> |
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 |
---|---|---|
@@ -1,60 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Encoder; | ||
|
||
use TinyBlocks\Encoder\Internal\Exceptions\InvalidBase62Encoding; | ||
use TinyBlocks\Encoder\Internal\Exceptions\InvalidDecoding; | ||
use TinyBlocks\Encoder\Internal\Hexadecimal; | ||
|
||
final class Base62 | ||
final readonly class Base62 implements Encoder | ||
{ | ||
private const BASE62_RADIX = 62; | ||
private const BASE62_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | ||
private const BASE62_CHARACTER_LENGTH = 1; | ||
private const BASE62_HEXADECIMAL_RADIX = 16; | ||
|
||
public static function encode(string $value): string | ||
private function __construct(private string $value) | ||
{ | ||
$bytes = 0; | ||
$hexadecimal = bin2hex($value); | ||
} | ||
|
||
while (str_starts_with($hexadecimal, '00')) { | ||
$bytes++; | ||
$hexadecimal = substr($hexadecimal, 2); | ||
} | ||
public static function from(string $value): Encoder | ||
{ | ||
return new Base62(value: $value); | ||
} | ||
|
||
public function encode(): string | ||
{ | ||
$hexadecimal = Hexadecimal::fromBinary(binary: $this->value); | ||
$bytes = $hexadecimal->removeLeadingZeroBytes(); | ||
|
||
$base62 = str_repeat(self::BASE62_ALPHABET[0], $bytes); | ||
|
||
if (empty($hexadecimal)) { | ||
if ($hexadecimal->isEmpty()) { | ||
return $base62; | ||
} | ||
|
||
$number = gmp_init($hexadecimal, self::BASE62_HEXADECIMAL_RADIX); | ||
$number = $hexadecimal->toGmpInit(base: self::BASE62_HEXADECIMAL_RADIX); | ||
|
||
return $base62 . gmp_strval($number, self::BASE62_RADIX); | ||
return sprintf('%s%s', $base62, gmp_strval($number, self::BASE62_RADIX)); | ||
} | ||
|
||
public static function decode(string $value): string | ||
public function decode(): string | ||
{ | ||
if (strlen($value) !== strspn($value, self::BASE62_ALPHABET)) { | ||
throw new InvalidBase62Encoding(value: $value); | ||
if (strlen($this->value) !== strspn($this->value, self::BASE62_ALPHABET)) { | ||
throw new InvalidDecoding(value: $this->value); | ||
} | ||
|
||
$bytes = 0; | ||
$value = $this->value; | ||
|
||
while (!empty($value) && str_starts_with($value, self::BASE62_ALPHABET[0])) { | ||
$bytes++; | ||
$value = substr($value, 1); | ||
$value = substr($value, self::BASE62_CHARACTER_LENGTH); | ||
} | ||
|
||
if (empty($value)) { | ||
return str_repeat("\x00", $bytes); | ||
} | ||
|
||
$number = gmp_init($value, self::BASE62_RADIX); | ||
$hexadecimal = gmp_strval($number, self::BASE62_HEXADECIMAL_RADIX); | ||
$hexadecimal = Hexadecimal::fromGmp(number: $number, base: self::BASE62_HEXADECIMAL_RADIX); | ||
$hexadecimal->padLeft(); | ||
|
||
$binary = hex2bin(sprintf('%s%s', str_repeat('00', $bytes), $hexadecimal->toString())); | ||
|
||
if (strlen($hexadecimal) % 2) { | ||
$hexadecimal = '0' . $hexadecimal; | ||
if (!is_string($binary)) { | ||
throw new InvalidDecoding(value: $this->value); | ||
} | ||
|
||
return hex2bin(str_repeat('00', $bytes) . $hexadecimal); | ||
return $binary; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TinyBlocks\Encoder; | ||
|
||
use TinyBlocks\Encoder\Internal\Exceptions\InvalidDecoding; | ||
|
||
/** | ||
* Define a contract for encoding and decoding data. | ||
*/ | ||
interface Encoder | ||
{ | ||
/** | ||
* Encodes the current value into a specific format. | ||
* | ||
* @return string The encoded value. | ||
*/ | ||
public function encode(): string; | ||
|
||
/** | ||
* Decodes the current encoded value back to its original form. | ||
* | ||
* @return string The decoded value. | ||
* @throws InvalidDecoding if decoding fails. | ||
*/ | ||
public function decode(): string; | ||
} |
Oops, something went wrong.