-
-
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
9 changed files
with
193 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
use Assert\Assert; | ||
use PetrKnap\Shorts\HasRequirements; | ||
use Throwable; | ||
|
||
class Byter | ||
{ | ||
use HasRequirements; | ||
|
||
public const ENCODING = '8bit'; | ||
|
||
public function __construct() | ||
{ | ||
self::checkRequirements( | ||
functions: [ | ||
'mb_strcut', | ||
'mb_strlen', | ||
], | ||
); | ||
} | ||
|
||
/** | ||
* Bites data into bites of specified sizes | ||
* | ||
* @param int $size1 bytes | ||
* @param int $sizeN bytes | ||
* | ||
* @return array<string> | ||
* | ||
* @throws Exception\CouldNotBiteData | ||
*/ | ||
public function bite(string $data, int $size1, int ...$sizeN): array | ||
{ | ||
try { | ||
Assert::that($this->size($data))->greaterThan(1, 'Too short data'); | ||
$remains = $data; | ||
$bites = []; | ||
foreach ([$size1, ...$sizeN] as $size) { | ||
Assert::that($size)->greaterThan(0, 'Size must be positive'); | ||
$bites[] = mb_strcut($remains, 0, $size, encoding: self::ENCODING); | ||
$remains = mb_strcut($remains, $size, encoding: self::ENCODING); | ||
} | ||
return [...$bites, $remains]; | ||
} catch (Throwable $reason) { | ||
throw new Exception\CouldNotBiteData(__METHOD__, $data, $reason); | ||
} | ||
} | ||
|
||
/** | ||
* Backward version of {@see self::bite()} | ||
* | ||
* @link https://en.wikipedia.org/wiki/Backwards_(Red_Dwarf) | ||
*/ | ||
public function unbite(string $bite1, string ...$biteN): string | ||
{ | ||
return implode([$bite1, ...$biteN]); | ||
} | ||
|
||
/** | ||
* @return int bytes | ||
*/ | ||
public function size(string $data): int | ||
{ | ||
return mb_strlen($data, self::ENCODING); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary\Exception; | ||
|
||
interface ByterException 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\Exception; | ||
|
||
use PetrKnap\Shorts\Exception\CouldNotProcessData; | ||
|
||
/** | ||
* @extends CouldNotProcessData<string> | ||
*/ | ||
final class CouldNotBiteData extends CouldNotProcessData implements ByterException | ||
{ | ||
} |
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,56 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PetrKnap\Binary; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ByterTest extends TestCase | ||
{ | ||
public function testBitesData(): void | ||
{ | ||
self::assertSame( | ||
[hex2bin('01'), hex2bin('0203'), hex2bin('040506'), hex2bin('070809')], | ||
(new Byter())->bite(self::getData(), 1, 2, 3), | ||
); | ||
} | ||
|
||
#[DataProvider('dataBiteThrows')] | ||
public function testBiteThrows(string $data, int $size) | ||
{ | ||
self::expectException(Exception\CouldNotBiteData::class); | ||
|
||
(new Byter())->bite($data, $size); | ||
} | ||
|
||
public static function dataBiteThrows(): array | ||
{ | ||
return [ | ||
'negative size' => [self::getData(), -1], | ||
'zero size' => [self::getData(), 0], | ||
'empty data' => ['', 1], | ||
'short data' => [hex2bin('01'), 1], | ||
]; | ||
} | ||
|
||
public function testUnbitesBites(): void | ||
{ | ||
self::assertSame( | ||
self::getData(), | ||
(new Byter())->unbite(hex2bin('01'), hex2bin('0203'), hex2bin('040506'), hex2bin('070809')), | ||
); | ||
} | ||
|
||
public function testReturnsSizeOfData(): void | ||
{ | ||
self::assertSame( | ||
9, | ||
(new Byter())->size(self::getData()), | ||
); | ||
} | ||
|
||
private static function getData(): string | ||
{ | ||
return hex2bin('010203040506070809'); | ||
} | ||
} |
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