Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationRequest;
6
7 use LogicException;
8 use SpomkyLabs\Pki\X501\ASN1\Attribute;
9 use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue;
10 use SpomkyLabs\Pki\X501\ASN1\Collection\SetOfAttributes;
11 use SpomkyLabs\Pki\X509\CertificationRequest\Attribute\ExtensionRequestValue;
12
13 /**
14 * Implements *Attributes* ASN.1 type of *CertificationRequestInfo*.
15 *
16 * @see https://tools.ietf.org/html/rfc2986#section-4
17 */
18 final class Attributes extends SetOfAttributes
19 {
20 /**
21 * Mapping from OID to attribute value class name.
22 *
23 * @internal
24 *
25 * @var array<string, string>
26 */
27 private const MAP_OID_TO_CLASS = [
28 ExtensionRequestValue::OID => ExtensionRequestValue::class,
29 ];
30
31 /**
32 * Initialize from attribute values.
33 *
34 * @param AttributeValue ...$values List of attribute values
35 */
36 public static function fromAttributeValues(AttributeValue ...$values): static
37 {
38 return static::create(...array_map(static fn (AttributeValue $value) => $value->toAttribute(), $values));
39 }
40
41 /**
42 * Check whether extension request attribute is present.
43 */
44 public function hasExtensionRequest(): bool
45 {
46 return $this->has(ExtensionRequestValue::OID);
47 }
48
49 /**
50 * Get extension request attribute value.
51 */
52 public function extensionRequest(): ExtensionRequestValue
53 {
54 if (! $this->hasExtensionRequest()) {
55 throw new LogicException('No extension request attribute.');
56 }
57 return $this->firstOf(ExtensionRequestValue::OID)->first();
58 }
59
60 protected static function _castAttributeValues(Attribute $attribute): Attribute
61 {
62 $oid = $attribute->oid();
63 if (isset(self::MAP_OID_TO_CLASS[$oid])) {
64 return $attribute->castValues(self::MAP_OID_TO_CLASS[$oid]);
65 }
66 return $attribute;
67 }
68 }