MongoTimestamp.php 2.7 KB

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