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
|
|
|
|
16
|
|
|
if (class_exists('MongoTimestamp', false)) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
use Alcaeus\MongoDbAdapter\TypeInterface; |
21
|
|
|
use MongoDB\BSON\Timestamp; |
22
|
|
|
|
23
|
|
|
class MongoTimestamp implements TypeInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private static $globalInc = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @link http://php.net/manual/en/class.mongotimestamp.php#mongotimestamp.props.sec |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
public $sec; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @link http://php.net/manual/en/class.mongotimestamp.php#mongotimestamp.props.inc |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
public $inc; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a new timestamp. If no parameters are given, the current time is used |
44
|
|
|
* and the increment is automatically provided. The increment is set to 0 when the |
45
|
|
|
* module is loaded and is incremented every time this constructor is called |
46
|
|
|
* (without the $inc parameter passed in). |
47
|
|
|
* |
48
|
|
|
* @link http://php.net/manual/en/mongotimestamp.construct.php |
49
|
|
|
* @param int $sec [optional] Number of seconds since January 1st, 1970 |
50
|
|
|
* @param int $inc [optional] Increment |
51
|
|
|
*/ |
52
|
|
|
public function __construct($sec = 0, $inc = 0) |
53
|
|
|
{ |
54
|
|
|
if ($sec instanceof Timestamp) { |
55
|
|
|
// Only way is to convert is from string: [<inc>:<sec>] |
56
|
|
|
$parts = explode(':', substr((string) $sec, 1, -1)); |
57
|
|
|
$this->sec = (int) $parts[1]; |
58
|
|
|
$this->inc = (int) $parts[0]; |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (func_num_args() == 0) { |
64
|
|
|
$sec = time(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (func_num_args() <= 1) { |
68
|
|
|
$inc = static::$globalInc; |
|
|
|
|
69
|
|
|
static::$globalInc++; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->sec = (int) $sec; |
73
|
|
|
$this->inc = (int) $inc; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function __toString() |
80
|
|
|
{ |
81
|
|
|
return (string) $this->sec; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Converts this MongoTimestamp to the new BSON Timestamp type |
86
|
|
|
* |
87
|
|
|
* @return Timestamp |
88
|
|
|
* @internal This method is not part of the ext-mongo API |
89
|
|
|
*/ |
90
|
|
|
public function toBSONType() |
91
|
|
|
{ |
92
|
|
|
return new Timestamp($this->inc, $this->sec); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: