44
55namespace TinyBlocks \Encoder \Internal ;
66
7- use TinyBlocks \Encoder \Base62 ;
8-
97final readonly class Decimal
108{
119 private function __construct (private string $ value )
1210 {
1311 }
1412
15- public static function fromBase62 (string $ number , string $ alphabet ): Decimal
13+ public static function from (string $ number , string $ alphabet, string $ baseRadix ): Decimal
1614 {
1715 $ value = '0 ' ;
1816 $ length = strlen ($ number );
1917
2018 for ($ index = 0 ; $ index < $ length ; $ index ++) {
21- $ digit = strpos ($ alphabet , $ number [$ index ]);
22- $ value = bcmul ($ value , ( string )Base62:: BASE62_RADIX );
23- $ value = bcadd ($ value , ( string ) $ digit );
19+ $ digit = ( string ) strpos ($ alphabet , $ number [$ index ]);
20+ $ value = bcmul ($ value , $ baseRadix );
21+ $ value = bcadd ($ value , $ digit );
2422 }
2523
2624 return new Decimal (value: $ value );
@@ -32,8 +30,8 @@ public function toHexadecimal(): string
3230 $ hexadecimalValue = '' ;
3331
3432 while (bccomp ($ value , '0 ' ) > 0 ) {
35- $ remainder = bcmod ($ value , Hexadecimal::HEXADECIMAL_RADIX );
36- $ hexadecimalValue = sprintf ('%s%s ' , Hexadecimal::HEXADECIMAL_ALPHABET [( int ) $ remainder ], $ hexadecimalValue );
33+ $ remainder = ( int ) bcmod ($ value , Hexadecimal::HEXADECIMAL_RADIX );
34+ $ hexadecimalValue = sprintf ('%s%s ' , Hexadecimal::HEXADECIMAL_ALPHABET [$ remainder ], $ hexadecimalValue );
3735 $ value = bcdiv ($ value , Hexadecimal::HEXADECIMAL_RADIX );
3836 }
3937
0 commit comments