1 | <?php |
||
19 | { |
||
20 | use TypedAttribute; |
||
21 | |||
22 | /** |
||
23 | * Attribute values. |
||
24 | * |
||
25 | * @var AttributeValue[] |
||
26 | */ |
||
27 | protected $_values; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param AttributeType $type Attribute type |
||
33 | * @param AttributeValue ...$values Attribute values |
||
34 | */ |
||
35 | 6 | public function __construct(AttributeType $type, AttributeValue ...$values) |
|
36 | { |
||
37 | // check that attribute values have correct oid |
||
38 | 6 | array_walk($values, |
|
39 | function (AttributeValue $value) use ($type) { |
||
40 | 5 | if ($value->oid() != $type->oid()) { |
|
41 | 1 | throw new \LogicException('Attribute OID mismatch.'); |
|
42 | } |
||
43 | 6 | }); |
|
44 | 5 | $this->_type = $type; |
|
45 | 5 | $this->_values = $values; |
|
46 | 5 | } |
|
47 | |||
48 | /** |
||
49 | * Initialize from ASN.1. |
||
50 | * |
||
51 | * @param Sequence $seq |
||
52 | * |
||
53 | * @return self |
||
54 | */ |
||
55 | 1 | public static function fromASN1(Sequence $seq): self |
|
56 | { |
||
57 | 1 | $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier()); |
|
58 | 1 | $values = array_map( |
|
59 | function (UnspecifiedType $el) use ($type) { |
||
60 | 1 | return AttributeValue::fromASN1ByOID($type->oid(), $el); |
|
61 | 1 | }, $seq->at(1) |
|
62 | 1 | ->asSet() |
|
63 | 1 | ->elements()); |
|
64 | 1 | return new self($type, ...$values); |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Convenience method to initialize from attribute values. |
||
69 | * |
||
70 | * @param AttributeValue ...$values One or more values |
||
71 | * |
||
72 | * @throws \LogicException |
||
73 | * |
||
74 | * @return self |
||
75 | */ |
||
76 | 5 | public static function fromAttributeValues(AttributeValue ...$values): self |
|
77 | { |
||
78 | // we need at least one value to determine OID |
||
79 | 5 | if (!count($values)) { |
|
80 | 1 | throw new \LogicException('No values.'); |
|
81 | } |
||
82 | 4 | $oid = reset($values)->oid(); |
|
83 | 4 | return new self(new AttributeType($oid), ...$values); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get first value of the attribute. |
||
88 | * |
||
89 | * @throws \LogicException |
||
90 | * |
||
91 | * @return AttributeValue |
||
92 | */ |
||
93 | 3 | public function first(): AttributeValue |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get all values. |
||
103 | * |
||
104 | * @return AttributeValue[] |
||
105 | */ |
||
106 | 1 | public function values(): array |
|
107 | { |
||
108 | 1 | return $this->_values; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Generate ASN.1 structure. |
||
113 | * |
||
114 | * @return Sequence |
||
115 | */ |
||
116 | 1 | public function toASN1(): Sequence |
|
117 | { |
||
118 | 1 | $values = array_map( |
|
119 | function (AttributeValue $value) { |
||
120 | 1 | return $value->toASN1(); |
|
121 | 1 | }, $this->_values); |
|
122 | 1 | $valueset = new Set(...$values); |
|
123 | 1 | return new Sequence($this->_type->toASN1(), $valueset->sortedSetOf()); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Cast attribute values to another AttributeValue class. |
||
128 | * |
||
129 | * This method is generally used to cast UnknownAttributeValue values |
||
130 | * to specific objects when class is declared outside this package. |
||
131 | * |
||
132 | * The new class must be derived from AttributeValue and have the same OID |
||
133 | * as current attribute values. |
||
134 | * |
||
135 | * @param string $cls AttributeValue class name |
||
136 | * |
||
137 | * @throws \LogicException |
||
138 | * |
||
139 | * @return self |
||
140 | */ |
||
141 | 3 | public function castValues(string $cls): self |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * @see \Countable::count() |
||
162 | * |
||
163 | * @return int |
||
164 | */ |
||
165 | 1 | public function count(): int |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @see \IteratorAggregate::getIterator() |
||
172 | * |
||
173 | * @return \ArrayIterator |
||
174 | */ |
||
175 | 1 | public function getIterator(): \ArrayIterator |
|
180 |