Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/SubjectDirectoryAttributesExtension.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\ASN1\Element;
13 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
14 use SpomkyLabs\Pki\X501\ASN1\Attribute;
15 use SpomkyLabs\Pki\X501\ASN1\Collection\SequenceOfAttributes;
16 use UnexpectedValueException;
17
18 /**
19 * Implements 'Subject Directory Attributes' certificate extension.
20 *
21 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.8
22 */
23 final class SubjectDirectoryAttributesExtension extends Extension implements Countable, IteratorAggregate
24 {
25 /**
26 * Attributes.
27 */
28 private readonly SequenceOfAttributes $attributes;
29
30 /**
31 * @param Attribute ...$attribs One or more Attribute objects
32 */
33 private function __construct(bool $critical, Attribute ...$attribs)
34 {
35 parent::__construct(self::OID_SUBJECT_DIRECTORY_ATTRIBUTES, $critical);
36 $this->attributes = SequenceOfAttributes::create(...$attribs);
37 }
38
39 public static function create(bool $critical, Attribute ...$attribs): self
40 {
41 return new self($critical, ...$attribs);
42 }
43
44 /**
45 * Check whether attribute is present.
46 *
47 * @param string $name OID or attribute name
48 */
49 public function has(string $name): bool
50 {
51 return $this->attributes->has($name);
52 }
53
54 /**
55 * Get first attribute by OID or attribute name.
56 *
57 * @param string $name OID or attribute name
58 */
59 public function firstOf(string $name): Attribute
60 {
61 return $this->attributes->firstOf($name);
62 }
63
64 /**
65 * Get all attributes of given name.
66 *
67 * @param string $name OID or attribute name
68 *
69 * @return Attribute[]
70 */
71 public function allOf(string $name): array
72 {
73 return $this->attributes->allOf($name);
74 }
75
76 /**
77 * Get all attributes.
78 *
79 * @return Attribute[]
80 */
81 public function all(): array
82 {
83 return $this->attributes->all();
84 }
85
86 /**
87 * Get number of attributes.
88 */
89 public function count(): int
90 {
91 return count($this->attributes);
92 }
93
94 /**
95 * Get iterator for attributes.
96 *
97 * @return ArrayIterator|Attribute[]
98 */
99 public function getIterator(): ArrayIterator
100 {
101 return $this->attributes->getIterator();
102 }
103
104 protected static function fromDER(string $data, bool $critical): static
105 {
106 $attribs = SequenceOfAttributes::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
107 if (count($attribs) === 0) {
108 throw new UnexpectedValueException('SubjectDirectoryAttributes must have at least one Attribute.');
109 }
110 return self::create($critical, ...$attribs->all());
111 }
112
113 protected function valueASN1(): Element
114 {
115 if (count($this->attributes) === 0) {
116 throw new LogicException('No attributes');
117 }
118 return $this->attributes->toASN1();
119 }
120 }