@@ -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 | } |
@@ -14,336 +14,336 @@ |
||
| 14 | 14 | * Base class for the constructed types. |
| 15 | 15 | */ |
| 16 | 16 | abstract class Structure extends Element implements |
| 17 | - \Countable, |
|
| 18 | - \IteratorAggregate |
|
| 17 | + \Countable, |
|
| 18 | + \IteratorAggregate |
|
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 20 | + use UniversalClass; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Array of elements in the structure. |
|
| 24 | - * |
|
| 25 | - * @var Element[] $_elements |
|
| 26 | - */ |
|
| 27 | - protected $_elements; |
|
| 22 | + /** |
|
| 23 | + * Array of elements in the structure. |
|
| 24 | + * |
|
| 25 | + * @var Element[] $_elements |
|
| 26 | + */ |
|
| 27 | + protected $_elements; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Lookup table for the tagged elements. |
|
| 31 | - * |
|
| 32 | - * @var TaggedType[]|null $_taggedMap |
|
| 33 | - */ |
|
| 34 | - private $_taggedMap; |
|
| 29 | + /** |
|
| 30 | + * Lookup table for the tagged elements. |
|
| 31 | + * |
|
| 32 | + * @var TaggedType[]|null $_taggedMap |
|
| 33 | + */ |
|
| 34 | + private $_taggedMap; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 38 | - * |
|
| 39 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 40 | - */ |
|
| 41 | - private $_unspecifiedTypes; |
|
| 36 | + /** |
|
| 37 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 38 | + * |
|
| 39 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 40 | + */ |
|
| 41 | + private $_unspecifiedTypes; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param Element ...$elements Any number of elements |
|
| 47 | - */ |
|
| 48 | - public function __construct(Element ...$elements) |
|
| 49 | - { |
|
| 50 | - $this->_elements = $elements; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param Element ...$elements Any number of elements |
|
| 47 | + */ |
|
| 48 | + public function __construct(Element ...$elements) |
|
| 49 | + { |
|
| 50 | + $this->_elements = $elements; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Clone magic method. |
|
| 55 | - */ |
|
| 56 | - public function __clone() |
|
| 57 | - { |
|
| 58 | - // clear cache-variables |
|
| 59 | - $this->_taggedMap = null; |
|
| 60 | - $this->_unspecifiedTypes = null; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Clone magic method. |
|
| 55 | + */ |
|
| 56 | + public function __clone() |
|
| 57 | + { |
|
| 58 | + // clear cache-variables |
|
| 59 | + $this->_taggedMap = null; |
|
| 60 | + $this->_unspecifiedTypes = null; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * |
|
| 65 | - * @see \ASN1\Element::isConstructed() |
|
| 66 | - * @return bool |
|
| 67 | - */ |
|
| 68 | - public function isConstructed(): bool |
|
| 69 | - { |
|
| 70 | - return true; |
|
| 71 | - } |
|
| 63 | + /** |
|
| 64 | + * |
|
| 65 | + * @see \ASN1\Element::isConstructed() |
|
| 66 | + * @return bool |
|
| 67 | + */ |
|
| 68 | + public function isConstructed(): bool |
|
| 69 | + { |
|
| 70 | + return true; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * |
|
| 75 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - protected function _encodedContentDER(): string |
|
| 79 | - { |
|
| 80 | - $data = ""; |
|
| 81 | - foreach ($this->_elements as $element) { |
|
| 82 | - $data .= $element->toDER(); |
|
| 83 | - } |
|
| 84 | - return $data; |
|
| 85 | - } |
|
| 73 | + /** |
|
| 74 | + * |
|
| 75 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + protected function _encodedContentDER(): string |
|
| 79 | + { |
|
| 80 | + $data = ""; |
|
| 81 | + foreach ($this->_elements as $element) { |
|
| 82 | + $data .= $element->toDER(); |
|
| 83 | + } |
|
| 84 | + return $data; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * |
|
| 89 | - * {@inheritdoc} |
|
| 90 | - * @see \ASN1\Element::_decodeFromDER() |
|
| 91 | - * @return self |
|
| 92 | - */ |
|
| 93 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 94 | - int &$offset): ElementBase |
|
| 95 | - { |
|
| 96 | - $idx = $offset; |
|
| 97 | - if (!$identifier->isConstructed()) { |
|
| 98 | - throw new DecodeException( |
|
| 99 | - "Structured element must have constructed bit set."); |
|
| 100 | - } |
|
| 101 | - $length = Length::expectFromDER($data, $idx); |
|
| 102 | - $end = $idx + $length->length(); |
|
| 103 | - $elements = []; |
|
| 104 | - while ($idx < $end) { |
|
| 105 | - $elements[] = Element::fromDER($data, $idx); |
|
| 106 | - // check that element didn't overflow length |
|
| 107 | - if ($idx > $end) { |
|
| 108 | - throw new DecodeException( |
|
| 109 | - "Structure's content overflows length."); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - $offset = $idx; |
|
| 113 | - // return instance by static late binding |
|
| 114 | - return new static(...$elements); |
|
| 115 | - } |
|
| 87 | + /** |
|
| 88 | + * |
|
| 89 | + * {@inheritdoc} |
|
| 90 | + * @see \ASN1\Element::_decodeFromDER() |
|
| 91 | + * @return self |
|
| 92 | + */ |
|
| 93 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 94 | + int &$offset): ElementBase |
|
| 95 | + { |
|
| 96 | + $idx = $offset; |
|
| 97 | + if (!$identifier->isConstructed()) { |
|
| 98 | + throw new DecodeException( |
|
| 99 | + "Structured element must have constructed bit set."); |
|
| 100 | + } |
|
| 101 | + $length = Length::expectFromDER($data, $idx); |
|
| 102 | + $end = $idx + $length->length(); |
|
| 103 | + $elements = []; |
|
| 104 | + while ($idx < $end) { |
|
| 105 | + $elements[] = Element::fromDER($data, $idx); |
|
| 106 | + // check that element didn't overflow length |
|
| 107 | + if ($idx > $end) { |
|
| 108 | + throw new DecodeException( |
|
| 109 | + "Structure's content overflows length."); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + $offset = $idx; |
|
| 113 | + // return instance by static late binding |
|
| 114 | + return new static(...$elements); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Explode DER structure to DER encoded components that it contains. |
|
| 119 | - * |
|
| 120 | - * @param string $data |
|
| 121 | - * @throws DecodeException |
|
| 122 | - * @return string[] |
|
| 123 | - */ |
|
| 124 | - public static function explodeDER(string $data): array |
|
| 125 | - { |
|
| 126 | - $offset = 0; |
|
| 127 | - $identifier = Identifier::fromDER($data, $offset); |
|
| 128 | - if (!$identifier->isConstructed()) { |
|
| 129 | - throw new DecodeException("Element is not constructed."); |
|
| 130 | - } |
|
| 131 | - $length = Length::expectFromDER($data, $offset); |
|
| 132 | - $end = $offset + $length->length(); |
|
| 133 | - $parts = []; |
|
| 134 | - while ($offset < $end) { |
|
| 135 | - // start of the element |
|
| 136 | - $idx = $offset; |
|
| 137 | - // skip identifier |
|
| 138 | - Identifier::fromDER($data, $offset); |
|
| 139 | - // decode element length |
|
| 140 | - $length = Length::expectFromDER($data, $offset); |
|
| 141 | - // extract der encoding of the element |
|
| 142 | - $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 143 | - // update offset over content |
|
| 144 | - $offset += $length->length(); |
|
| 145 | - } |
|
| 146 | - return $parts; |
|
| 147 | - } |
|
| 117 | + /** |
|
| 118 | + * Explode DER structure to DER encoded components that it contains. |
|
| 119 | + * |
|
| 120 | + * @param string $data |
|
| 121 | + * @throws DecodeException |
|
| 122 | + * @return string[] |
|
| 123 | + */ |
|
| 124 | + public static function explodeDER(string $data): array |
|
| 125 | + { |
|
| 126 | + $offset = 0; |
|
| 127 | + $identifier = Identifier::fromDER($data, $offset); |
|
| 128 | + if (!$identifier->isConstructed()) { |
|
| 129 | + throw new DecodeException("Element is not constructed."); |
|
| 130 | + } |
|
| 131 | + $length = Length::expectFromDER($data, $offset); |
|
| 132 | + $end = $offset + $length->length(); |
|
| 133 | + $parts = []; |
|
| 134 | + while ($offset < $end) { |
|
| 135 | + // start of the element |
|
| 136 | + $idx = $offset; |
|
| 137 | + // skip identifier |
|
| 138 | + Identifier::fromDER($data, $offset); |
|
| 139 | + // decode element length |
|
| 140 | + $length = Length::expectFromDER($data, $offset); |
|
| 141 | + // extract der encoding of the element |
|
| 142 | + $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 143 | + // update offset over content |
|
| 144 | + $offset += $length->length(); |
|
| 145 | + } |
|
| 146 | + return $parts; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Get self with an element at the given index replaced by another. |
|
| 151 | - * |
|
| 152 | - * @param int $idx Element index |
|
| 153 | - * @param Element $el New element to insert into the structure |
|
| 154 | - * @throws \OutOfBoundsException |
|
| 155 | - * @return self |
|
| 156 | - */ |
|
| 157 | - public function withReplaced(int $idx, Element $el): self |
|
| 158 | - { |
|
| 159 | - if (!isset($this->_elements[$idx])) { |
|
| 160 | - throw new \OutOfBoundsException( |
|
| 161 | - "Structure doesn't have element at index $idx."); |
|
| 162 | - } |
|
| 163 | - $obj = clone $this; |
|
| 164 | - $obj->_elements[$idx] = $el; |
|
| 165 | - return $obj; |
|
| 166 | - } |
|
| 149 | + /** |
|
| 150 | + * Get self with an element at the given index replaced by another. |
|
| 151 | + * |
|
| 152 | + * @param int $idx Element index |
|
| 153 | + * @param Element $el New element to insert into the structure |
|
| 154 | + * @throws \OutOfBoundsException |
|
| 155 | + * @return self |
|
| 156 | + */ |
|
| 157 | + public function withReplaced(int $idx, Element $el): self |
|
| 158 | + { |
|
| 159 | + if (!isset($this->_elements[$idx])) { |
|
| 160 | + throw new \OutOfBoundsException( |
|
| 161 | + "Structure doesn't have element at index $idx."); |
|
| 162 | + } |
|
| 163 | + $obj = clone $this; |
|
| 164 | + $obj->_elements[$idx] = $el; |
|
| 165 | + return $obj; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Get self with an element inserted before the given index. |
|
| 170 | - * |
|
| 171 | - * @param int $idx Element index |
|
| 172 | - * @param Element $el New element to insert into the structure |
|
| 173 | - * @throws \OutOfBoundsException |
|
| 174 | - * @return self |
|
| 175 | - */ |
|
| 176 | - public function withInserted(int $idx, Element $el): self |
|
| 177 | - { |
|
| 178 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
| 179 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 180 | - } |
|
| 181 | - $obj = clone $this; |
|
| 182 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 183 | - return $obj; |
|
| 184 | - } |
|
| 168 | + /** |
|
| 169 | + * Get self with an element inserted before the given index. |
|
| 170 | + * |
|
| 171 | + * @param int $idx Element index |
|
| 172 | + * @param Element $el New element to insert into the structure |
|
| 173 | + * @throws \OutOfBoundsException |
|
| 174 | + * @return self |
|
| 175 | + */ |
|
| 176 | + public function withInserted(int $idx, Element $el): self |
|
| 177 | + { |
|
| 178 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
| 179 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 180 | + } |
|
| 181 | + $obj = clone $this; |
|
| 182 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 183 | + return $obj; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * Get self with an element appended to the end. |
|
| 188 | - * |
|
| 189 | - * @param Element $el Element to insert into the structure |
|
| 190 | - * @return self |
|
| 191 | - */ |
|
| 192 | - public function withAppended(Element $el): self |
|
| 193 | - { |
|
| 194 | - $obj = clone $this; |
|
| 195 | - array_push($obj->_elements, $el); |
|
| 196 | - return $obj; |
|
| 197 | - } |
|
| 186 | + /** |
|
| 187 | + * Get self with an element appended to the end. |
|
| 188 | + * |
|
| 189 | + * @param Element $el Element to insert into the structure |
|
| 190 | + * @return self |
|
| 191 | + */ |
|
| 192 | + public function withAppended(Element $el): self |
|
| 193 | + { |
|
| 194 | + $obj = clone $this; |
|
| 195 | + array_push($obj->_elements, $el); |
|
| 196 | + return $obj; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Get self with an element prepended in the beginning. |
|
| 201 | - * |
|
| 202 | - * @param Element $el Element to insert into the structure |
|
| 203 | - * @return self |
|
| 204 | - */ |
|
| 205 | - public function withPrepended(Element $el): self |
|
| 206 | - { |
|
| 207 | - $obj = clone $this; |
|
| 208 | - array_unshift($obj->_elements, $el); |
|
| 209 | - return $obj; |
|
| 210 | - } |
|
| 199 | + /** |
|
| 200 | + * Get self with an element prepended in the beginning. |
|
| 201 | + * |
|
| 202 | + * @param Element $el Element to insert into the structure |
|
| 203 | + * @return self |
|
| 204 | + */ |
|
| 205 | + public function withPrepended(Element $el): self |
|
| 206 | + { |
|
| 207 | + $obj = clone $this; |
|
| 208 | + array_unshift($obj->_elements, $el); |
|
| 209 | + return $obj; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Get self with an element at the given index removed. |
|
| 214 | - * |
|
| 215 | - * @param int $idx Element index |
|
| 216 | - * @throws \OutOfBoundsException |
|
| 217 | - * @return self |
|
| 218 | - */ |
|
| 219 | - public function withoutElement($idx): self |
|
| 220 | - { |
|
| 221 | - if (!isset($this->_elements[$idx])) { |
|
| 222 | - throw new \OutOfBoundsException( |
|
| 223 | - "Structure doesn't have element at index $idx."); |
|
| 224 | - } |
|
| 225 | - $obj = clone $this; |
|
| 226 | - array_splice($obj->_elements, $idx, 1); |
|
| 227 | - return $obj; |
|
| 228 | - } |
|
| 212 | + /** |
|
| 213 | + * Get self with an element at the given index removed. |
|
| 214 | + * |
|
| 215 | + * @param int $idx Element index |
|
| 216 | + * @throws \OutOfBoundsException |
|
| 217 | + * @return self |
|
| 218 | + */ |
|
| 219 | + public function withoutElement($idx): self |
|
| 220 | + { |
|
| 221 | + if (!isset($this->_elements[$idx])) { |
|
| 222 | + throw new \OutOfBoundsException( |
|
| 223 | + "Structure doesn't have element at index $idx."); |
|
| 224 | + } |
|
| 225 | + $obj = clone $this; |
|
| 226 | + array_splice($obj->_elements, $idx, 1); |
|
| 227 | + return $obj; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Get elements in the structure. |
|
| 232 | - * |
|
| 233 | - * @return UnspecifiedType[] |
|
| 234 | - */ |
|
| 235 | - public function elements(): array |
|
| 236 | - { |
|
| 237 | - if (!isset($this->_unspecifiedTypes)) { |
|
| 238 | - $this->_unspecifiedTypes = array_map( |
|
| 239 | - function (Element $el) { |
|
| 240 | - return new UnspecifiedType($el); |
|
| 241 | - }, $this->_elements); |
|
| 242 | - } |
|
| 243 | - return $this->_unspecifiedTypes; |
|
| 244 | - } |
|
| 230 | + /** |
|
| 231 | + * Get elements in the structure. |
|
| 232 | + * |
|
| 233 | + * @return UnspecifiedType[] |
|
| 234 | + */ |
|
| 235 | + public function elements(): array |
|
| 236 | + { |
|
| 237 | + if (!isset($this->_unspecifiedTypes)) { |
|
| 238 | + $this->_unspecifiedTypes = array_map( |
|
| 239 | + function (Element $el) { |
|
| 240 | + return new UnspecifiedType($el); |
|
| 241 | + }, $this->_elements); |
|
| 242 | + } |
|
| 243 | + return $this->_unspecifiedTypes; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * Check whether the structure has an element at the given index, optionally |
|
| 248 | - * satisfying given tag expectation. |
|
| 249 | - * |
|
| 250 | - * @param int $idx Index 0..n |
|
| 251 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 252 | - * @return bool |
|
| 253 | - */ |
|
| 254 | - public function has(int $idx, $expectedTag = null): bool |
|
| 255 | - { |
|
| 256 | - if (!isset($this->_elements[$idx])) { |
|
| 257 | - return false; |
|
| 258 | - } |
|
| 259 | - if (isset($expectedTag)) { |
|
| 260 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 261 | - return false; |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - return true; |
|
| 265 | - } |
|
| 246 | + /** |
|
| 247 | + * Check whether the structure has an element at the given index, optionally |
|
| 248 | + * satisfying given tag expectation. |
|
| 249 | + * |
|
| 250 | + * @param int $idx Index 0..n |
|
| 251 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 252 | + * @return bool |
|
| 253 | + */ |
|
| 254 | + public function has(int $idx, $expectedTag = null): bool |
|
| 255 | + { |
|
| 256 | + if (!isset($this->_elements[$idx])) { |
|
| 257 | + return false; |
|
| 258 | + } |
|
| 259 | + if (isset($expectedTag)) { |
|
| 260 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 261 | + return false; |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + return true; |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * Get the element at the given index, optionally checking that the element |
|
| 269 | - * has a given tag. |
|
| 270 | - * |
|
| 271 | - * NOTE! Expectation checking is deprecated and should be done |
|
| 272 | - * with UnspecifiedType. |
|
| 273 | - * |
|
| 274 | - * @param int $idx Index 0..n |
|
| 275 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 276 | - * @throws \OutOfBoundsException If element doesn't exists |
|
| 277 | - * @throws \UnexpectedValueException If expectation fails |
|
| 278 | - * @return UnspecifiedType |
|
| 279 | - */ |
|
| 280 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
| 281 | - { |
|
| 282 | - if (!isset($this->_elements[$idx])) { |
|
| 283 | - throw new \OutOfBoundsException( |
|
| 284 | - "Structure doesn't have an element at index $idx."); |
|
| 285 | - } |
|
| 286 | - $element = $this->_elements[$idx]; |
|
| 287 | - if (isset($expectedTag)) { |
|
| 288 | - $element->expectType($expectedTag); |
|
| 289 | - } |
|
| 290 | - return new UnspecifiedType($element); |
|
| 291 | - } |
|
| 267 | + /** |
|
| 268 | + * Get the element at the given index, optionally checking that the element |
|
| 269 | + * has a given tag. |
|
| 270 | + * |
|
| 271 | + * NOTE! Expectation checking is deprecated and should be done |
|
| 272 | + * with UnspecifiedType. |
|
| 273 | + * |
|
| 274 | + * @param int $idx Index 0..n |
|
| 275 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 276 | + * @throws \OutOfBoundsException If element doesn't exists |
|
| 277 | + * @throws \UnexpectedValueException If expectation fails |
|
| 278 | + * @return UnspecifiedType |
|
| 279 | + */ |
|
| 280 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
| 281 | + { |
|
| 282 | + if (!isset($this->_elements[$idx])) { |
|
| 283 | + throw new \OutOfBoundsException( |
|
| 284 | + "Structure doesn't have an element at index $idx."); |
|
| 285 | + } |
|
| 286 | + $element = $this->_elements[$idx]; |
|
| 287 | + if (isset($expectedTag)) { |
|
| 288 | + $element->expectType($expectedTag); |
|
| 289 | + } |
|
| 290 | + return new UnspecifiedType($element); |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * Check whether the structure contains a context specific element with a |
|
| 295 | - * given tag. |
|
| 296 | - * |
|
| 297 | - * @param int $tag Tag number |
|
| 298 | - * @return boolean |
|
| 299 | - */ |
|
| 300 | - public function hasTagged($tag): bool |
|
| 301 | - { |
|
| 302 | - // lazily build lookup map |
|
| 303 | - if (!isset($this->_taggedMap)) { |
|
| 304 | - $this->_taggedMap = []; |
|
| 305 | - foreach ($this->_elements as $element) { |
|
| 306 | - if ($element->isTagged()) { |
|
| 307 | - $this->_taggedMap[$element->tag()] = $element; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - return isset($this->_taggedMap[$tag]); |
|
| 312 | - } |
|
| 293 | + /** |
|
| 294 | + * Check whether the structure contains a context specific element with a |
|
| 295 | + * given tag. |
|
| 296 | + * |
|
| 297 | + * @param int $tag Tag number |
|
| 298 | + * @return boolean |
|
| 299 | + */ |
|
| 300 | + public function hasTagged($tag): bool |
|
| 301 | + { |
|
| 302 | + // lazily build lookup map |
|
| 303 | + if (!isset($this->_taggedMap)) { |
|
| 304 | + $this->_taggedMap = []; |
|
| 305 | + foreach ($this->_elements as $element) { |
|
| 306 | + if ($element->isTagged()) { |
|
| 307 | + $this->_taggedMap[$element->tag()] = $element; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + return isset($this->_taggedMap[$tag]); |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | - /** |
|
| 315 | - * Get a context specific element tagged with a given tag. |
|
| 316 | - * |
|
| 317 | - * @param int $tag |
|
| 318 | - * @throws \LogicException If tag doesn't exists |
|
| 319 | - * @return TaggedType |
|
| 320 | - */ |
|
| 321 | - public function getTagged($tag): TaggedType |
|
| 322 | - { |
|
| 323 | - if (!$this->hasTagged($tag)) { |
|
| 324 | - throw new \LogicException("No tagged element for tag $tag."); |
|
| 325 | - } |
|
| 326 | - return $this->_taggedMap[$tag]; |
|
| 327 | - } |
|
| 314 | + /** |
|
| 315 | + * Get a context specific element tagged with a given tag. |
|
| 316 | + * |
|
| 317 | + * @param int $tag |
|
| 318 | + * @throws \LogicException If tag doesn't exists |
|
| 319 | + * @return TaggedType |
|
| 320 | + */ |
|
| 321 | + public function getTagged($tag): TaggedType |
|
| 322 | + { |
|
| 323 | + if (!$this->hasTagged($tag)) { |
|
| 324 | + throw new \LogicException("No tagged element for tag $tag."); |
|
| 325 | + } |
|
| 326 | + return $this->_taggedMap[$tag]; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - /** |
|
| 330 | - * |
|
| 331 | - * @see \Countable::count() |
|
| 332 | - * @return int |
|
| 333 | - */ |
|
| 334 | - public function count(): int |
|
| 335 | - { |
|
| 336 | - return count($this->_elements); |
|
| 337 | - } |
|
| 329 | + /** |
|
| 330 | + * |
|
| 331 | + * @see \Countable::count() |
|
| 332 | + * @return int |
|
| 333 | + */ |
|
| 334 | + public function count(): int |
|
| 335 | + { |
|
| 336 | + return count($this->_elements); |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Get an iterator for the UnspecifiedElement objects. |
|
| 341 | - * |
|
| 342 | - * @see \IteratorAggregate::getIterator() |
|
| 343 | - * @return \ArrayIterator |
|
| 344 | - */ |
|
| 345 | - public function getIterator(): \ArrayIterator |
|
| 346 | - { |
|
| 347 | - return new \ArrayIterator($this->elements()); |
|
| 348 | - } |
|
| 339 | + /** |
|
| 340 | + * Get an iterator for the UnspecifiedElement objects. |
|
| 341 | + * |
|
| 342 | + * @see \IteratorAggregate::getIterator() |
|
| 343 | + * @return \ArrayIterator |
|
| 344 | + */ |
|
| 345 | + public function getIterator(): \ArrayIterator |
|
| 346 | + { |
|
| 347 | + return new \ArrayIterator($this->elements()); |
|
| 348 | + } |
|
| 349 | 349 | } |