| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- /*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- use MongoDB\BSON\ObjectID;
- class MongoId implements Serializable
- {
- /*
- * @var ObjectID
- */
- private $objectID;
- private $attributes = [];
- /**
- * Creates a new id
- *
- *
- * @link http://www.php.net/manual/en/mongoid.construct.php
- * @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.
- * @return MongoId
- *
- * @throws MongoException
- */
- public function __construct($id = null)
- {
- $this->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 <p>Theoretically, an array of properties used to create the new id. However, as MongoId instances have no properties, this is not used.</p>
- * @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);
- }
- }
- }
|