Skip to content

Commit 8e39459

Browse files
committed
Add BinaryStr
1 parent f7ff5d4 commit 8e39459

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Nexus/Encryption/BinaryStr.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus framework.
7+
*
8+
* (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Encryption;
15+
16+
/**
17+
* Binary string operations wrapping on related `mb_*` functions.
18+
*/
19+
final class BinaryStr
20+
{
21+
/**
22+
* Get the length of a binary string.
23+
*/
24+
public static function strlen(#[\SensitiveParameter] string $string): int
25+
{
26+
return mb_strlen($string, '8bit');
27+
}
28+
29+
/**
30+
* Get a substring of a binary string.
31+
*/
32+
public static function substr(#[\SensitiveParameter] string $string, int $start, ?int $length = null): string
33+
{
34+
return mb_substr($string, $start, $length, '8bit');
35+
}
36+
}

tests/Encryption/BinaryStrTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus framework.
7+
*
8+
* (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Tests\Encryption;
15+
16+
use Nexus\Encryption\BinaryStr;
17+
use PHPUnit\Framework\Attributes\CoversClass;
18+
use PHPUnit\Framework\Attributes\Group;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @internal
23+
*/
24+
#[CoversClass(BinaryStr::class)]
25+
#[Group('unit-test')]
26+
final class BinaryStrTest extends TestCase
27+
{
28+
public function testStrlen(): void
29+
{
30+
$binaryStr = str_repeat("\x00", 10);
31+
self::assertSame(10, BinaryStr::strlen($binaryStr));
32+
}
33+
34+
public function testSubstr(): void
35+
{
36+
$binaryStr = str_repeat("\x00", 10);
37+
self::assertSame("\x00\x00", BinaryStr::substr($binaryStr, 0, 2));
38+
}
39+
}

0 commit comments

Comments
 (0)