MongoTimestamp.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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('MongoTimestamp', false)) {
  16. return;
  17. }
  18. use Alcaeus\MongoDbAdapter\TypeInterface;
  19. use MongoDB\BSON\Timestamp;
  20. class MongoTimestamp implements TypeInterface
  21. {
  22. /**
  23. * @var int
  24. */
  25. private static $globalInc = 0;
  26. /**
  27. * @link http://php.net/manual/en/class.mongotimestamp.php#mongotimestamp.props.sec
  28. * @var int
  29. */
  30. public $sec;
  31. /**
  32. * @link http://php.net/manual/en/class.mongotimestamp.php#mongotimestamp.props.inc
  33. * @var int
  34. */
  35. public $inc;
  36. /**
  37. * Creates a new timestamp. If no parameters are given, the current time is used
  38. * and the increment is automatically provided. The increment is set to 0 when the
  39. * module is loaded and is incremented every time this constructor is called
  40. * (without the $inc parameter passed in).
  41. *
  42. * @link http://php.net/manual/en/mongotimestamp.construct.php
  43. * @param int $sec [optional] Number of seconds since January 1st, 1970
  44. * @param int $inc [optional] Increment
  45. */
  46. public function __construct($sec = 0, $inc = 0)
  47. {
  48. if ($sec instanceof Timestamp) {
  49. // Only way is to convert is from string: [<inc>:<sec>]
  50. $parts = explode(':', substr((string) $sec, 1, -1));
  51. $this->sec = (int) $parts[1];
  52. $this->inc = (int) $parts[0];
  53. return;
  54. }
  55. if (func_num_args() == 0) {
  56. $sec = time();
  57. }
  58. if (func_num_args() <= 1) {
  59. $inc = static::$globalInc;
  60. static::$globalInc++;
  61. }
  62. $this->sec = (int) $sec;
  63. $this->inc = (int) $inc;
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function __toString()
  69. {
  70. return (string) $this->sec;
  71. }
  72. /**
  73. * Converts this MongoTimestamp to the new BSON Timestamp type
  74. *
  75. * @return Timestamp
  76. * @internal This method is not part of the ext-mongo API
  77. */
  78. public function toBSONType()
  79. {
  80. return new Timestamp($this->inc, $this->sec);
  81. }
  82. }