1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Clients\Mongo; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @package Applications |
6
|
|
|
* @subpackage MongoClientAsync |
7
|
|
|
* @author Vasily Zorin <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class MongoId extends \MongoId |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string $id |
|
|
|
|
14
|
|
|
*/ |
15
|
|
|
public function __construct($id = null) |
16
|
|
|
{ |
17
|
|
|
if ($id !== null && mb_orig_strlen($id) < 20 && ctype_alnum($id)) { |
18
|
|
|
$id = gmp_strval(gmp_init(strrev($id), 62), 16); |
19
|
|
|
if (mb_orig_strlen($id) > 24) { |
20
|
|
|
$id = 'FFFFFFFFFFFFFFFFFFFFFFFF'; |
21
|
|
|
} elseif (mb_orig_strlen($id) < 24) { |
22
|
|
|
$id = str_pad($id, 24, '0', STR_PAD_LEFT); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
@parent::__construct($id); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Import |
30
|
|
|
* @param mixed $id ID |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public static function import($id) |
34
|
|
|
{ |
35
|
|
|
if ($id instanceof static) { |
36
|
|
|
return $id; |
37
|
|
|
} elseif ($id instanceof \MongoId) { |
38
|
|
|
$id = (string)$id; |
39
|
|
|
} elseif (!is_string($id)) { |
40
|
|
|
if (is_array($id) && isset($id['$id'])) { |
41
|
|
|
return static::import($id['$id']); |
42
|
|
|
} |
43
|
|
|
return false; |
44
|
|
|
} elseif (mb_orig_strlen($id) === 24) { |
45
|
|
|
if (!ctype_xdigit($id)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
} elseif (ctype_alnum($id)) { |
49
|
|
|
$id = gmp_strval(gmp_init(strrev($id), 62), 16); |
50
|
|
|
if (mb_orig_strlen($id) > 24) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
if (mb_orig_strlen($id) < 24) { |
54
|
|
|
$id = str_pad($id, 24, '0', STR_PAD_LEFT); |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
return new static($id); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* __toString |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function __toString() |
67
|
|
|
{ |
68
|
|
|
return strrev(gmp_strval(gmp_init(parent::__toString(), 16), 62)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* toHex |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function toHex() |
76
|
|
|
{ |
77
|
|
|
return parent::__toString(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* getPlainObject |
82
|
|
|
* @return \MongoId |
83
|
|
|
*/ |
84
|
|
|
public function getPlainObject() |
85
|
|
|
{ |
86
|
|
|
return new \MongoId(parent::__toString()); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.