Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy;
6
7 use LogicException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
10 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
11
12 /**
13 * Implements *UserNotice* ASN.1 type used by 'Certificate Policies' certificate extension.
14 *
15 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.4
16 */
17 final class UserNoticeQualifier extends PolicyQualifierInfo
18 {
19 private function __construct(
20 private readonly ?DisplayText $text,
21 private readonly ?NoticeReference $ref
22 ) {
23 parent::__construct(self::OID_UNOTICE);
24 }
25
26 public static function create(?DisplayText $text = null, ?NoticeReference $ref = null): self
27 {
28 return new self($text, $ref);
29 }
30
31 /**
32 * @return self
33 */
34 public static function fromQualifierASN1(UnspecifiedType $el): PolicyQualifierInfo
35 {
36 $seq = $el->asSequence();
37 $ref = null;
38 $text = null;
39 $idx = 0;
40 if ($seq->has($idx, Element::TYPE_SEQUENCE)) {
41 $ref = NoticeReference::fromASN1($seq->at($idx++)->asSequence());
42 }
43 if ($seq->has($idx, Element::TYPE_STRING)) {
44 $text = DisplayText::fromASN1($seq->at($idx)->asString());
45 }
46 return self::create($text, $ref);
47 }
48
49 /**
50 * Whether explicit text is present.
51 */
52 public function hasExplicitText(): bool
53 {
54 return isset($this->text);
55 }
56
57 /**
58 * Get explicit text.
59 */
60 public function explicitText(): DisplayText
61 {
62 if (! $this->hasExplicitText()) {
63 throw new LogicException('explicitText not set.');
64 }
65 return $this->text;
66 }
67
68 /**
69 * Whether notice reference is present.
70 */
71 public function hasNoticeRef(): bool
72 {
73 return isset($this->ref);
74 }
75
76 /**
77 * Get notice reference.
78 */
79 public function noticeRef(): NoticeReference
80 {
81 if (! $this->hasNoticeRef()) {
82 throw new LogicException('noticeRef not set.');
83 }
84 return $this->ref;
85 }
86
87 protected function qualifierASN1(): Element
88 {
89 $elements = [];
90 if (isset($this->ref)) {
91 $elements[] = $this->ref->toASN1();
92 }
93 if (isset($this->text)) {
94 $elements[] = $this->text->toASN1();
95 }
96 return Sequence::create(...$elements);
97 }
98 }