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