-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from redthor/fix-get-migrated-timestamp
Fix get migrated timestamp
- Loading branch information
Showing
5 changed files
with
259 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/AntiMattr/MongoDB/Migrations/Configuration/Timestamp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. | ||
* | ||
* (c) 2014 Matthew Fitzgerald | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace AntiMattr\MongoDB\Migrations\Configuration; | ||
|
||
/** | ||
* This class is to normalise the potential values from the 't' version | ||
* attribute. | ||
* | ||
* @author Douglas Reith <[email protected]> | ||
*/ | ||
class Timestamp | ||
{ | ||
private $t; | ||
|
||
/** | ||
* @param mixed $t | ||
*/ | ||
public function __construct($t) | ||
{ | ||
$this->t = $t; | ||
} | ||
|
||
/** | ||
* Normalise based on the different options for backward/forward | ||
* compatibility. | ||
* | ||
* @return int Time in seconds since 1970 | ||
*/ | ||
public function getTimestamp(): int | ||
{ | ||
$supportedClasses = implode( | ||
', ', | ||
[ | ||
'\MongoTimestamp', | ||
'\MongoDate', | ||
'\MongoDB\BSON\Timestamp', | ||
'\MongoDB\BSON\UTCDateTime', | ||
'\DateTimeInterface', | ||
] | ||
); | ||
|
||
if (!$this->t || !is_object($this->t)) { | ||
throw new \DomainException( | ||
'The timestamp to normalise must be one of ' . $supportedClasses . ' but it is not an object' | ||
); | ||
} | ||
|
||
switch (get_class($this->t)) { | ||
case 'MongoTimestamp': | ||
return (int) $this->t->__toString(); | ||
|
||
case 'MongoDate': | ||
return $this->t->sec; | ||
|
||
case 'MongoDB\BSON\Timestamp': | ||
return $this->t->getTimestamp(); | ||
|
||
case 'MongoDB\BSON\UTCDateTime': | ||
return (int) $this->t->toDateTime()->format('U'); | ||
} | ||
|
||
if ($this->t instanceof \DateTimeInterface) { | ||
return $this->t->getTimestamp(); | ||
} | ||
|
||
throw new \DomainException( | ||
'The normalised timestamp must be one of ' . $supportedClasses | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return (string) $this->getTimestamp(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.