Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X501\ASN1\AttributeValue;
6
7 use function array_key_exists;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
10 use SpomkyLabs\Pki\X501\ASN1\Attribute;
11 use SpomkyLabs\Pki\X501\ASN1\AttributeType;
12 use SpomkyLabs\Pki\X501\ASN1\AttributeTypeAndValue;
13 use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule;
14 use Stringable;
15
16 /**
17 * Base class for attribute values.
18 *
19 * @see https://www.itu.int/ITU-T/formal-language/itu-t/x/x501/2012/InformationFramework.html#InformationFramework.AttributeValue
20 */
21 abstract class AttributeValue implements Stringable
22 {
23 /**
24 * Mapping from attribute type OID to attribute value class name.
25 *
26 * @internal
27 *
28 * @var array<string, string>
29 */
30 private const MAP_OID_TO_CLASS = [
31 AttributeType::OID_COMMON_NAME => CommonNameValue::class,
32 AttributeType::OID_SURNAME => SurnameValue::class,
33 AttributeType::OID_SERIAL_NUMBER => SerialNumberValue::class,
34 AttributeType::OID_COUNTRY_NAME => CountryNameValue::class,
35 AttributeType::OID_LOCALITY_NAME => LocalityNameValue::class,
36 AttributeType::OID_STATE_OR_PROVINCE_NAME => StateOrProvinceNameValue::class,
37 AttributeType::OID_ORGANIZATION_NAME => OrganizationNameValue::class,
38 AttributeType::OID_ORGANIZATIONAL_UNIT_NAME => OrganizationalUnitNameValue::class,
39 AttributeType::OID_TITLE => TitleValue::class,
40 AttributeType::OID_DESCRIPTION => DescriptionValue::class,
41 AttributeType::OID_NAME => NameValue::class,
42 AttributeType::OID_GIVEN_NAME => GivenNameValue::class,
43 AttributeType::OID_PSEUDONYM => PseudonymValue::class,
44 ];
45
46 /**
47 * @param string $oid OID of the attribute type.
48 */
49 protected function __construct(
50 protected string $oid
51 ) {
52 }
53
54 /**
55 * Get attribute value as an UTF-8 encoded string.
56 */
57 public function __toString(): string
58 {
59 return $this->_transcodedString();
60 }
61
62 /**
63 * Generate ASN.1 element.
64 */
65 abstract public function toASN1(): Element;
66
67 /**
68 * Get attribute value as a string.
69 */
70 abstract public function stringValue(): string;
71
72 /**
73 * Get matching rule for equality comparison.
74 */
75 abstract public function equalityMatchingRule(): MatchingRule;
76
77 /**
78 * Get attribute value as a string conforming to RFC 2253.
79 *
80 * @see https://tools.ietf.org/html/rfc2253#section-2.4
81 */
82 abstract public function rfc2253String(): string;
83
84 /**
85 * Initialize from ASN.1.
86 */
87 abstract public static function fromASN1(UnspecifiedType $el): self;
88
89 /**
90 * Initialize from ASN.1 with given OID hint.
91 *
92 * @param string $oid Attribute's OID
93 */
94 public static function fromASN1ByOID(string $oid, UnspecifiedType $el): self
95 {
96 if (! array_key_exists($oid, self::MAP_OID_TO_CLASS)) {
97 return new UnknownAttributeValue($oid, $el->asElement());
98 }
99 $cls = self::MAP_OID_TO_CLASS[$oid];
100 return $cls::fromASN1($el);
101 }
102
103 /**
104 * Initialize from another AttributeValue.
105 *
106 * This method is generally used to cast UnknownAttributeValue to specific object when class is declared outside
107 * this package.
108 *
109 * @param self $obj Instance of AttributeValue
110 */
111 public static function fromSelf(self $obj): self
112 {
113 return static::fromASN1($obj->toASN1()->asUnspecified());
114 }
115
116 /**
117 * Get attribute type's OID.
118 */
119 public function oid(): string
120 {
121 return $this->oid;
122 }
123
124 /**
125 * Get Attribute object with this as a single value.
126 */
127 public function toAttribute(): Attribute
128 {
129 return Attribute::fromAttributeValues($this);
130 }
131
132 /**
133 * Get AttributeTypeAndValue object with this as a value.
134 */
135 public function toAttributeTypeAndValue(): AttributeTypeAndValue
136 {
137 return AttributeTypeAndValue::fromAttributeValue($this);
138 }
139
140 /**
141 * Get attribute value as an UTF-8 string conforming to RFC 4518.
142 *
143 * @see https://tools.ietf.org/html/rfc4518#section-2.1
144 */
145 abstract protected function _transcodedString(): string;
146 }