MongoId.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 Alcaeus\MongoDbAdapter\TypeInterface;
  16. use MongoDB\BSON\ObjectID;
  17. class MongoId implements Serializable, TypeInterface
  18. {
  19. /*
  20. * @var ObjectID
  21. */
  22. private $objectID;
  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. * Converts this MongoId to the new BSON ObjectID type
  62. *
  63. * @return ObjectID
  64. * @internal This method is not part of the ext-mongo API
  65. */
  66. public function toBSONType()
  67. {
  68. return $this->objectID;
  69. }
  70. /**
  71. * @param string $name
  72. *
  73. * @return null|string
  74. */
  75. public function __get($name)
  76. {
  77. if ($name === '$id') {
  78. return (string) $this->objectID;
  79. }
  80. return null;
  81. }
  82. /**
  83. * @param string $name
  84. * @param mixed $value
  85. */
  86. public function __set($name, $value)
  87. {
  88. if ($name === 'id') {
  89. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  90. return;
  91. }
  92. }
  93. /**
  94. * @param string $name
  95. * @return bool
  96. */
  97. public function __isset($name)
  98. {
  99. return $name === 'id';
  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. }
  111. /**
  112. * @return string
  113. */
  114. public function serialize()
  115. {
  116. return (string) $this->objectID;
  117. }
  118. /**
  119. * @param string $serialized
  120. */
  121. public function unserialize($serialized)
  122. {
  123. $this->createObjectID($serialized);
  124. }
  125. /**
  126. * Gets the incremented value to create this id
  127. * @link http://php.net/manual/en/mongoid.getinc.php
  128. * @return int Returns the incremented value used to create this MongoId.
  129. */
  130. public function getInc()
  131. {
  132. return hexdec(substr((string) $this->objectID, -6));
  133. }
  134. /**
  135. * (PECL mongo &gt;= 1.0.11)
  136. * Gets the process ID
  137. * @link http://php.net/manual/en/mongoid.getpid.php
  138. * @return int Returns the PID of the MongoId.
  139. */
  140. public function getPID()
  141. {
  142. $id = (string) $this->objectID;
  143. // PID is stored as little-endian, flip it around
  144. $pid = substr($id, 16, 2) . substr($id, 14, 2);
  145. return hexdec($pid);
  146. }
  147. /**
  148. * (PECL mongo &gt;= 1.0.1)
  149. * Gets the number of seconds since the epoch that this id was created
  150. * @link http://www.php.net/manual/en/mongoid.gettimestamp.php
  151. * @return int
  152. */
  153. public function getTimestamp()
  154. {
  155. return hexdec(substr((string) $this->objectID, 0, 8));
  156. }
  157. /**
  158. * Gets the hostname being used for this machine's ids
  159. * @link http://www.php.net/manual/en/mongoid.gethostname.php
  160. * @return string
  161. */
  162. public static function getHostname()
  163. {
  164. return gethostname();
  165. }
  166. /**
  167. * (PECL mongo &gt;= 1.0.8)
  168. * Create a dummy MongoId
  169. * @link http://php.net/manual/en/mongoid.set-state.php
  170. * @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>
  171. * @return MongoId A new id with the value "000000000000000000000000".
  172. */
  173. public static function __set_state(array $props)
  174. {
  175. }
  176. /**
  177. * @param $id
  178. * @throws MongoException
  179. */
  180. private function createObjectID($id)
  181. {
  182. try {
  183. if (is_string($id)) {
  184. $this->objectID = new ObjectID($id);
  185. } elseif ($id instanceof self || $id instanceof ObjectID) {
  186. $this->objectID = new ObjectID((string) $id);
  187. } else {
  188. $this->objectID = new ObjectId();
  189. }
  190. } catch (\Exception $e) {
  191. throw new MongoException('Invalid object ID', 19);
  192. }
  193. }
  194. }