Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/ASN1/RDN.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 SpomkyLabs\Pki\ASN1\Type\Constructed\Set;
12 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
13 use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue;
14 use Stringable;
15 use UnexpectedValueException;
16
17 /**
18 * Implements *RelativeDistinguishedName* ASN.1 type.
19 *
20 * @see https://www.itu.int/ITU-T/formal-language/itu-t/x/x501/2012/InformationFramework.html#InformationFramework.RelativeDistinguishedName
21 */
22 final class RDN implements Countable, IteratorAggregate, Stringable
23 {
24 /**
25 * Attributes.
26 *
27 * @var AttributeTypeAndValue[]
28 */
29 private readonly array $_attribs;
30
31 /**
32 * @param AttributeTypeAndValue ...$attribs One or more attributes
33 */
34 private function __construct(AttributeTypeAndValue ...$attribs)
35 {
36 if (count($attribs) === 0) {
37 throw new UnexpectedValueException('RDN must have at least one AttributeTypeAndValue.');
38 }
39 $this->_attribs = $attribs;
40 }
41
42 public function __toString(): string
43 {
44 return $this->toString();
45 }
46
47 public static function create(AttributeTypeAndValue ...$attribs): self
48 {
49 return new self(...$attribs);
50 }
51
52 /**
53 * Convenience method to initialize RDN from AttributeValue objects.
54 *
55 * @param AttributeValue ...$values One or more attributes
56 */
57 public static function fromAttributeValues(AttributeValue ...$values): self
58 {
59 $attribs = array_map(
60 static fn (AttributeValue $value) => AttributeTypeAndValue::create(AttributeType::create(
61 $value->oid()
62 ), $value),
63 $values
64 );
65 return self::create(...$attribs);
66 }
67
68 /**
69 * Initialize from ASN.1.
70 */
71 public static function fromASN1(Set $set): self
72 {
73 $attribs = array_map(
74 static fn (UnspecifiedType $el) => AttributeTypeAndValue::fromASN1($el->asSequence()),
75 $set->elements()
76 );
77 return self::create(...$attribs);
78 }
79
80 /**
81 * Generate ASN.1 structure.
82 */
83 public function toASN1(): Set
84 {
85 $elements = array_map(static fn (AttributeTypeAndValue $tv) => $tv->toASN1(), $this->_attribs);
86 return Set::create(...$elements)->sortedSetOf();
87 }
88
89 /**
90 * Get name-component string conforming to RFC 2253.
91 *
92 * @see https://tools.ietf.org/html/rfc2253#section-2.2
93 */
94 public function toString(): string
95 {
96 $parts = array_map(static fn (AttributeTypeAndValue $tv) => $tv->toString(), $this->_attribs);
97 return implode('+', $parts);
98 }
99
100 /**
101 * Check whether RDN is semantically equal to other.
102 *
103 * @param RDN $other Object to compare to
104 */
105 public function equals(self $other): bool
106 {
107 // if attribute count doesn't match
108 if (count($this) !== count($other)) {
109 return false;
110 }
111 $attribs1 = $this->_attribs;
112 $attribs2 = $other->_attribs;
113 // if there's multiple attributes, sort using SET OF rules
114 if (count($attribs1) > 1) {
115 $attribs1 = self::fromASN1($this->toASN1())->_attribs;
116 $attribs2 = self::fromASN1($other->toASN1())->_attribs;
117 }
118 for ($i = count($attribs1) - 1; $i >= 0; --$i) {
119 $tv1 = $attribs1[$i];
120 $tv2 = $attribs2[$i];
121 if (! $tv1->equals($tv2)) {
122 return false;
123 }
124 }
125 return true;
126 }
127
128 /**
129 * Get all AttributeTypeAndValue objects.
130 *
131 * @return AttributeTypeAndValue[]
132 */
133 public function all(): array
134 {
135 return $this->_attribs;
136 }
137
138 /**
139 * Get all AttributeTypeAndValue objects of the given attribute type.
140 *
141 * @param string $name Attribute OID or name
142 *
143 * @return AttributeTypeAndValue[]
144 */
145 public function allOf(string $name): array
146 {
147 $oid = AttributeType::attrNameToOID($name);
148 $attribs = array_filter($this->_attribs, static fn (AttributeTypeAndValue $tv) => $tv->oid() === $oid);
149 return array_values($attribs);
150 }
151
152 /**
153 * @see \Countable::count()
154 */
155 public function count(): int
156 {
157 return count($this->_attribs);
158 }
159
160 /**
161 * @see \IteratorAggregate::getIterator()
162 */
163 public function getIterator(): ArrayIterator
164 {
165 return new ArrayIterator($this->_attribs);
166 }
167 }