| 1 | <?php |
||
| 21 | */ |
||
| 22 | abstract class IetfAttrSyntax extends AttributeValue implements \Countable, \IteratorAggregate |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Policy authority. |
||
| 26 | * |
||
| 27 | * @var null|GeneralNames |
||
| 28 | */ |
||
| 29 | protected $_policyAuthority; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Values. |
||
| 33 | * |
||
| 34 | * @var IetfAttrValue[] |
||
| 35 | */ |
||
| 36 | protected $_values; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructor. |
||
| 40 | * |
||
| 41 | * @param IetfAttrValue ...$values |
||
| 42 | */ |
||
| 43 | 9 | public function __construct(IetfAttrValue ...$values) |
|
| 44 | { |
||
| 45 | 9 | $this->_policyAuthority = null; |
|
| 46 | 9 | $this->_values = $values; |
|
| 47 | 9 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | * |
||
| 52 | * @return self |
||
| 53 | */ |
||
| 54 | 4 | public static function fromASN1(UnspecifiedType $el): AttributeValue |
|
| 55 | { |
||
| 56 | 4 | $seq = $el->asSequence(); |
|
| 57 | 4 | $authority = null; |
|
| 58 | 4 | $idx = 0; |
|
| 59 | 4 | if ($seq->hasTagged(0)) { |
|
| 60 | 4 | $authority = GeneralNames::fromASN1( |
|
| 61 | 4 | $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 62 | 4 | ->asSequence()); |
|
| 63 | 4 | ++$idx; |
|
| 64 | } |
||
| 65 | 4 | $values = array_map( |
|
| 66 | function (UnspecifiedType $el) { |
||
| 67 | 4 | return IetfAttrValue::fromASN1($el); |
|
| 68 | 4 | }, $seq->at($idx)->asSequence()->elements()); |
|
| 69 | 4 | $obj = new static(...$values); |
|
| 70 | 4 | $obj->_policyAuthority = $authority; |
|
| 71 | 4 | return $obj; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get self with policy authority. |
||
| 76 | * |
||
| 77 | * @param GeneralNames $names |
||
| 78 | * |
||
| 79 | * @return self |
||
| 80 | */ |
||
| 81 | 2 | public function withPolicyAuthority(GeneralNames $names): self |
|
| 82 | { |
||
| 83 | 2 | $obj = clone $this; |
|
| 84 | 2 | $obj->_policyAuthority = $names; |
|
| 85 | 2 | return $obj; |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Check whether policy authority is present. |
||
| 90 | * |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | 3 | public function hasPolicyAuthority(): bool |
|
| 94 | { |
||
| 95 | 3 | return isset($this->_policyAuthority); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get policy authority. |
||
| 100 | * |
||
| 101 | * @throws \LogicException If not set |
||
| 102 | * |
||
| 103 | * @return GeneralNames |
||
| 104 | */ |
||
| 105 | 3 | public function policyAuthority(): GeneralNames |
|
| 106 | { |
||
| 107 | 3 | if (!$this->hasPolicyAuthority()) { |
|
| 108 | 1 | throw new \LogicException('policyAuthority not set.'); |
|
| 109 | } |
||
| 110 | 2 | return $this->_policyAuthority; |
|
|
1 ignored issue
–
show
|
|||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get values. |
||
| 115 | * |
||
| 116 | * @return IetfAttrValue[] |
||
| 117 | */ |
||
| 118 | 3 | public function values(): array |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get first value. |
||
| 125 | * |
||
| 126 | * @throws \LogicException If not set |
||
| 127 | * |
||
| 128 | * @return IetfAttrValue |
||
| 129 | */ |
||
| 130 | 2 | public function first(): IetfAttrValue |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | 8 | public function toASN1(): Element |
|
| 142 | { |
||
| 143 | 8 | $elements = []; |
|
| 144 | 8 | if (isset($this->_policyAuthority)) { |
|
| 145 | 5 | $elements[] = new ImplicitlyTaggedType( |
|
| 146 | 5 | 0, $this->_policyAuthority->toASN1()); |
|
|
1 ignored issue
–
show
|
|||
| 147 | } |
||
| 148 | 8 | $values = array_map( |
|
| 149 | function (IetfAttrValue $val) { |
||
| 150 | 5 | return $val->toASN1(); |
|
| 151 | 8 | }, $this->_values); |
|
| 152 | 8 | $elements[] = new Sequence(...$values); |
|
| 153 | 8 | return new Sequence(...$elements); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 3 | public function stringValue(): string |
|
| 160 | { |
||
| 161 | 3 | return '#' . bin2hex($this->toASN1()->toDER()); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 1 | public function equalityMatchingRule(): MatchingRule |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 1 | public function rfc2253String(): string |
|
| 176 | { |
||
| 177 | 1 | return $this->stringValue(); |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get number of values. |
||
| 182 | * |
||
| 183 | * @see \Countable::count() |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | 2 | public function count(): int |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get iterator for values. |
||
| 194 | * |
||
| 195 | * @see \IteratorAggregate::getIterator() |
||
| 196 | * |
||
| 197 | * @return \ArrayIterator |
||
| 198 | */ |
||
| 199 | 1 | public function getIterator(): \ArrayIterator |
|
| 200 | { |
||
| 201 | 1 | return new \ArrayIterator($this->_values); |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | 1 | protected function _transcodedString(): string |
|
| 210 | } |
||
| 211 | } |
||
| 212 |