1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Alcaeus\MongoDbAdapter\TypeInterface; |
17
|
|
|
use MongoDB\BSON\ObjectID; |
18
|
|
|
|
19
|
|
|
class MongoId implements Serializable, TypeInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/* |
22
|
|
|
* @var ObjectID |
23
|
|
|
*/ |
24
|
|
|
private $objectID; |
25
|
|
|
|
26
|
|
|
private $attributes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates a new id |
30
|
|
|
* |
31
|
|
|
* |
32
|
|
|
* @link http://www.php.net/manual/en/mongoid.construct.php |
33
|
|
|
* @param string $id [optional] A string to use as the id. Must be 24 hexidecimal characters. If an invalid string is passed to this constructor, the constructor will ignore it and create a new id value. |
34
|
|
|
* @return MongoId |
|
|
|
|
35
|
|
|
* |
36
|
|
|
* @throws MongoException |
37
|
|
|
*/ |
38
|
|
|
public function __construct($id = null) |
39
|
|
|
{ |
40
|
|
|
$this->createObjectID($id); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if a value is a valid ObjectId |
45
|
|
|
* |
46
|
|
|
* @link http://php.net/manual/en/mongoid.isvalid.php |
47
|
|
|
* @param mixed $value The value to check for validity. |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public static function isValid($value) |
51
|
|
|
{ |
52
|
|
|
if ($value instanceof ObjectID || $value instanceof MongoId) { |
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return (bool) preg_match('#^[a-f0-9]{24}$#i', $value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a hexidecimal representation of this id |
61
|
|
|
* @link http://www.php.net/manual/en/mongoid.tostring.php |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function __toString() |
65
|
|
|
{ |
66
|
|
|
return (string) $this->objectID; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Converts this MongoId to the new BSON ObjectID type |
71
|
|
|
* |
72
|
|
|
* @return ObjectID |
73
|
|
|
* @internal This method is not part of the ext-mongo API |
74
|
|
|
*/ |
75
|
|
|
public function toBSONType() |
76
|
|
|
{ |
77
|
|
|
return $this->objectID; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $name |
82
|
|
|
* |
83
|
|
|
* @return null|string |
84
|
|
|
*/ |
85
|
|
|
public function __get($name) |
86
|
|
|
{ |
87
|
|
|
if ($name === '$id') { |
88
|
|
|
return (string) $this->objectID; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->attributes[$name]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
* @param mixed $value |
97
|
|
|
*/ |
98
|
|
|
public function __set($name, $value) |
99
|
|
|
{ |
100
|
|
|
if ($name === 'id') { |
101
|
|
|
trigger_error("The '\$id' property is read-only", E_DEPRECATED); |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->attributes[$name] = $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function __isset($name) |
113
|
|
|
{ |
114
|
|
|
return $name === 'id' || array_key_exists($name, $this->attributes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
*/ |
120
|
|
|
public function __unset($name) |
121
|
|
|
{ |
122
|
|
|
if ($name === 'id') { |
123
|
|
|
trigger_error("The '\$id' property is read-only", E_DEPRECATED); |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
unset($this->attributes[$name]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function serialize() |
134
|
|
|
{ |
135
|
|
|
return (string) $this->objectID; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $serialized |
140
|
|
|
*/ |
141
|
|
|
public function unserialize($serialized) |
142
|
|
|
{ |
143
|
|
|
$this->createObjectID($serialized); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Gets the incremented value to create this id |
148
|
|
|
* @link http://php.net/manual/en/mongoid.getinc.php |
149
|
|
|
* @return int Returns the incremented value used to create this MongoId. |
150
|
|
|
*/ |
151
|
|
|
public function getInc() |
152
|
|
|
{ |
153
|
|
|
return hexdec(substr((string) $this->objectID, -6)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* (PECL mongo >= 1.0.11) |
158
|
|
|
* Gets the process ID |
159
|
|
|
* @link http://php.net/manual/en/mongoid.getpid.php |
160
|
|
|
* @return int Returns the PID of the MongoId. |
161
|
|
|
*/ |
162
|
|
|
public function getPID() |
163
|
|
|
{ |
164
|
|
|
$id = (string) $this->objectID; |
165
|
|
|
|
166
|
|
|
// PID is stored as little-endian, flip it around |
167
|
|
|
$pid = substr($id, 16, 2) . substr($id, 14, 2); |
168
|
|
|
return hexdec($pid); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* (PECL mongo >= 1.0.1) |
173
|
|
|
* Gets the number of seconds since the epoch that this id was created |
174
|
|
|
* @link http://www.php.net/manual/en/mongoid.gettimestamp.php |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
public function getTimestamp() |
178
|
|
|
{ |
179
|
|
|
return hexdec(substr((string) $this->objectID, 0, 8)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Gets the hostname being used for this machine's ids |
184
|
|
|
* @link http://www.php.net/manual/en/mongoid.gethostname.php |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public static function getHostname() |
188
|
|
|
{ |
189
|
|
|
return gethostname(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* (PECL mongo >= 1.0.8) |
194
|
|
|
* Create a dummy MongoId |
195
|
|
|
* @link http://php.net/manual/en/mongoid.set-state.php |
196
|
|
|
* @param array $props <p>Theoretically, an array of properties used to create the new id. However, as MongoId instances have no properties, this is not used.</p> |
197
|
|
|
* @return MongoId A new id with the value "000000000000000000000000". |
198
|
|
|
*/ |
199
|
|
|
public static function __set_state(array $props) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $id |
206
|
|
|
* @throws MongoException |
207
|
|
|
*/ |
208
|
|
|
private function createObjectID($id) |
209
|
|
|
{ |
210
|
|
|
try { |
211
|
|
|
if (is_string($id)) { |
212
|
|
|
$this->objectID = new ObjectID($id); |
213
|
|
|
} elseif ($id instanceof self || $id instanceof ObjectID) { |
|
|
|
|
214
|
|
|
$this->objectID = new ObjectID((string) $id); |
215
|
|
|
} else { |
216
|
|
|
$this->objectID = new ObjectId(); |
217
|
|
|
} |
218
|
|
|
} catch (\Exception $e) { |
219
|
|
|
throw new MongoException('Invalid object ID', 19); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.