-
-
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.
- Loading branch information
Showing
48 changed files
with
919 additions
and
716 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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
/** | ||
* @see base64_encode() | ||
* @see base64_decode() | ||
*/ | ||
final class Base64 extends Coder | ||
{ | ||
public const URL_SAFE = false; | ||
private const URL_REPLACE_TABLE = [ | ||
['+', '/', '='], | ||
['-', '_', ''], | ||
]; | ||
|
||
private bool $urlSafe; | ||
|
||
public function encode(string $decoded, ?bool $urlSafe = null): string | ||
{ | ||
$this->urlSafe = $urlSafe ?? self::URL_SAFE; | ||
return parent::encode($decoded); | ||
} | ||
|
||
protected function doEncode(string $decoded): string | ||
{ | ||
$encoded = base64_encode($decoded); | ||
if ($this->urlSafe) { | ||
$encoded = str_replace(self::URL_REPLACE_TABLE[0], self::URL_REPLACE_TABLE[1], $encoded); | ||
} | ||
return $encoded; | ||
} | ||
|
||
protected function doDecode(string $encoded): string | ||
{ | ||
$decoded = base64_decode( | ||
str_replace(self::URL_REPLACE_TABLE[1], self::URL_REPLACE_TABLE[0], $encoded), | ||
strict: true, | ||
); | ||
if ($decoded === false) { | ||
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded); | ||
} | ||
return $decoded; | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
use PetrKnap\Binary\HasRequirementsTrait; | ||
use PetrKnap\Shorts\HasRequirements; | ||
|
||
/** | ||
* @see hash() | ||
* | ||
* @link https://en.wikipedia.org/wiki/Checksum | ||
*/ | ||
final class Checksum extends Coder implements HasRequirements | ||
{ | ||
use HasRequirementsTrait; | ||
|
||
public const ALGORITHM = 'crc32'; | ||
private const REQUIRED_FUNCTIONS = [ | ||
'mb_strlen', | ||
'mb_strcut', | ||
]; | ||
|
||
private string $algorithm; | ||
|
||
public function __construct() | ||
{ | ||
self::checkRequirements(); | ||
} | ||
|
||
public function encode(string $decoded, ?string $algorithm = null): string | ||
{ | ||
$this->algorithm = $algorithm ?? self::ALGORITHM; | ||
return parent::encode($decoded); | ||
} | ||
|
||
public function decode(string $encoded, ?string $algorithm = null): string | ||
{ | ||
$this->algorithm = $algorithm ?? self::ALGORITHM; | ||
return parent::decode($encoded); | ||
} | ||
|
||
protected function doEncode(string $decoded): string | ||
{ | ||
$checksum = hash($this->algorithm, $decoded, binary: true); | ||
return $decoded . $checksum; | ||
} | ||
|
||
protected function doDecode(string $encoded): string | ||
{ | ||
$checksumLength = mb_strlen(hash($this->algorithm, '', binary: true), encoding: '8bit'); | ||
$dataLength = mb_strlen($encoded, encoding: '8bit') - $checksumLength; | ||
$decoded = mb_strcut($encoded, 0, $dataLength, encoding: '8bit'); | ||
if ($this->doEncode($decoded) !== $encoded) { | ||
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded); | ||
} | ||
return $decoded; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
use Throwable; | ||
|
||
abstract class Coder implements CoderInterface | ||
{ | ||
public function encode(string $decoded): string | ||
{ | ||
try { | ||
return $this->doEncode($decoded); | ||
} catch (Throwable $reason) { | ||
if ($reason instanceof Exception\CouldNotEncodeData) { | ||
throw $reason; | ||
} | ||
throw new Exception\CouldNotEncodeData(__METHOD__, $decoded, $reason); | ||
} | ||
} | ||
|
||
public function decode(string $encoded): string | ||
{ | ||
try { | ||
return $this->doDecode($encoded); | ||
} catch (Throwable $reason) { | ||
if ($reason instanceof Exception\CouldNotDecodeData) { | ||
throw $reason; | ||
} | ||
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded, $reason); | ||
} | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
abstract protected function doEncode(string $decoded): string; | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
abstract protected function doDecode(string $encoded): string; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
interface CoderInterface | ||
{ | ||
/** | ||
* @throws Exception\CouldNotEncodeData | ||
*/ | ||
public function encode(string $decoded): string; | ||
|
||
/** | ||
* @throws Exception\CouldNotDecodeData | ||
*/ | ||
public function decode(string $encoded): string; | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder\Exception; | ||
|
||
use PetrKnap\Binary\Exception\BinaryException; | ||
|
||
interface CoderException extends BinaryException | ||
{ | ||
} |
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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder\Exception; | ||
|
||
use PetrKnap\Shorts\Exception\CouldNotProcessData; | ||
|
||
/** | ||
* @extends CouldNotProcessData<string> | ||
*/ | ||
final class CouldNotDecodeData extends CouldNotProcessData implements CoderException | ||
{ | ||
} |
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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder\Exception; | ||
|
||
use PetrKnap\Shorts\Exception\CouldNotProcessData; | ||
|
||
/** | ||
* @extends CouldNotProcessData<string> | ||
*/ | ||
final class CouldNotEncodeData extends CouldNotProcessData implements CoderException | ||
{ | ||
} |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Coder; | ||
|
||
use PetrKnap\Binary\HasRequirementsTrait; | ||
use PetrKnap\Shorts\HasRequirements; | ||
|
||
/** | ||
* @see zlib_encode() | ||
* @see zlib_decode() | ||
*/ | ||
final class Zlib extends Coder implements HasRequirements | ||
{ | ||
use HasRequirementsTrait; | ||
|
||
public const ENCODING = ZLIB_ENCODING_RAW; | ||
public const LEVEL = -1; | ||
public const MAX_LENGTH = 0; | ||
private const REQUIRED_FUNCTIONS = [ | ||
'zlib_encode', | ||
'zlib_decode', | ||
]; | ||
|
||
private int $encoding; | ||
private int $level; | ||
private int $maxLength; | ||
|
||
public function encode(string $decoded, ?int $encoding = null, ?int $level = null): string | ||
{ | ||
$this->encoding = $encoding ?? self::ENCODING; | ||
$this->level = $level ?? self::LEVEL; | ||
return parent::encode($decoded); | ||
} | ||
|
||
public function decode(string $encoded, ?int $maxLength = null): string | ||
{ | ||
$this->maxLength = $maxLength ?? self::MAX_LENGTH; | ||
return parent::decode($encoded); | ||
} | ||
|
||
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; | ||
} | ||
|
||
protected function doDecode(string $encoded): string | ||
{ | ||
$decoded = zlib_decode($encoded, $this->maxLength); | ||
if ($decoded === false) { | ||
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded); | ||
} | ||
return $decoded; | ||
} | ||
} |
Oops, something went wrong.