-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortOID.php
55 lines (45 loc) · 1.55 KB
/
ShortOID.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
<?php
require_once 'vendor/autoload.php';
define("MACPID", "297759dc04");
//objectid = 55da83a5 297759 dc04 000029
class ShortOID
{
public static function encode($objectId){
$hashids = new Hashids\Hashids('imprest', 10);
if(!$objectId) return false;
$shortId = $hashids->encode(hexdec(substr($objectId, 0, 8)), hexdec(substr($objectId, 18, 23)));
return $shortId;
}
public static function decode($shortId){
$hashids = new Hashids\Hashids('imprest', 10);
if(!$shortId) return false;
$numbers = $hashids->decode($shortId);
$objectId = vsprintf('%08x' . MACPID . '%06x', $numbers);
return $objectId;
}
public static function fixMongoId($id){
$objectId = substr($id, 0, 8) . MACPID . substr($id, 18, 23);
return new MongoId($objectId);
}
/* 将 _id 替换为 objectId */
public static function replace(&$data){
if( ! array_key_exists('_id', $data)) return false;
$shortId = ShortOID::encode($data['_id']);
unset($data['_id']);
$data[OBJECTID] = $shortId;
return true;
}
/* 将 objectId 还原为 _id */
public static function revert(&$data, $addId){
if(array_key_exists(OBJECTID, $data)){
$objectId = ShortOID::decode($data[OBJECTID]);
unset($data[OBJECTID]);
$data['_id'] = new MongoId($objectId);
}else if($addId){
$data['_id'] = ShortOID::fixMongoId(new MongoId());
}else{
return false;
}
return true;
}
}