1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
5
|
|
|
* |
6
|
|
|
* (c) 2014 Matthew Fitzgerald |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AntiMattr\MongoDB\Migrations\Configuration; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class is to normalise the potential values from the 't' version |
16
|
|
|
* attribute. |
17
|
|
|
* |
18
|
|
|
* @author Douglas Reith <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Timestamp |
21
|
|
|
{ |
22
|
|
|
private $t; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* __construct. |
26
|
|
|
* |
27
|
|
|
* @param mixed $t |
28
|
|
|
*/ |
29
|
6 |
|
public function __construct($t) |
30
|
|
|
{ |
31
|
6 |
|
$this->t = $t; |
32
|
6 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* __invoke. |
36
|
|
|
* |
37
|
|
|
* @param mixed $t |
38
|
|
|
* |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
|
|
public function __invoke($t): int |
42
|
|
|
{ |
43
|
|
|
return (new static($t))->getTimestamp(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* getTimestamp. |
48
|
|
|
* |
49
|
|
|
* Normalise based on the different options for backward/forward |
50
|
|
|
* compatibility |
51
|
|
|
* |
52
|
|
|
* @return int Time in seconds since 1970 |
53
|
|
|
*/ |
54
|
6 |
|
public function getTimestamp(): int |
55
|
|
|
{ |
56
|
6 |
|
$supportedClasses = implode( |
57
|
6 |
|
', ', |
58
|
|
|
[ |
59
|
6 |
|
'\MongoTimestamp', |
60
|
|
|
'\MongoDate', |
61
|
|
|
'\MongoDB\BSON\Timestamp', |
62
|
|
|
'\MongoDB\BSON\UTCDateTime', |
63
|
|
|
'\DateTimeInterface', |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
6 |
|
if (!$this->t || !is_object($this->t)) { |
68
|
1 |
|
throw new \DomainException( |
69
|
1 |
|
'The timestamp to normalise must be one of ' . $supportedClasses . ' but it is not an object' |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
switch (get_class($this->t)) { |
74
|
5 |
|
case 'MongoTimestamp': |
75
|
|
|
return (int) $this->t->__toString(); |
76
|
|
|
|
77
|
5 |
|
case 'MongoDate': |
78
|
|
|
return $this->t->sec; |
79
|
|
|
|
80
|
5 |
|
case 'MongoDB\BSON\Timestamp': |
81
|
1 |
|
return $this->t->getTimestamp(); |
82
|
|
|
|
83
|
4 |
|
case 'MongoDB\BSON\UTCDateTime': |
84
|
1 |
|
return (int) $this->t->toDateTime()->format('U'); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
if ($this->t instanceof \DateTimeInterface) { |
88
|
2 |
|
return $this->t->getTimestamp(); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
throw new \DomainException( |
92
|
1 |
|
'The normalised timestamp must be one of ' . $supportedClasses |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* __toString. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
1 |
|
public function __toString(): string |
102
|
|
|
{ |
103
|
1 |
|
return (string) $this->getTimestamp(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|