/
GmpEncoder.php
80 lines (66 loc) · 1.91 KB
/
GmpEncoder.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
<?php
/*
* This file is part of the Base62 package
*
* Copyright (c) 2016-2018 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/tuupola/base62
*
*/
namespace Tuupola\Base62;
use InvalidArgumentException;
use Tuupola\Base62;
class GmpEncoder
{
private $options = [
"characters" => Base62::GMP,
];
public function __construct($options = [])
{
$this->options = array_merge($this->options, (array) $options);
}
public function encode($data, $integer = false)
{
if (is_integer($data) || true === $integer) {
$base62 = gmp_strval(gmp_init($data, 10), 62);
} else {
$hex = bin2hex($data);
$base62 = gmp_strval(gmp_init($hex, 16), 62);
}
if (Base62::GMP === $this->options["characters"]) {
return $base62;
}
return strtr($base62, Base62::GMP, $this->options["characters"]);
}
public function decode($data, $integer = false)
{
// If the data contains characters that aren't in the character set
if (strlen($data) !== strspn($data, $this->options["characters"])) {
throw new InvalidArgumentException("Data contains invalid characters");
}
if (Base62::GMP !== $this->options["characters"]) {
$data = strtr($data, $this->options["characters"], Base62::GMP);
}
$hex = gmp_strval(gmp_init($data, 62), 16);
if (strlen($hex) % 2) {
$hex = "0" . $hex;
}
/* Return as integer when requested. */
if ($integer) {
return hexdec($hex);
}
return hex2bin($hex);
}
public function encodeInteger($data)
{
return $this->encode($data, true);
}
public function decodeInteger($data)
{
return $this->decode($data, true);
}
}