-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathTime.php
More file actions
117 lines (100 loc) · 2.98 KB
/
Time.php
File metadata and controls
117 lines (100 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Type;
use Ramsey\Uuid\Type\Integer as IntegerObject;
use ValueError;
use function sprintf;
/**
* A value object representing a timestamp
*
* This class exists for type-safety purposes, to ensure that timestamps used by ramsey/uuid are truly timestamp
* integers and not some other kind of string or integer.
*
* @immutable
*/
final class Time implements TypeInterface
{
private readonly IntegerObject $seconds;
private readonly IntegerObject $microseconds;
public function __construct(
IntegerObject | float | int | string $seconds,
IntegerObject | float | int | string $microseconds = new IntegerObject(0),
) {
$this->seconds = $seconds instanceof IntegerObject ? $seconds : new IntegerObject($seconds);
$this->microseconds = $microseconds instanceof IntegerObject ? $microseconds : new IntegerObject($microseconds);
}
/**
* @pure
*/
public function getSeconds(): IntegerObject
{
return $this->seconds;
}
/**
* @pure
*/
public function getMicroseconds(): IntegerObject
{
return $this->microseconds;
}
/**
* @return numeric-string
*/
public function toString(): string
{
/** @var numeric-string $microseconds */
$microseconds = sprintf('%06s', $this->microseconds->toString());
/** @var numeric-string $time */
$time = "{$this->seconds->toString()}.$microseconds";
return $time;
}
/**
* @return numeric-string
*/
public function __toString(): string
{
return $this->toString();
}
/**
* @return array{seconds: string, microseconds: string}
*/
public function jsonSerialize(): array
{
return [
'seconds' => $this->getSeconds()->toString(),
'microseconds' => $this->getMicroseconds()->toString(),
];
}
/**
* @return array{seconds: string, microseconds: string}
*/
public function __serialize(): array
{
return [
'seconds' => $this->getSeconds()->toString(),
'microseconds' => $this->getMicroseconds()->toString(),
];
}
/**
* @inheritDoc
*/
public function __unserialize(array $data): void
{
if (!isset($data['seconds']) || !isset($data['microseconds'])) {
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));
}
assert(is_string($data['seconds']));
assert(is_string($data['microseconds']));
$this->seconds = new IntegerObject($data['seconds']);
$this->microseconds = new IntegerObject($data['microseconds']);
}
}