| 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\Provider\Time; |
| 16 |
|
| 17 |
use InvalidArgumentException; |
| 18 |
use Ramsey\Uuid\Provider\TimeProviderInterface; |
| 19 |
|
| 20 |
/** |
| 21 |
* FixedTimeProvider uses an previously-generated timestamp to provide the time |
| 22 |
* |
| 23 |
* This provider allows the use of a previously-generated timestamp, such as one |
| 24 |
* stored in a database, when creating version 1 UUIDs. |
| 25 |
*/ |
| 26 |
class FixedTimeProvider implements TimeProviderInterface |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @var int[] Array containing `sec` and `usec` components of a timestamp |
| 30 |
*/ |
| 31 |
private $fixedTime; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructs a `FixedTimeProvider` using the provided `$timestamp` |
| 35 |
* |
| 36 |
* @param int[] Array containing `sec` and `usec` components of a timestamp |
| 37 |
* @throws InvalidArgumentException if the `$timestamp` does not contain `sec` or `usec` components |
| 38 |
*/ |
| 39 |
public function __construct(array $timestamp) |
| 40 |
{ |
| 41 |
if (!array_key_exists('sec', $timestamp) || !array_key_exists('usec', $timestamp)) { |
| 42 |
throw new InvalidArgumentException('Array must contain sec and usec keys.'); |
| 43 |
} |
| 44 |
|
| 45 |
$this->fixedTime = $timestamp; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Sets the `usec` component of the timestamp |
| 50 |
* |
| 51 |
* @param int $value The `usec` value to set |
| 52 |
*/ |
| 53 |
public function setUsec($value) |
| 54 |
{ |
| 55 |
$this->fixedTime['usec'] = $value; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the `sec` component of the timestamp |
| 60 |
* |
| 61 |
* @param int $value The `sec` value to set |
| 62 |
*/ |
| 63 |
public function setSec($value) |
| 64 |
{ |
| 65 |
$this->fixedTime['sec'] = $value; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns a timestamp array |
| 70 |
* |
| 71 |
* @return int[] Array containing `sec` and `usec` components of a timestamp |
| 72 |
*/ |
| 73 |
public function currentTime() |
| 74 |
{ |
| 75 |
return $this->fixedTime; |
| 76 |
} |
| 77 |
} |