| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the ramsey/uuid library |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
* |
| 8 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 9 |
* @license http://opensource.org/licenses/MIT MIT |
| 10 |
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation |
| 11 |
* @link https://packagist.org/packages/ramsey/uuid Packagist |
| 12 |
* @link https://github.com/ramsey/uuid GitHub |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Ramsey\Uuid; |
| 16 |
|
| 17 |
use DateTime; |
| 18 |
use Moontoast\Math\BigNumber; |
| 19 |
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; |
| 20 |
use Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 21 |
|
| 22 |
/** |
| 23 |
* DegradedUuid represents an RFC 4122 UUID on 32-bit systems |
| 24 |
* |
| 25 |
* @see Uuid |
| 26 |
*/ |
| 27 |
class DegradedUuid extends Uuid |
| 28 |
{ |
| 29 |
/** |
| 30 |
* @inheritdoc |
| 31 |
*/ |
| 32 |
public function getDateTime() |
| 33 |
{ |
| 34 |
if ($this->getVersion() != 1) { |
| 35 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 36 |
} |
| 37 |
|
| 38 |
$time = $this->converter->fromHex($this->getTimestampHex()); |
| 39 |
|
| 40 |
$ts = new BigNumber($time, 20); |
| 41 |
$ts->subtract('122192928000000000'); |
| 42 |
$ts->divide('10000000.0'); |
| 43 |
$ts->floor(); |
| 44 |
$unixTime = $ts->getValue(); |
| 45 |
|
| 46 |
return new DateTime("@{$unixTime}"); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when |
| 51 |
* called on a 32-bit system |
| 52 |
* |
| 53 |
* @throws UnsatisfiedDependencyException if called on a 32-bit system |
| 54 |
*/ |
| 55 |
public function getFields() |
| 56 |
{ |
| 57 |
throw new UnsatisfiedDependencyException( |
| 58 |
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since some ' |
| 59 |
. 'values overflow the system max integer value' |
| 60 |
. '; consider calling getFieldsHex instead' |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when |
| 66 |
* called on a 32-bit system |
| 67 |
* |
| 68 |
* @throws UnsatisfiedDependencyException if called on a 32-bit system |
| 69 |
*/ |
| 70 |
public function getNode() |
| 71 |
{ |
| 72 |
throw new UnsatisfiedDependencyException( |
| 73 |
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since node ' |
| 74 |
. 'is an unsigned 48-bit integer and can overflow the system ' |
| 75 |
. 'max integer value' |
| 76 |
. '; consider calling getNodeHex instead' |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when |
| 82 |
* called on a 32-bit system |
| 83 |
* |
| 84 |
* @throws UnsatisfiedDependencyException if called on a 32-bit system |
| 85 |
*/ |
| 86 |
public function getTimeLow() |
| 87 |
{ |
| 88 |
throw new UnsatisfiedDependencyException( |
| 89 |
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since time_low ' |
| 90 |
. 'is an unsigned 32-bit integer and can overflow the system ' |
| 91 |
. 'max integer value' |
| 92 |
. '; consider calling getTimeLowHex instead' |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* For degraded UUIDs, throws an `UnsatisfiedDependencyException` when |
| 98 |
* called on a 32-bit system |
| 99 |
* |
| 100 |
* @throws UnsatisfiedDependencyException if called on a 32-bit system |
| 101 |
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID |
| 102 |
*/ |
| 103 |
public function getTimestamp() |
| 104 |
{ |
| 105 |
if ($this->getVersion() != 1) { |
| 106 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 107 |
} |
| 108 |
|
| 109 |
throw new UnsatisfiedDependencyException( |
| 110 |
'Cannot call ' . __METHOD__ . ' on a 32-bit system, since timestamp ' |
| 111 |
. 'is an unsigned 60-bit integer and can overflow the system ' |
| 112 |
. 'max integer value' |
| 113 |
. '; consider calling getTimestampHex instead' |
| 114 |
); |
| 115 |
} |
| 116 |
} |