MongoId.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. *
  30. * @throws MongoException
  31. */
  32. public function __construct($id = null)
  33. {
  34. $this->createObjectID($id);
  35. }
  36. /**
  37. * Check if a value is a valid ObjectId
  38. *
  39. * @link http://php.net/manual/en/mongoid.isvalid.php
  40. * @param mixed $value The value to check for validity.
  41. * @return bool
  42. */
  43. public static function isValid($value)
  44. {
  45. if ($value instanceof ObjectID || $value instanceof MongoId) {
  46. return true;
  47. } elseif (! is_string($value)) {
  48. return false;
  49. }
  50. return (bool) preg_match('#^[a-f0-9]{24}$#i', $value);
  51. }
  52. /**
  53. * Returns a hexidecimal representation of this id
  54. * @link http://www.php.net/manual/en/mongoid.tostring.php
  55. * @return string
  56. */
  57. public function __toString()
  58. {
  59. return (string) $this->objectID;
  60. }
  61. /**
  62. * Converts this MongoId to the new BSON ObjectID type
  63. *
  64. * @return ObjectID
  65. * @internal This method is not part of the ext-mongo API
  66. */
  67. public function toBSONType()
  68. {
  69. return $this->objectID;
  70. }
  71. /**
  72. * @param string $name
  73. *
  74. * @return null|string
  75. */
  76. public function __get($name)
  77. {
  78. if ($name === '$id') {
  79. return (string) $this->objectID;
  80. }
  81. return null;
  82. }
  83. /**
  84. * @param string $name
  85. * @param mixed $value
  86. */
  87. public function __set($name, $value)
  88. {
  89. if ($name === 'id') {
  90. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  91. return;
  92. }
  93. }
  94. /**
  95. * @param string $name
  96. * @return bool
  97. */
  98. public function __isset($name)
  99. {
  100. return $name === 'id';
  101. }
  102. /**
  103. * @param string $name
  104. */
  105. public function __unset($name)
  106. {
  107. if ($name === 'id') {
  108. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  109. return;
  110. }
  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. }