MongoId.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. use MongoDB\BSON\ObjectID;
  16. class MongoId implements Serializable
  17. {
  18. /*
  19. * @var ObjectID
  20. */
  21. private $objectID;
  22. private $attributes = [];
  23. /**
  24. * Creates a new id
  25. *
  26. *
  27. * @link http://www.php.net/manual/en/mongoid.construct.php
  28. * @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.
  29. * @return MongoId
  30. *
  31. * @throws MongoException
  32. */
  33. public function __construct($id = null)
  34. {
  35. $this->createObjectID($id);
  36. }
  37. /**
  38. * Check if a value is a valid ObjectId
  39. *
  40. * @link http://php.net/manual/en/mongoid.isvalid.php
  41. * @param mixed $value The value to check for validity.
  42. * @return bool
  43. */
  44. public static function isValid($value)
  45. {
  46. if ($value instanceof ObjectID || $value instanceof MongoId) {
  47. return true;
  48. }
  49. return (bool) preg_match('#^[a-f0-9]{24}$#i', $value);
  50. }
  51. /**
  52. * Returns a hexidecimal representation of this id
  53. * @link http://www.php.net/manual/en/mongoid.tostring.php
  54. * @return string
  55. */
  56. public function __toString()
  57. {
  58. return (string) $this->objectID;
  59. }
  60. /**
  61. * Returns the ObjectID used internally
  62. * @return ObjectID
  63. * @internal This method is not part of the ext-mongo API. Do not use this!
  64. */
  65. public function getObjectID()
  66. {
  67. return $this->objectID;
  68. }
  69. /**
  70. * @param string $name
  71. *
  72. * @return null|string
  73. */
  74. public function __get($name)
  75. {
  76. if ($name === '$id') {
  77. return (string) $this->objectID;
  78. }
  79. return $this->attributes[$name];
  80. }
  81. /**
  82. * @param string $name
  83. * @param mixed $value
  84. */
  85. public function __set($name, $value)
  86. {
  87. if ($name === 'id') {
  88. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  89. return;
  90. }
  91. $this->attributes[$name] = $value;
  92. }
  93. /**
  94. * @param string $name
  95. * @return bool
  96. */
  97. public function __isset($name)
  98. {
  99. return $name === 'id' || array_key_exists($name, $this->attributes);
  100. }
  101. /**
  102. * @param string $name
  103. */
  104. public function __unset($name)
  105. {
  106. if ($name === 'id') {
  107. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  108. return;
  109. }
  110. unset($this->attributes[$name]);
  111. }
  112. /**
  113. * @return string
  114. */
  115. public function serialize()
  116. {
  117. return (string) $this->objectID;
  118. }
  119. /**
  120. * @param string $serialized
  121. */
  122. public function unserialize($serialized)
  123. {
  124. $this->createObjectID($serialized);
  125. }
  126. /**
  127. * Gets the incremented value to create this id
  128. * @link http://php.net/manual/en/mongoid.getinc.php
  129. * @return int Returns the incremented value used to create this MongoId.
  130. */
  131. public function getInc()
  132. {
  133. return hexdec(substr((string) $this->objectID, -6));
  134. }
  135. /**
  136. * (PECL mongo &gt;= 1.0.11)
  137. * Gets the process ID
  138. * @link http://php.net/manual/en/mongoid.getpid.php
  139. * @return int Returns the PID of the MongoId.
  140. */
  141. public function getPID()
  142. {
  143. $id = (string) $this->objectID;
  144. // PID is stored as little-endian, flip it around
  145. $pid = substr($id, 16, 2) . substr($id, 14, 2);
  146. return hexdec($pid);
  147. }
  148. /**
  149. * (PECL mongo &gt;= 1.0.1)
  150. * Gets the number of seconds since the epoch that this id was created
  151. * @link http://www.php.net/manual/en/mongoid.gettimestamp.php
  152. * @return int
  153. */
  154. public function getTimestamp()
  155. {
  156. return hexdec(substr((string) $this->objectID, 0, 8));
  157. }
  158. /**
  159. * Gets the hostname being used for this machine's ids
  160. * @link http://www.php.net/manual/en/mongoid.gethostname.php
  161. * @return string
  162. */
  163. public static function getHostname()
  164. {
  165. return gethostname();
  166. }
  167. /**
  168. * (PECL mongo &gt;= 1.0.8)
  169. * Create a dummy MongoId
  170. * @link http://php.net/manual/en/mongoid.set-state.php
  171. * @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>
  172. * @return MongoId A new id with the value "000000000000000000000000".
  173. */
  174. public static function __set_state(array $props)
  175. {
  176. }
  177. /**
  178. * @param $id
  179. * @throws MongoException
  180. */
  181. private function createObjectID($id)
  182. {
  183. try {
  184. if (is_string($id)) {
  185. $this->objectID = new ObjectID($id);
  186. } elseif ($id instanceof self || $id instanceof ObjectID) {
  187. $this->objectID = new ObjectID((string) $id);
  188. } else {
  189. $this->objectID = new ObjectId();
  190. }
  191. } catch (\Exception $e) {
  192. throw new MongoException('Invalid object ID', 19);
  193. }
  194. }
  195. }