MongoId.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. private $attributes = [];
  24. /**
  25. * Creates a new id
  26. *
  27. *
  28. * @link http://www.php.net/manual/en/mongoid.construct.php
  29. * @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.
  30. * @return MongoId
  31. *
  32. * @throws MongoException
  33. */
  34. public function __construct($id = null)
  35. {
  36. $this->createObjectID($id);
  37. }
  38. /**
  39. * Check if a value is a valid ObjectId
  40. *
  41. * @link http://php.net/manual/en/mongoid.isvalid.php
  42. * @param mixed $value The value to check for validity.
  43. * @return bool
  44. */
  45. public static function isValid($value)
  46. {
  47. if ($value instanceof ObjectID || $value instanceof MongoId) {
  48. return true;
  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 $this->attributes[$name];
  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. $this->attributes[$name] = $value;
  94. }
  95. /**
  96. * @param string $name
  97. * @return bool
  98. */
  99. public function __isset($name)
  100. {
  101. return $name === 'id' || array_key_exists($name, $this->attributes);
  102. }
  103. /**
  104. * @param string $name
  105. */
  106. public function __unset($name)
  107. {
  108. if ($name === 'id') {
  109. trigger_error("The '\$id' property is read-only", E_DEPRECATED);
  110. return;
  111. }
  112. unset($this->attributes[$name]);
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function serialize()
  118. {
  119. return (string) $this->objectID;
  120. }
  121. /**
  122. * @param string $serialized
  123. */
  124. public function unserialize($serialized)
  125. {
  126. $this->createObjectID($serialized);
  127. }
  128. /**
  129. * Gets the incremented value to create this id
  130. * @link http://php.net/manual/en/mongoid.getinc.php
  131. * @return int Returns the incremented value used to create this MongoId.
  132. */
  133. public function getInc()
  134. {
  135. return hexdec(substr((string) $this->objectID, -6));
  136. }
  137. /**
  138. * (PECL mongo &gt;= 1.0.11)
  139. * Gets the process ID
  140. * @link http://php.net/manual/en/mongoid.getpid.php
  141. * @return int Returns the PID of the MongoId.
  142. */
  143. public function getPID()
  144. {
  145. $id = (string) $this->objectID;
  146. // PID is stored as little-endian, flip it around
  147. $pid = substr($id, 16, 2) . substr($id, 14, 2);
  148. return hexdec($pid);
  149. }
  150. /**
  151. * (PECL mongo &gt;= 1.0.1)
  152. * Gets the number of seconds since the epoch that this id was created
  153. * @link http://www.php.net/manual/en/mongoid.gettimestamp.php
  154. * @return int
  155. */
  156. public function getTimestamp()
  157. {
  158. return hexdec(substr((string) $this->objectID, 0, 8));
  159. }
  160. /**
  161. * Gets the hostname being used for this machine's ids
  162. * @link http://www.php.net/manual/en/mongoid.gethostname.php
  163. * @return string
  164. */
  165. public static function getHostname()
  166. {
  167. return gethostname();
  168. }
  169. /**
  170. * (PECL mongo &gt;= 1.0.8)
  171. * Create a dummy MongoId
  172. * @link http://php.net/manual/en/mongoid.set-state.php
  173. * @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>
  174. * @return MongoId A new id with the value "000000000000000000000000".
  175. */
  176. public static function __set_state(array $props)
  177. {
  178. }
  179. /**
  180. * @param $id
  181. * @throws MongoException
  182. */
  183. private function createObjectID($id)
  184. {
  185. try {
  186. if (is_string($id)) {
  187. $this->objectID = new ObjectID($id);
  188. } elseif ($id instanceof self || $id instanceof ObjectID) {
  189. $this->objectID = new ObjectID((string) $id);
  190. } else {
  191. $this->objectID = new ObjectId();
  192. }
  193. } catch (\Exception $e) {
  194. throw new MongoException('Invalid object ID', 19);
  195. }
  196. }
  197. }