|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWT\Claim\Feature; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait for claims having NumericDate value. |
|
9
|
|
|
*/ |
|
10
|
|
|
trait NumericDateClaim |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param int $timestamp Unix timestamp |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract public function __construct(int $timestamp); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get the parameter value. |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract public function value(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Initialize instance from date/time string. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $time `strtotime` compatible time string |
|
30
|
|
|
* @param string $tz Default timezone |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \RuntimeException |
|
33
|
|
|
* |
|
34
|
|
|
* @return static |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public static function fromString(string $time, string $tz = 'UTC') |
|
37
|
|
|
{ |
|
38
|
|
|
try { |
|
39
|
2 |
|
$dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
40
|
1 |
|
return new static($dt->getTimestamp()); |
|
41
|
1 |
|
} catch (\Exception $e) { |
|
42
|
1 |
|
throw new \RuntimeException( |
|
43
|
|
|
'Failed to create DateTime: ' . |
|
44
|
1 |
|
self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get date as a unix timestamp. |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function timestamp(): int |
|
52
|
|
|
{ |
|
53
|
1 |
|
return (int) $this->value(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get date as a datetime object. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $tz Timezone |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \RuntimeException |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function dateTime(string $tz = 'UTC'): \DateTimeImmutable |
|
64
|
|
|
{ |
|
65
|
3 |
|
$dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()), |
|
66
|
3 |
|
self::_createTimeZone($tz)); |
|
67
|
2 |
|
if (false === $dt) { |
|
68
|
1 |
|
throw new \RuntimeException( |
|
69
|
|
|
'Failed to create DateTime: ' . |
|
70
|
1 |
|
self::_getLastDateTimeImmutableErrorsStr()); |
|
71
|
|
|
} |
|
72
|
1 |
|
return $dt; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create DateTimeZone object from string. |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \UnexpectedValueException |
|
79
|
|
|
*/ |
|
80
|
5 |
|
private static function _createTimeZone(string $tz): \DateTimeZone |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
5 |
|
return new \DateTimeZone($tz); |
|
84
|
1 |
|
} catch (\Exception $e) { |
|
85
|
1 |
|
throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get last error caused by DateTimeImmutable. |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private static function _getLastDateTimeImmutableErrorsStr(): string |
|
93
|
|
|
{ |
|
94
|
2 |
|
$errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
95
|
2 |
|
return implode(', ', $errors); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|