DisplayText::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 2
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\StringType;
9
use ASN1\Type\Primitive\BMPString;
10
use ASN1\Type\Primitive\IA5String;
11
use ASN1\Type\Primitive\UTF8String;
12
use ASN1\Type\Primitive\VisibleString;
13
14
/**
15
 * Implements <i>DisplayText</i> ASN.1 CHOICE type used by
16
 * 'Certificate Policies' certificate extension.
17
 *
18
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.4
19
 */
20
class DisplayText
21
{
22
    /**
23
     * Text.
24
     *
25
     * @var string $_text
26
     */
27
    protected $_text;
28
    
29
    /**
30
     * Element tag.
31
     *
32
     * @var int $_tag
33
     */
34
    protected $_tag;
35
    
36
    /**
37
     * Constructor.
38
     *
39
     * @param string $text
40
     * @param int $tag
41
     */
42 24
    public function __construct(string $text, int $tag)
43
    {
44 24
        $this->_text = $text;
45 24
        $this->_tag = $tag;
46 24
    }
47
    
48
    /**
49
     * Initialize from ASN.1.
50
     *
51
     * @param StringType $el
52
     * @return self
53
     */
54 13
    public static function fromASN1(StringType $el): self
55
    {
56 13
        return new self($el->string(), $el->tag());
57
    }
58
    
59
    /**
60
     * Initialize from a UTF-8 string.
61
     *
62
     * @param string $str
63
     * @return self
64
     */
65 6
    public static function fromString(string $str): self
66
    {
67 6
        return new self($str, Element::TYPE_UTF8_STRING);
68
    }
69
    
70
    /**
71
     * Get the text.
72
     *
73
     * @return string
74
     */
75 8
    public function string(): string
76
    {
77 8
        return $this->_text;
78
    }
79
    
80
    /**
81
     * Generate ASN.1 element.
82
     *
83
     * @throws \UnexpectedValueException
84
     * @return StringType
85
     */
86 25
    public function toASN1(): StringType
87
    {
88 25
        switch ($this->_tag) {
89 25
            case Element::TYPE_IA5_STRING:
90 1
                return new IA5String($this->_text);
91 24
            case Element::TYPE_VISIBLE_STRING:
92 15
                return new VisibleString($this->_text);
93 9
            case Element::TYPE_BMP_STRING:
94 1
                return new BMPString($this->_text);
95 8
            case Element::TYPE_UTF8_STRING:
96 7
                return new UTF8String($this->_text);
97
            default:
98 1
                throw new \UnexpectedValueException(
99 1
                    "Type " . Element::tagToName($this->_tag) . " not supported.");
100
        }
101
    }
102
    
103
    /**
104
     *
105
     * @return string
106
     */
107 3
    public function __toString()
108
    {
109 3
        return $this->string();
110
    }
111
}
112