1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\ASN1\Type; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Base class for all types representing a point in time. |
11
|
|
|
*/ |
12
|
|
|
abstract class BaseTime extends Element implements TimeType |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* UTC timezone. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
const TZ_UTC = 'UTC'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Date and time. |
23
|
|
|
* |
24
|
|
|
* @var \DateTimeImmutable |
25
|
|
|
*/ |
26
|
|
|
protected $_dateTime; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
*/ |
31
|
25 |
|
public function __construct(\DateTimeImmutable $dt) |
32
|
|
|
{ |
33
|
25 |
|
$this->_dateTime = $dt; |
34
|
25 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function __toString(): string |
40
|
|
|
{ |
41
|
2 |
|
return $this->string(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialize from datetime string. |
46
|
|
|
* |
47
|
|
|
* @see http://php.net/manual/en/datetime.formats.php |
48
|
|
|
* |
49
|
|
|
* @param string $time Time string |
50
|
|
|
* @param null|string $tz timezone, if null use default |
51
|
|
|
* |
52
|
|
|
* @throws \RuntimeException |
53
|
|
|
*/ |
54
|
6 |
|
public static function fromString(string $time, ?string $tz = null): self |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
6 |
|
if (!isset($tz)) { |
58
|
4 |
|
$tz = date_default_timezone_get(); |
59
|
|
|
} |
60
|
6 |
|
return new static( |
61
|
6 |
|
new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
62
|
2 |
|
} catch (\Exception $e) { |
63
|
2 |
|
throw new \RuntimeException( |
64
|
|
|
'Failed to create DateTime: ' . |
65
|
2 |
|
self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the date and time. |
71
|
|
|
*/ |
72
|
7 |
|
public function dateTime(): \DateTimeImmutable |
73
|
|
|
{ |
74
|
7 |
|
return $this->_dateTime; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the date and time as a type specific string. |
79
|
|
|
*/ |
80
|
5 |
|
public function string(): string |
81
|
|
|
{ |
82
|
5 |
|
return $this->_encodedContentDER(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create `DateTimeZone` object from string. |
87
|
|
|
* |
88
|
|
|
* @throws \UnexpectedValueException If timezone is invalid |
89
|
|
|
*/ |
90
|
31 |
|
protected static function _createTimeZone(string $tz): \DateTimeZone |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
31 |
|
return new \DateTimeZone($tz); |
94
|
1 |
|
} catch (\Exception $e) { |
95
|
1 |
|
throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get last error caused by `DateTimeImmutable`. |
101
|
|
|
*/ |
102
|
3 |
|
protected static function _getLastDateTimeImmutableErrorsStr(): string |
103
|
|
|
{ |
104
|
3 |
|
$errors = \DateTimeImmutable::getLastErrors()['errors']; |
105
|
3 |
|
return implode(', ', $errors); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|