Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/ASN1/Attribute.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X501\ASN1;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
13 use SpomkyLabs\Pki\ASN1\Type\Constructed\Set;
14 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
15 use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue;
16 use function sprintf;
17
18 /**
19 * Implements *Attribute* ASN.1 type.
20 *
21 * @see https://www.itu.int/ITU-T/formal-language/itu-t/x/x501/2012/InformationFramework.html#InformationFramework.Attribute
22 */
23 final class Attribute implements Countable, IteratorAggregate
24 {
25 /**
26 * Attribute type.
27 */
28 private readonly AttributeType $type;
29
30 /**
31 * Attribute values.
32 *
33 * @var AttributeValue[]
34 */
35 private readonly array $values;
36
37 /**
38 * @param AttributeType $type Attribute type
39 * @param AttributeValue ...$values Attribute values
40 */
41 private function __construct(AttributeType $type, AttributeValue ...$values)
42 {
43 // check that attribute values have correct oid
44 foreach ($values as $value) {
45 if ($value->oid() !== $type->oid()) {
46 throw new LogicException('Attribute OID mismatch.');
47 }
48 }
49 $this->type = $type;
50 $this->values = $values;
51 }
52
53 public static function create(AttributeType $type, AttributeValue ...$values): self
54 {
55 return new self($type, ...$values);
56 }
57
58 /**
59 * Initialize from ASN.1.
60 */
61 public static function fromASN1(Sequence $seq): self
62 {
63 $type = AttributeType::fromASN1($seq->at(0)->asObjectIdentifier());
64 $values = array_map(
65 static fn (UnspecifiedType $el) => AttributeValue::fromASN1ByOID($type->oid(), $el),
66 $seq->at(1)
67 ->asSet()
68 ->elements()
69 );
70 return self::create($type, ...$values);
71 }
72
73 /**
74 * Convenience method to initialize from attribute values.
75 *
76 * @param AttributeValue ...$values One or more values
77 */
78 public static function fromAttributeValues(AttributeValue ...$values): self
79 {
80 // we need at least one value to determine OID
81 if (count($values) === 0) {
82 throw new LogicException('No values.');
83 }
84 $oid = reset($values)
85 ->oid();
86 return self::create(AttributeType::create($oid), ...$values);
87 }
88
89 /**
90 * Get first value of the attribute.
91 */
92 public function first(): AttributeValue
93 {
94 if (count($this->values) === 0) {
95 throw new LogicException('Attribute contains no values.');
96 }
97 return $this->values[0];
98 }
99
100 /**
101 * Get all values.
102 *
103 * @return AttributeValue[]
104 */
105 public function values(): array
106 {
107 return $this->values;
108 }
109
110 /**
111 * Generate ASN.1 structure.
112 */
113 public function toASN1(): Sequence
114 {
115 $values = array_map(static fn (AttributeValue $value) => $value->toASN1(), $this->values);
116 $valueset = Set::create(...$values);
117 return Sequence::create($this->type->toASN1(), $valueset->sortedSetOf());
118 }
119
120 /**
121 * Cast attribute values to another AttributeValue class.
122 *
123 * This method is generally used to cast UnknownAttributeValue values to specific objects when class is declared
124 * outside this package.
125 *
126 * The new class must be derived from AttributeValue and have the same OID as current attribute values.
127 *
128 * @param string $cls AttributeValue class name
129 */
130 public function castValues(string $cls): self
131 {
132 // check that target class derives from AttributeValue
133 if (! is_subclass_of($cls, AttributeValue::class)) {
134 throw new LogicException(sprintf('%s must be derived from %s.', $cls, AttributeValue::class));
135 }
136 $values = array_map(
137 function (AttributeValue $value) use ($cls) {
138 /** @var AttributeValue $cls Class name as a string */
139 $value = $cls::fromSelf($value);
140 if ($value->oid() !== $this->oid()) {
141 throw new LogicException('Attribute OID mismatch.');
142 }
143 return $value;
144 },
145 $this->values
146 );
147 return self::fromAttributeValues(...$values);
148 }
149
150 /**
151 * @see \Countable::count()
152 */
153 public function count(): int
154 {
155 return count($this->values);
156 }
157
158 /**
159 * @see \IteratorAggregate::getIterator()
160 */
161 public function getIterator(): ArrayIterator
162 {
163 return new ArrayIterator($this->values);
164 }
165
166 /**
167 * Get attribute type.
168 */
169 public function type(): AttributeType
170 {
171 return $this->type;
172 }
173
174 /**
175 * Get OID of the attribute.
176 */
177 public function oid(): string
178 {
179 return $this->type->oid();
180 }
181 }