Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/AttributeCertificate/Attributes.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\AttributeCertificate;
6
7 use SpomkyLabs\Pki\X501\ASN1\Attribute;
8 use SpomkyLabs\Pki\X501\ASN1\AttributeType;
9 use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue;
10 use SpomkyLabs\Pki\X501\ASN1\Collection\SequenceOfAttributes;
11 use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\AccessIdentityAttributeValue;
12 use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\AuthenticationInfoAttributeValue;
13 use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\ChargingIdentityAttributeValue;
14 use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\GroupAttributeValue;
15 use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\RoleAttributeValue;
16
17 /**
18 * Implements *Attributes* ASN.1 type of *AttributeCertificateInfo*.
19 *
20 * @see https://tools.ietf.org/html/rfc5755#section-4.1
21 * @see https://tools.ietf.org/html/rfc5755#section-4.2.7
22 */
23 final class Attributes extends SequenceOfAttributes
24 {
25 /**
26 * Mapping from OID to attribute value class name.
27 *
28 * @internal
29 *
30 * @var array<string, string>
31 */
32 private const MAP_OID_TO_CLASS = [
33 AccessIdentityAttributeValue::OID => AccessIdentityAttributeValue::class,
34 AuthenticationInfoAttributeValue::OID => AuthenticationInfoAttributeValue::class,
35 ChargingIdentityAttributeValue::OID => ChargingIdentityAttributeValue::class,
36 GroupAttributeValue::OID => GroupAttributeValue::class,
37 AttributeType::OID_ROLE => RoleAttributeValue::class,
38 ];
39
40 /**
41 * Initialize from attribute values.
42 *
43 * @param AttributeValue ...$values List of attribute values
44 */
45 public static function fromAttributeValues(AttributeValue ...$values): static
46 {
47 return static::create(...array_map(static fn (AttributeValue $value) => $value->toAttribute(), $values));
48 }
49
50 /**
51 * Check whether 'Access Identity' attribute is present.
52 */
53 public function hasAccessIdentity(): bool
54 {
55 return $this->has(AccessIdentityAttributeValue::OID);
56 }
57
58 /**
59 * Get the first 'Access Identity' attribute value.
60 */
61 public function accessIdentity(): AccessIdentityAttributeValue
62 {
63 return $this->firstOf(AccessIdentityAttributeValue::OID)->first();
64 }
65
66 /**
67 * Check whether 'Service Authentication Information' attribute is present.
68 */
69 public function hasAuthenticationInformation(): bool
70 {
71 return $this->has(AuthenticationInfoAttributeValue::OID);
72 }
73
74 /**
75 * Get the first 'Service Authentication Information' attribute value.
76 */
77 public function authenticationInformation(): AuthenticationInfoAttributeValue
78 {
79 return $this->firstOf(AuthenticationInfoAttributeValue::OID)->first();
80 }
81
82 /**
83 * Check whether 'Charging Identity' attribute is present.
84 */
85 public function hasChargingIdentity(): bool
86 {
87 return $this->has(ChargingIdentityAttributeValue::OID);
88 }
89
90 /**
91 * Get the first 'Charging Identity' attribute value.
92 */
93 public function chargingIdentity(): ChargingIdentityAttributeValue
94 {
95 return $this->firstOf(ChargingIdentityAttributeValue::OID)->first();
96 }
97
98 /**
99 * Check whether 'Group' attribute is present.
100 */
101 public function hasGroup(): bool
102 {
103 return $this->has(GroupAttributeValue::OID);
104 }
105
106 /**
107 * Get the first 'Group' attribute value.
108 */
109 public function group(): GroupAttributeValue
110 {
111 return $this->firstOf(GroupAttributeValue::OID)->first();
112 }
113
114 /**
115 * Check whether 'Role' attribute is present.
116 */
117 public function hasRole(): bool
118 {
119 return $this->has(AttributeType::OID_ROLE);
120 }
121
122 /**
123 * Get the first 'Role' attribute value.
124 */
125 public function role(): RoleAttributeValue
126 {
127 return $this->firstOf(AttributeType::OID_ROLE)->first();
128 }
129
130 /**
131 * Get all 'Role' attribute values.
132 *
133 * @return RoleAttributeValue[]
134 */
135 public function roles(): array
136 {
137 return array_merge(
138 [],
139 ...array_map(static fn (Attribute $attr) => $attr->values(), $this->allOf(AttributeType::OID_ROLE))
140 );
141 }
142
143 protected static function _castAttributeValues(Attribute $attribute): Attribute
144 {
145 $oid = $attribute->oid();
146 if (isset(self::MAP_OID_TO_CLASS[$oid])) {
147 return $attribute->castValues(self::MAP_OID_TO_CLASS[$oid]);
148 }
149 return $attribute;
150 }
151 }