1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/base-convert |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocubom\Math\Base; |
13
|
|
|
|
14
|
|
|
use Ocubom\Math\AbstractBase; |
15
|
|
|
use Ocubom\Math\Exception\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Numeric bases from 2 to 62. |
19
|
|
|
*/ |
20
|
|
|
class Numeric extends AbstractBase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Base62. |
24
|
|
|
* |
25
|
|
|
* @see https://en.wikipedia.org/wiki/Base62 |
26
|
|
|
*/ |
27
|
|
|
const SYMBOLS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Bases with special names. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
const NAMED_BASES = [ |
35
|
|
|
'bin' => 2, |
36
|
|
|
'dec' => 10, |
37
|
|
|
'hex' => 16, |
38
|
|
|
'oct' => 8, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
const OPTIONS = ['options' => [ |
42
|
|
|
'min_range' => 2, |
43
|
|
|
'max_range' => 62, |
44
|
|
|
]]; |
45
|
|
|
|
46
|
|
|
/** @var int */ |
47
|
|
|
private $value; |
48
|
|
|
|
49
|
|
|
/** @var array */ |
50
|
|
|
private static $maps = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int|string $base |
54
|
|
|
*/ |
55
|
6644 |
|
public function __construct($base) |
56
|
|
|
{ |
57
|
6644 |
|
$value = filter_var( |
58
|
6644 |
|
self::NAMED_BASES[strtolower((string) $base)] ?? $base, |
59
|
6644 |
|
\FILTER_VALIDATE_INT, |
60
|
6644 |
|
self::OPTIONS |
61
|
6644 |
|
); |
62
|
|
|
|
63
|
6644 |
|
if (false === $value) { |
64
|
6 |
|
throw new InvalidArgumentException("Invalid base ({$base}), must be in the range [2-62]"); |
65
|
|
|
} |
66
|
|
|
|
67
|
6641 |
|
$this->value = $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
6495 |
|
public function getMap(): array |
71
|
|
|
{ |
72
|
6495 |
|
if (!array_key_exists($this->value, self::$maps)) { |
73
|
|
|
// Extract valid symbols |
74
|
36 |
|
$symbols = substr(self::SYMBOLS, 0, $this->value); |
75
|
|
|
|
76
|
|
|
// Create reverse mapping |
77
|
36 |
|
$mapping = array_flip(str_split($symbols)); |
78
|
|
|
|
79
|
|
|
// For bases up to 36, case is ignored |
80
|
36 |
|
if ($this->value <= 36) { |
81
|
|
|
// User lowercase letters for compatibility with native base_convert |
82
|
35 |
|
$symbols = strtolower($symbols); |
83
|
35 |
|
$mapping = array_merge($mapping, array_slice(array_flip(str_split($symbols)), 10)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Add special symbols entry |
87
|
36 |
|
$mapping[''] = $symbols; |
88
|
|
|
|
89
|
36 |
|
self::$maps[$this->value] = $mapping; |
90
|
|
|
} |
91
|
|
|
|
92
|
6495 |
|
return self::$maps[$this->value]; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function __toString(): string |
96
|
|
|
{ |
97
|
1 |
|
return 'Base'.$this->value; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|