MongoId.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. }
  48. return (bool) preg_match('#^[a-f0-9]{24}$#i', $value);
  49. }
  50. /**
  51. * Returns a hexidecimal representation of this id
  52. * @link http://www.php.net/manual/en/mongoid.tostring.php
  53. * @return string
  54. */
  55. public function __toString()
  56. {
  57. return (string) $this->objectID;
  58. }
  59. /**
  60. * Converts this MongoId to the new BSON ObjectID type
  61. *
  62. * @return ObjectID
  63. * @internal This method is not part of the ext-mongo API
  64. */
  65. public function toBSONType()
  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 null;
  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. }
  92. /**
  93. * @param string $name
  94. * @return bool
  95. */
  96. public function __isset($name)
  97. {
  98. return $name === 'id';
  99. }
  100. /**
  101. * @param string $name
  102. */
  103. public function __unset($name)
  104. {
  105. if ($name === 'id') {
  106. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  107. return;
  108. }
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function serialize()
  114. {
  115. return (string) $this->objectID;
  116. }
  117. /**
  118. * @param string $serialized
  119. */
  120. public function unserialize($serialized)
  121. {
  122. $this->createObjectID($serialized);
  123. }
  124. /**
  125. * Gets the incremented value to create this id
  126. * @link http://php.net/manual/en/mongoid.getinc.php
  127. * @return int Returns the incremented value used to create this MongoId.
  128. */
  129. public function getInc()
  130. {
  131. return hexdec(substr((string) $this->objectID, -6));
  132. }
  133. /**
  134. * (PECL mongo &gt;= 1.0.11)
  135. * Gets the process ID
  136. * @link http://php.net/manual/en/mongoid.getpid.php
  137. * @return int Returns the PID of the MongoId.
  138. */
  139. public function getPID()
  140. {
  141. $id = (string) $this->objectID;
  142. // PID is stored as little-endian, flip it around
  143. $pid = substr($id, 16, 2) . substr($id, 14, 2);
  144. return hexdec($pid);
  145. }
  146. /**
  147. * (PECL mongo &gt;= 1.0.1)
  148. * Gets the number of seconds since the epoch that this id was created
  149. * @link http://www.php.net/manual/en/mongoid.gettimestamp.php
  150. * @return int
  151. */
  152. public function getTimestamp()
  153. {
  154. return hexdec(substr((string) $this->objectID, 0, 8));
  155. }
  156. /**
  157. * Gets the hostname being used for this machine's ids
  158. * @link http://www.php.net/manual/en/mongoid.gethostname.php
  159. * @return string
  160. */
  161. public static function getHostname()
  162. {
  163. return gethostname();
  164. }
  165. /**
  166. * (PECL mongo &gt;= 1.0.8)
  167. * Create a dummy MongoId
  168. * @link http://php.net/manual/en/mongoid.set-state.php
  169. * @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>
  170. * @return MongoId A new id with the value "000000000000000000000000".
  171. */
  172. public static function __set_state(array $props)
  173. {
  174. }
  175. /**
  176. * @param $id
  177. * @throws MongoException
  178. */
  179. private function createObjectID($id)
  180. {
  181. try {
  182. if (is_string($id)) {
  183. $this->objectID = new ObjectID($id);
  184. } elseif ($id instanceof self || $id instanceof ObjectID) {
  185. $this->objectID = new ObjectID((string) $id);
  186. } else {
  187. $this->objectID = new ObjectId();
  188. }
  189. } catch (\Exception $e) {
  190. throw new MongoException('Invalid object ID', 19);
  191. }
  192. }
  193. }