Skip to content

Commit 422e15c

Browse files
committed
Update base32 package
1 parent 37252f3 commit 422e15c

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

src/Base32/Base32.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use RuntimeException;
88
use ValueError;
99

10-
use function array_key_exists;
1110
use function chr;
1211
use function rtrim;
1312
use function str_replace;
@@ -32,7 +31,7 @@ final class Base32
3231
* @param non-empty-string $alphabet
3332
* @param non-empty-string $padding
3433
*/
35-
private function __construct(string $alphabet, string $padding)
34+
public function __construct(string $alphabet, string $padding)
3635
{
3736
if (1 !== strlen($padding) || false !== strpos(self::RESERVED_CHARACTERS, $padding)) {
3837
throw new ValueError('The padding character must be a non-reserved single byte character.');
@@ -48,29 +47,20 @@ private function __construct(string $alphabet, string $padding)
4847
throw new ValueError('The alphabet can not contain a reserved character.');
4948
}
5049

51-
$uniqueChars = [];
50+
$uniqueChars = '';
5251
for ($index = 0; $index < self::ALPHABET_SIZE; $index++) {
5352
$char = $upperAlphabet[$index];
54-
if (array_key_exists($char, $uniqueChars)) {
53+
if (false !== strpos($uniqueChars, $char)) {
5554
throw new ValueError('The alphabet must only contain unique characters.');
5655
}
5756

58-
$uniqueChars[$char] = 1;
57+
$uniqueChars .= $char;
5958
}
6059

6160
$this->alphabet = $alphabet;
6261
$this->padding = $padding;
6362
}
6463

65-
/**
66-
* @param non-empty-string $alphabet
67-
* @param non-empty-string $padding
68-
*/
69-
public static function new(string $alphabet, string $padding): self
70-
{
71-
return new self($alphabet, $padding);
72-
}
73-
7464
public function encode(string $decoded): string
7565
{
7666
if ('' === $decoded) {
@@ -119,7 +109,7 @@ public function decode(string $encoded, bool $strict = false): string
119109
do {
120110
if (!isset($val)) {
121111
$index = $encoded[$offset];
122-
$val = array_key_exists($index, $chars) ? $chars[$index] : -1;
112+
$val = $chars[$index] ?? -1;
123113
}
124114

125115
if (-1 === $val) {
@@ -142,7 +132,7 @@ public function decode(string $encoded, bool $strict = false): string
142132
$offset = $length;
143133
}
144134

145-
if ($strict && !array_key_exists($pentet, $chars)) {
135+
if ($strict && !isset($chars[$pentet])) {
146136
throw new RuntimeException('The encoded data contains characters unknown to the alphabet.');
147137
}
148138

src/Base32/Base32Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function it_wiil_distinguish_alphabet_on_character_casing_on_strict_mode(
7474
$expected = 'bangui';
7575

7676
self::assertSame($expected, base32_decode(encoded: base32_encode($expected, $alphabet)));
77-
self::assertFalse(base32_decode(encoded: base32_encode($expected, $alphabet), strict: true));
77+
self::assertNull(base32_decode(encoded: base32_encode($expected, $alphabet), strict: true));
7878
}
7979

8080
#[DataProvider('invalidDecodingSequence')]
@@ -86,7 +86,7 @@ public function it_will_return_false_from_invalid_encoded_string_with_base32_dec
8686
string $padding
8787
): void {
8888
try {
89-
self::assertFalse(base32_decode($sequence, $alphabet, $padding, true)); /* @phpstan-ignore-line */
89+
self::assertNull(base32_decode($sequence, $alphabet, $padding, true)); /* @phpstan-ignore-line */
9090
} catch (ValueError $exception) {
9191
self::assertSame($message, $exception->getMessage());
9292
}

src/Base32/functions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function base32_encode(
1717
string $alphabet = PHP_BASE32_ASCII,
1818
string $padding = '=',
1919
): string {
20-
return Base32::new($alphabet, $padding)->encode($decoded);
20+
return (new Base32($alphabet, $padding))->encode($decoded);
2121
}
2222
}
2323

@@ -31,11 +31,11 @@ function base32_decode(
3131
string $alphabet = PHP_BASE32_ASCII,
3232
string $padding = '=',
3333
bool $strict = false
34-
): string|false {
34+
): ?string {
3535
try {
36-
return Base32::new($alphabet, $padding)->decode($encoded, $strict);
36+
return (new Base32($alphabet, $padding))->decode($encoded, $strict);
3737
} catch (RuntimeException) {
38-
return false;
38+
return null;
3939
}
4040
}
4141
}

0 commit comments

Comments
 (0)