This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConversor.php
255 lines (218 loc) · 6.98 KB
/
Conversor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* based in Base62 by Lalit Patel
*
* Base62: A class to convert a number from any base between 2-62 to any other base between 2-62
* It doesn't use BC Math functions so works without the use of BC Math library.
* It uses the native base_convert functions when the base is below 36 for faster execution.
* The output number is backward compatible with the native base_convert function.
*
* Author : Lalit Patel
* Website: http://www.lalit.org/lab/base62-php-convert-number-to-base-62-for-short-urls
* License: Apache License 2.0
* http://www.apache.org/licenses/LICENSE-2.0
* Version: 0.1 (08 December 2011)
*
* Usage:
* $converted_num = Base62::convert($number, $from_base, $to_base);
*
* modificada por eturino
*/
class EtuDev_ShortURL_Conversor
{
const CHARLIST_NORMAL = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* needs to be a prime number (better if large)
* @var int
*/
static protected $MULTIPLIER = 13; //primo
/**
* a positive number to be added (extra protection)
* @var int
*/
static protected $SUMATOR = 1; //sumador extra
/**
* @var string
*/
static protected $CHARLIST;
static protected $init = false;
static public function init($multiplier = null, $sumator = null, $charlist = null)
{
if (!$multiplier && !$sumator && !$charlist && static::$MULTIPLIER && static::$SUMATOR && static::$CHARLIST) {
return true;
}
if (!$multiplier) {
if (static::$MULTIPLIER) {
$multiplier = static::$MULTIPLIER;
} else {
if (defined('SHORT_URL_MULTIPLIER')) {
$multiplier = SHORT_URL_MULTIPLIER;
}
}
}
if (!$sumator) {
if (static::$SUMATOR) {
$sumator = static::$SUMATOR;
} else {
if (defined('SHORT_URL_SUMATOR')) {
$sumator = SHORT_URL_SUMATOR;
}
}
}
if (!$charlist) {
if (static::$CHARLIST) {
$charlist = static::$CHARLIST;
} else {
if (defined('SHORT_URL_CHARLIST')) {
$charlist = SHORT_URL_CHARLIST;
} else {
$charlist = static::CHARLIST_NORMAL;
}
}
}
if ($multiplier) {
static::setMultiplier($multiplier);
}
if ($sumator) {
static::setSumator($sumator);
}
if ($charlist) {
static::setCharlist($charlist);
}
static::$init = true;
return true;
}
static public function fromDecimal($decimal)
{
if (!static::$init) {
static::init();
}
return static::convert(($decimal * static::$MULTIPLIER) + static::$SUMATOR, 10, 62, static::$CHARLIST);
}
static public function toDecimal($base62)
{
if (!static::$init) {
static::init();
}
$x = (static::convert($base62, 62, 10, static::$CHARLIST) - static::$SUMATOR) / static::$MULTIPLIER;
if (intval($x) == $x) {
return $x;
}
return null;
}
/**
* Converts a number/string from any base between 2-62 to any other base from 2-62
*
* @param mixed $number
* @param int $from_base
* @param int $to_base
* @param string|null $charlist
*
* @return string $conveted_number.
* @throws Exception
*/
protected static function convert($number, $from_base = 10, $to_base = 62, $charlist = null)
{
if (!$charlist) {
$charlist = static::$CHARLIST;
}
if ($to_base > 62 || $to_base < 2) {
throw new Exception("Invalid base (" . $to_base . "). Max base can be 62. Min base can be 2.");
}
//OPTIMIZATION: no need to convert 0
if ("{$number}" === '0') {
return 0;
}
//OPTIMIZATION: if to and from base are same.
if ($from_base == $to_base) {
return $number;
}
//OPTIMIZATION: if base is lower than 36, use PHP internal function (only if we use the NORMAL charset)
if ($from_base <= 36 && $to_base <= 36 && $charlist == static::CHARLIST_NORMAL) {
// for lower base, use the default PHP function for faster results
return base_convert($number, $from_base, $to_base);
}
// char list starts from 0-9 and then small alphabets and then capital alphabets
// to make it compatible with eixisting base_convert function
if ($from_base < $to_base) {
// if converstion is from lower base to higher base
// first get the number into decimal and then convert it to higher base from decimal;
if ($from_base != 10) {
$decimal = self::convert($number, $from_base, 10);
} else {
$decimal = intval($number);
}
//get the list of valid characters
$charlist = substr($charlist, 0, $to_base);
if ($number == 0) {
return 0;
}
$converted = '';
while ($number > 0) {
$converted = $charlist{($number % $to_base)} . $converted;
$number = floor($number / $to_base);
}
return $converted;
} else {
// if conversion is from higher base to lower base;
// first convert it into decimal and the convert it to lower base with help of same function.
$number = "{$number}";
$length = strlen($number);
$decimal = 0;
$i = 0;
while ($length > 0) {
$char = $number{$length - 1};
$pos = strpos($charlist, $char);
if ($pos === false) {
trigger_error("Invalid character in the input number: " . ($char), E_USER_ERROR);
}
$decimal += $pos * pow($from_base, $i);
$length--;
$i++;
}
return self::convert($decimal, 10, $to_base);
}
}
/**
* @param string $CHARLIST
*/
public static function setCharlist($CHARLIST)
{
self::$CHARLIST = $CHARLIST;
}
/**
* @return string
*/
public static function getCharlist()
{
return self::$CHARLIST;
}
/**
* @param int $MULTIPLIER
*/
public static function setMultiplier($MULTIPLIER)
{
self::$MULTIPLIER = $MULTIPLIER;
}
/**
* @return int
*/
public static function getMultiplier()
{
return self::$MULTIPLIER;
}
/**
* @param int $SUMATOR
*/
public static function setSumator($SUMATOR)
{
self::$SUMATOR = $SUMATOR;
}
/**
* @return int
*/
public static function getSumator()
{
return self::$SUMATOR;
}
}