createObjectID($id); } /** * Check if a value is a valid ObjectId * * @link http://php.net/manual/en/mongoid.isvalid.php * @param mixed $value The value to check for validity. * @return bool */ public static function isValid($value) { if ($value instanceof ObjectID || $value instanceof MongoId) { return true; } return (bool) preg_match('#^[a-f0-9]{24}$#i', $value); } /** * Returns a hexidecimal representation of this id * @link http://www.php.net/manual/en/mongoid.tostring.php * @return string */ public function __toString() { return (string) $this->objectID; } /** * Returns the ObjectID used internally * @return ObjectID * @internal This method is not part of the ext-mongo API. Do not use this! */ public function getObjectID() { return $this->objectID; } /** * @param string $name * * @return null|string */ public function __get($name) { if ($name === '$id') { return (string) $this->objectID; } return $this->attributes[$name]; } /** * @param string $name * @param mixed $value */ public function __set($name, $value) { if ($name === 'id') { trigger_error("The '\$id' property is read-only", E_DEPRECATED); return; } $this->attributes[$name] = $value; } /** * @param string $name * @return bool */ public function __isset($name) { return $name === 'id' || array_key_exists($name, $this->attributes); } /** * @param string $name */ public function __unset($name) { if ($name === 'id') { trigger_error("The '\$id' property is read-only", E_DEPRECATED); return; } unset($this->attributes[$name]); } /** * @return string */ public function serialize() { return (string) $this->objectID; } /** * @param string $serialized */ public function unserialize($serialized) { $this->createObjectID($serialized); } /** * Gets the incremented value to create this id * @link http://php.net/manual/en/mongoid.getinc.php * @return int Returns the incremented value used to create this MongoId. */ public function getInc() { return hexdec(substr((string) $this->objectID, -6)); } /** * (PECL mongo >= 1.0.11) * Gets the process ID * @link http://php.net/manual/en/mongoid.getpid.php * @return int Returns the PID of the MongoId. */ public function getPID() { $id = (string) $this->objectID; // PID is stored as little-endian, flip it around $pid = substr($id, 16, 2) . substr($id, 14, 2); return hexdec($pid); } /** * (PECL mongo >= 1.0.1) * Gets the number of seconds since the epoch that this id was created * @link http://www.php.net/manual/en/mongoid.gettimestamp.php * @return int */ public function getTimestamp() { return hexdec(substr((string) $this->objectID, 0, 8)); } /** * Gets the hostname being used for this machine's ids * @link http://www.php.net/manual/en/mongoid.gethostname.php * @return string */ public static function getHostname() { return gethostname(); } /** * (PECL mongo >= 1.0.8) * Create a dummy MongoId * @link http://php.net/manual/en/mongoid.set-state.php * @param array $props
Theoretically, an array of properties used to create the new id. However, as MongoId instances have no properties, this is not used.
* @return MongoId A new id with the value "000000000000000000000000". */ public static function __set_state(array $props) { } /** * @param $id * @throws MongoException */ private function createObjectID($id) { try { if (is_string($id)) { $this->objectID = new ObjectID($id); } elseif ($id instanceof self || $id instanceof ObjectID) { $this->objectID = new ObjectID((string) $id); } else { $this->objectID = new ObjectId(); } } catch (\Exception $e) { throw new MongoException('Invalid object ID', 19); } } }