UserNoticeQualifier::hasNoticeRef()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension\CertificatePolicy;
6
7
use ASN1\Element;
8
use ASN1\Type\UnspecifiedType;
9
use ASN1\Type\Constructed\Sequence;
10
11
/**
12
 * Implements <i>UserNotice</i> ASN.1 type used by
13
 * 'Certificate Policies' certificate extension.
14
 *
15
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.4
16
 */
17
class UserNoticeQualifier extends PolicyQualifierInfo
18
{
19
    /**
20
     * Explicit notice text.
21
     *
22
     * @var DisplayText $_text
23
     */
24
    protected $_text;
25
    
26
    /**
27
     * Notice reference.
28
     *
29
     * @var NoticeReference $_ref
30
     */
31
    protected $_ref;
32
    
33
    /**
34
     * Constructor.
35
     *
36
     * @param DisplayText|null $text
37
     * @param NoticeReference|null $ref
38
     */
39 16
    public function __construct(DisplayText $text = null, NoticeReference $ref = null)
40
    {
41 16
        $this->_oid = self::OID_UNOTICE;
42 16
        $this->_text = $text;
43 16
        $this->_ref = $ref;
44 16
    }
45
    
46
    /**
47
     *
48
     * @param UnspecifiedType $el
49
     * @return self
50
     */
51 11
    public static function fromQualifierASN1(UnspecifiedType $el): self
52
    {
53 11
        $seq = $el->asSequence();
54 11
        $ref = null;
55 11
        $text = null;
56 11
        $idx = 0;
57 11
        if ($seq->has($idx, Element::TYPE_SEQUENCE)) {
58 10
            $ref = NoticeReference::fromASN1($seq->at($idx++)->asSequence());
59
        }
60 11
        if ($seq->has($idx, Element::TYPE_STRING)) {
61 11
            $text = DisplayText::fromASN1($seq->at($idx)->asString());
62
        }
63 11
        return new self($text, $ref);
64
    }
65
    
66
    /**
67
     * Whether explicit text is present.
68
     *
69
     * @return bool
70
     */
71 5
    public function hasExplicitText(): bool
72
    {
73 5
        return isset($this->_text);
74
    }
75
    
76
    /**
77
     * Get explicit text.
78
     *
79
     * @return DisplayText
80
     */
81 5
    public function explicitText(): DisplayText
82
    {
83 5
        if (!$this->hasExplicitText()) {
84 1
            throw new \LogicException("explicitText not set.");
85
        }
86 4
        return $this->_text;
87
    }
88
    
89
    /**
90
     * Whether notice reference is present.
91
     *
92
     * @return bool
93
     */
94 4
    public function hasNoticeRef(): bool
95
    {
96 4
        return isset($this->_ref);
97
    }
98
    
99
    /**
100
     * Get notice reference.
101
     *
102
     * @throws \RuntimeException
103
     * @return NoticeReference
104
     */
105 4
    public function noticeRef(): NoticeReference
106
    {
107 4
        if (!$this->hasNoticeRef()) {
108 1
            throw new \LogicException("noticeRef not set.");
109
        }
110 3
        return $this->_ref;
111
    }
112
    
113
    /**
114
     *
115
     * {@inheritdoc}
116
     * @return Sequence
117
     */
118 18
    protected function _qualifierASN1(): Sequence
119
    {
120 18
        $elements = array();
121 18
        if (isset($this->_ref)) {
122 16
            $elements[] = $this->_ref->toASN1();
123
        }
124 18
        if (isset($this->_text)) {
125 18
            $elements[] = $this->_text->toASN1();
126
        }
127 18
        return new Sequence(...$elements);
128
    }
129
}
130