77use RuntimeException ;
88use ValueError ;
99
10- use function array_key_exists ;
1110use function chr ;
1211use function rtrim ;
1312use 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
0 commit comments