MongoId.php 6.0 KB

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