forked from 2lovecode/code-segment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoIncreaseShortener.php
138 lines (112 loc) · 2.97 KB
/
AutoIncreaseShortener.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
<?php
/**
* code-segment
*
* @author liu hao<[email protected]>
* @copyright liu hao<[email protected]>
*/
class DbStore
{
private $autoIncreaseID = 0;
private $recordMap = [];
public function insert($data = [])
{
$this->autoIncreaseID++;
$id = $this->autoIncreaseID;
$data['id'] = $id;
$this->recordMap[$id] = $data;
return $id;
}
public function update($id, $data = [])
{
$ori = $this->findByID($id);
foreach ($data as $key => $value) {
$ori[$key] = $value;
}
$this->recordMap[$id] = $ori;
}
public function findByID($id)
{
return isset($this->recordMap[$id]) ? $this->recordMap[$id] : [];
}
}
class AutoIncreaseShortener
{
private $alphanumericMap = [];
private $dbStore;
public function __construct()
{
for ($i = 0; $i < 10; $i++) {
$this->alphanumericMap[$i] = $i;
}
$start = ord('a') - 10;
for ($i = 10; $i < 36; $i++) {
$this->alphanumericMap[$i] = chr($start + $i);
}
$start = ord('A') - 36;
for ($i = 36; $i < 62; $i++) {
$this->alphanumericMap[$i] = chr($start + $i);
}
shuffle($this->alphanumericMap);
$this->dbStore = new DbStore();
}
public function short($url)
{
$dbStore = $this->dbStore;
$id = $dbStore->insert(['url' => $url]);
$short = $this->decToN62($id);
$dbStore->update($id, ['short' => $short]);
return $short;
}
public function find($short)
{
$id = $this->n62ToDec($short);
return $this->dbStore->findByID($id);
}
public function decToN62($number)
{
$tmp = [];
$str = '';
do {
$m = $number % 62;
$tmp[] = $m;
$number = floor($number / 62);
} while ($number != 0);
$len = count($tmp);
for ($i = $len - 1; $i >= 0; $i--) {
$str .= $this->alphanumericMap[$tmp[$i]];
}
$str = str_pad($str, 6, $this->alphanumericMap[0], STR_PAD_LEFT);
return $str;
}
public function n62ToDec($string)
{
$len = strlen($string);
$map = array_flip($this->alphanumericMap);
$number = 0;
$exp = 0;
for ($i = $len - 1; $i >= 0; $i--) {
$number += $map[$string[$i]] * (62 ** $exp);
$exp++;
}
return $number;
}
}
$testData = [
'http://www.baidu.com/path/to/file',
'http://www.google.com/path/to/file',
'http://sogou.com/path/to/file',
'http://csdn.net/path/to/file',
'http://oschina.com/path/to/file',
];
echo '<pre>';
$autoIncrease = new AutoIncreaseShortener();
foreach ($testData as $each) {
$short = $autoIncrease->short($each);
$shortUrlMap[] = $short;
}
foreach ($shortUrlMap as $each) {
$data = $autoIncrease->find($each);
// var_dump($data);
echo $data['short'].' : '.$data['url'].'<br>';
}