@@ -11,48 +11,48 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class StringType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * String value. |
|
| 16 | - * |
|
| 17 | - * @var string $_string |
|
| 18 | - */ |
|
| 19 | - protected $_string; |
|
| 14 | + /** |
|
| 15 | + * String value. |
|
| 16 | + * |
|
| 17 | + * @var string $_string |
|
| 18 | + */ |
|
| 19 | + protected $_string; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param string $string |
|
| 25 | - * @throws \InvalidArgumentException |
|
| 26 | - */ |
|
| 27 | - public function __construct(string $string) |
|
| 28 | - { |
|
| 29 | - if (!$this->_validateString($string)) { |
|
| 30 | - throw new \InvalidArgumentException( |
|
| 31 | - sprintf("Not a valid %s string.", |
|
| 32 | - self::tagToName($this->_typeTag))); |
|
| 33 | - } |
|
| 34 | - $this->_string = $string; |
|
| 35 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param string $string |
|
| 25 | + * @throws \InvalidArgumentException |
|
| 26 | + */ |
|
| 27 | + public function __construct(string $string) |
|
| 28 | + { |
|
| 29 | + if (!$this->_validateString($string)) { |
|
| 30 | + throw new \InvalidArgumentException( |
|
| 31 | + sprintf("Not a valid %s string.", |
|
| 32 | + self::tagToName($this->_typeTag))); |
|
| 33 | + } |
|
| 34 | + $this->_string = $string; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get the string value. |
|
| 39 | - * |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function string(): string |
|
| 43 | - { |
|
| 44 | - return $this->_string; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Get the string value. |
|
| 39 | + * |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function string(): string |
|
| 43 | + { |
|
| 44 | + return $this->_string; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Check whether string is valid for the concrete type. |
|
| 49 | - * |
|
| 50 | - * @param string $string |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - protected function _validateString(string $string): bool |
|
| 54 | - { |
|
| 55 | - // Override in derived classes |
|
| 56 | - return true; |
|
| 57 | - } |
|
| 47 | + /** |
|
| 48 | + * Check whether string is valid for the concrete type. |
|
| 49 | + * |
|
| 50 | + * @param string $string |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + protected function _validateString(string $string): bool |
|
| 54 | + { |
|
| 55 | + // Override in derived classes |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -20,105 +20,105 @@ |
||
| 20 | 20 | * May be encoded back to complete DER encoding. |
| 21 | 21 | */ |
| 22 | 22 | class DERTaggedType extends TaggedType implements |
| 23 | - ExplicitTagging, |
|
| 24 | - ImplicitTagging |
|
| 23 | + ExplicitTagging, |
|
| 24 | + ImplicitTagging |
|
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Identifier. |
|
| 28 | - * |
|
| 29 | - * @var Identifier |
|
| 30 | - */ |
|
| 31 | - private $_identifier; |
|
| 26 | + /** |
|
| 27 | + * Identifier. |
|
| 28 | + * |
|
| 29 | + * @var Identifier |
|
| 30 | + */ |
|
| 31 | + private $_identifier; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * DER data. |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $_data; |
|
| 33 | + /** |
|
| 34 | + * DER data. |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $_data; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Offset to data. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_offset; |
|
| 40 | + /** |
|
| 41 | + * Offset to data. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_offset; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Constructor. |
|
| 49 | - * |
|
| 50 | - * @param Identifier $identifier |
|
| 51 | - * @param string $data |
|
| 52 | - * @param int $offset Offset to next byte after identifier |
|
| 53 | - */ |
|
| 54 | - public function __construct(Identifier $identifier, string $data, int $offset) |
|
| 55 | - { |
|
| 56 | - $this->_identifier = $identifier; |
|
| 57 | - $this->_data = $data; |
|
| 58 | - $this->_offset = $offset; |
|
| 59 | - $this->_typeTag = intval($identifier->tag()); |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Constructor. |
|
| 49 | + * |
|
| 50 | + * @param Identifier $identifier |
|
| 51 | + * @param string $data |
|
| 52 | + * @param int $offset Offset to next byte after identifier |
|
| 53 | + */ |
|
| 54 | + public function __construct(Identifier $identifier, string $data, int $offset) |
|
| 55 | + { |
|
| 56 | + $this->_identifier = $identifier; |
|
| 57 | + $this->_data = $data; |
|
| 58 | + $this->_offset = $offset; |
|
| 59 | + $this->_typeTag = intval($identifier->tag()); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * |
|
| 64 | - * @see \ASN1\Element::typeClass() |
|
| 65 | - * @return int |
|
| 66 | - */ |
|
| 67 | - public function typeClass(): int |
|
| 68 | - { |
|
| 69 | - return $this->_identifier->typeClass(); |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * |
|
| 64 | + * @see \ASN1\Element::typeClass() |
|
| 65 | + * @return int |
|
| 66 | + */ |
|
| 67 | + public function typeClass(): int |
|
| 68 | + { |
|
| 69 | + return $this->_identifier->typeClass(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * |
|
| 74 | - * @see \ASN1\Element::isConstructed() |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function isConstructed(): bool |
|
| 78 | - { |
|
| 79 | - return $this->_identifier->isConstructed(); |
|
| 80 | - } |
|
| 72 | + /** |
|
| 73 | + * |
|
| 74 | + * @see \ASN1\Element::isConstructed() |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function isConstructed(): bool |
|
| 78 | + { |
|
| 79 | + return $this->_identifier->isConstructed(); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * |
|
| 84 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - protected function _encodedContentDER(): string |
|
| 88 | - { |
|
| 89 | - $idx = $this->_offset; |
|
| 90 | - $length = Length::expectFromDER($this->_data, $idx); |
|
| 91 | - return substr($this->_data, $idx, $length->length()); |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * |
|
| 84 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + protected function _encodedContentDER(): string |
|
| 88 | + { |
|
| 89 | + $idx = $this->_offset; |
|
| 90 | + $length = Length::expectFromDER($this->_data, $idx); |
|
| 91 | + return substr($this->_data, $idx, $length->length()); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * |
|
| 96 | - * {@inheritdoc} |
|
| 97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | - * @return UnspecifiedType |
|
| 99 | - */ |
|
| 100 | - public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | - { |
|
| 102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | - $cls = self::_determineImplClass($identifier); |
|
| 104 | - $idx = $this->_offset; |
|
| 105 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 106 | - return new UnspecifiedType($element); |
|
| 107 | - } |
|
| 94 | + /** |
|
| 95 | + * |
|
| 96 | + * {@inheritdoc} |
|
| 97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 98 | + * @return UnspecifiedType |
|
| 99 | + */ |
|
| 100 | + public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 101 | + { |
|
| 102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 103 | + $cls = self::_determineImplClass($identifier); |
|
| 104 | + $idx = $this->_offset; |
|
| 105 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 106 | + return new UnspecifiedType($element); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * |
|
| 111 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 112 | - * @return UnspecifiedType |
|
| 113 | - */ |
|
| 114 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 115 | - { |
|
| 116 | - $idx = $this->_offset; |
|
| 117 | - Length::expectFromDER($this->_data, $idx); |
|
| 118 | - $element = Element::fromDER($this->_data, $idx); |
|
| 119 | - if (isset($expectedTag)) { |
|
| 120 | - $element->expectType($expectedTag); |
|
| 121 | - } |
|
| 122 | - return new UnspecifiedType($element); |
|
| 123 | - } |
|
| 109 | + /** |
|
| 110 | + * |
|
| 111 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 112 | + * @return UnspecifiedType |
|
| 113 | + */ |
|
| 114 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 115 | + { |
|
| 116 | + $idx = $this->_offset; |
|
| 117 | + Length::expectFromDER($this->_data, $idx); |
|
| 118 | + $element = Element::fromDER($this->_data, $idx); |
|
| 119 | + if (isset($expectedTag)) { |
|
| 120 | + $element->expectType($expectedTag); |
|
| 121 | + } |
|
| 122 | + return new UnspecifiedType($element); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -14,51 +14,51 @@ |
||
| 14 | 14 | * is not changed. |
| 15 | 15 | */ |
| 16 | 16 | class ExplicitlyTaggedType extends ContextSpecificTaggedType implements |
| 17 | - ExplicitTagging |
|
| 17 | + ExplicitTagging |
|
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param int $tag Tag number |
|
| 23 | - * @param Element $element Wrapped element |
|
| 24 | - */ |
|
| 25 | - public function __construct($tag, Element $element) |
|
| 26 | - { |
|
| 27 | - $this->_typeTag = $tag; |
|
| 28 | - $this->_element = $element; |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param int $tag Tag number |
|
| 23 | + * @param Element $element Wrapped element |
|
| 24 | + */ |
|
| 25 | + public function __construct($tag, Element $element) |
|
| 26 | + { |
|
| 27 | + $this->_typeTag = $tag; |
|
| 28 | + $this->_element = $element; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * |
|
| 33 | - * @see \ASN1\Element::isConstructed() |
|
| 34 | - * @return bool |
|
| 35 | - */ |
|
| 36 | - public function isConstructed(): bool |
|
| 37 | - { |
|
| 38 | - return true; |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * |
|
| 33 | + * @see \ASN1\Element::isConstructed() |
|
| 34 | + * @return bool |
|
| 35 | + */ |
|
| 36 | + public function isConstructed(): bool |
|
| 37 | + { |
|
| 38 | + return true; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * |
|
| 43 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - protected function _encodedContentDER(): string |
|
| 47 | - { |
|
| 48 | - return $this->_element->toDER(); |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * |
|
| 43 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + protected function _encodedContentDER(): string |
|
| 47 | + { |
|
| 48 | + return $this->_element->toDER(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 55 | - * @return UnspecifiedType |
|
| 56 | - */ |
|
| 57 | - public function explicit($expectedTag = null): UnspecifiedType |
|
| 58 | - { |
|
| 59 | - if (isset($expectedTag)) { |
|
| 60 | - $this->_element->expectType($expectedTag); |
|
| 61 | - } |
|
| 62 | - return new UnspecifiedType($this->_element); |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
| 55 | + * @return UnspecifiedType |
|
| 56 | + */ |
|
| 57 | + public function explicit($expectedTag = null): UnspecifiedType |
|
| 58 | + { |
|
| 59 | + if (isset($expectedTag)) { |
|
| 60 | + $this->_element->expectType($expectedTag); |
|
| 61 | + } |
|
| 62 | + return new UnspecifiedType($this->_element); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -13,13 +13,13 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface ImplicitTagging extends ElementBase |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Get implicitly tagged wrapped element. |
|
| 18 | - * |
|
| 19 | - * @param int $tag Tag of the element |
|
| 20 | - * @param int $class Expected type class of the element |
|
| 21 | - * @throws \UnexpectedValueException If expectation fails |
|
| 22 | - * @return \ASN1\Type\UnspecifiedType |
|
| 23 | - */ |
|
| 24 | - public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
| 16 | + /** |
|
| 17 | + * Get implicitly tagged wrapped element. |
|
| 18 | + * |
|
| 19 | + * @param int $tag Tag of the element |
|
| 20 | + * @param int $class Expected type class of the element |
|
| 21 | + * @throws \UnexpectedValueException If expectation fails |
|
| 22 | + * @return \ASN1\Type\UnspecifiedType |
|
| 23 | + */ |
|
| 24 | + public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
| 25 | 25 | } |
@@ -16,56 +16,56 @@ |
||
| 16 | 16 | * decoding the data. |
| 17 | 17 | */ |
| 18 | 18 | class ImplicitlyTaggedType extends ContextSpecificTaggedType implements |
| 19 | - ImplicitTagging |
|
| 19 | + ImplicitTagging |
|
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param int $tag |
|
| 25 | - * @param Element $element |
|
| 26 | - */ |
|
| 27 | - public function __construct($tag, Element $element) |
|
| 28 | - { |
|
| 29 | - $this->_typeTag = $tag; |
|
| 30 | - $this->_element = $element; |
|
| 31 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param int $tag |
|
| 25 | + * @param Element $element |
|
| 26 | + */ |
|
| 27 | + public function __construct($tag, Element $element) |
|
| 28 | + { |
|
| 29 | + $this->_typeTag = $tag; |
|
| 30 | + $this->_element = $element; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * |
|
| 35 | - * @see \ASN1\Element::isConstructed() |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function isConstructed(): bool |
|
| 39 | - { |
|
| 40 | - // depends on the underlying type |
|
| 41 | - return $this->_element->isConstructed(); |
|
| 42 | - } |
|
| 33 | + /** |
|
| 34 | + * |
|
| 35 | + * @see \ASN1\Element::isConstructed() |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function isConstructed(): bool |
|
| 39 | + { |
|
| 40 | + // depends on the underlying type |
|
| 41 | + return $this->_element->isConstructed(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * |
|
| 46 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - protected function _encodedContentDER(): string |
|
| 50 | - { |
|
| 51 | - return $this->_element->_encodedContentDER(); |
|
| 52 | - } |
|
| 44 | + /** |
|
| 45 | + * |
|
| 46 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + protected function _encodedContentDER(): string |
|
| 50 | + { |
|
| 51 | + return $this->_element->_encodedContentDER(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * |
|
| 56 | - * {@inheritdoc} |
|
| 57 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 58 | - * @return UnspecifiedType |
|
| 59 | - */ |
|
| 60 | - public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 61 | - { |
|
| 62 | - $this->_element->expectType($tag); |
|
| 63 | - if ($this->_element->typeClass() != $class) { |
|
| 64 | - throw new \UnexpectedValueException( |
|
| 65 | - sprintf("Type class %s expected, got %s.", |
|
| 66 | - Identifier::classToName($class), |
|
| 67 | - Identifier::classToName($this->_element->typeClass()))); |
|
| 68 | - } |
|
| 69 | - return new UnspecifiedType($this->_element); |
|
| 70 | - } |
|
| 54 | + /** |
|
| 55 | + * |
|
| 56 | + * {@inheritdoc} |
|
| 57 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
| 58 | + * @return UnspecifiedType |
|
| 59 | + */ |
|
| 60 | + public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 61 | + { |
|
| 62 | + $this->_element->expectType($tag); |
|
| 63 | + if ($this->_element->typeClass() != $class) { |
|
| 64 | + throw new \UnexpectedValueException( |
|
| 65 | + sprintf("Type class %s expected, got %s.", |
|
| 66 | + Identifier::classToName($class), |
|
| 67 | + Identifier::classToName($this->_element->typeClass()))); |
|
| 68 | + } |
|
| 69 | + return new UnspecifiedType($this->_element); |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -11,87 +11,87 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class TimeType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * UTC timezone. |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - const TZ_UTC = "UTC"; |
|
| 14 | + /** |
|
| 15 | + * UTC timezone. |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + const TZ_UTC = "UTC"; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Date and time. |
|
| 23 | - * |
|
| 24 | - * @var \DateTimeImmutable $_dateTime |
|
| 25 | - */ |
|
| 26 | - protected $_dateTime; |
|
| 21 | + /** |
|
| 22 | + * Date and time. |
|
| 23 | + * |
|
| 24 | + * @var \DateTimeImmutable $_dateTime |
|
| 25 | + */ |
|
| 26 | + protected $_dateTime; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param \DateTimeImmutable $dt |
|
| 32 | - */ |
|
| 33 | - public function __construct(\DateTimeImmutable $dt) |
|
| 34 | - { |
|
| 35 | - $this->_dateTime = $dt; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param \DateTimeImmutable $dt |
|
| 32 | + */ |
|
| 33 | + public function __construct(\DateTimeImmutable $dt) |
|
| 34 | + { |
|
| 35 | + $this->_dateTime = $dt; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Initialize from datetime string. |
|
| 40 | - * |
|
| 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 | - } |
|
| 38 | + /** |
|
| 39 | + * Initialize from datetime string. |
|
| 40 | + * |
|
| 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($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($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 | } |