IetfAttrValue::fromOID()   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 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\AttributeCertificate\Attribute;
6
7
use ASN1\Element;
8
use ASN1\Type\UnspecifiedType;
9
use ASN1\Type\Primitive\ObjectIdentifier;
10
use ASN1\Type\Primitive\OctetString;
11
use ASN1\Type\Primitive\UTF8String;
12
13
/**
14
 * Implements <i>IetfAttrSyntax.values</i> ASN.1 CHOICE type.
15
 *
16
 * @link https://tools.ietf.org/html/rfc5755#section-4.4
17
 */
18
class IetfAttrValue
19
{
20
    /**
21
     * Element type tag.
22
     *
23
     * @var int $_type
24
     */
25
    protected $_type;
26
    
27
    /**
28
     * Value.
29
     *
30
     * @var string $_value
31
     */
32
    protected $_value;
33
    
34
    /**
35
     * Constructor.
36
     *
37
     * @param string $value
38
     * @param int $type
39
     */
40 12
    public function __construct(string $value, int $type)
41
    {
42 12
        $this->_type = $type;
43 12
        $this->_value = $value;
44 12
    }
45
    
46
    /**
47
     * Initialize from ASN.1.
48
     *
49
     * @param UnspecifiedType $el
50
     * @throws \UnexpectedValueException
51
     * @return self
52
     */
53 5
    public static function fromASN1(UnspecifiedType $el): self
54
    {
55 5
        switch ($el->tag()) {
56 5
            case Element::TYPE_OCTET_STRING:
57 5
            case Element::TYPE_UTF8_STRING:
58 4
                return new self($el->asString()->string(), $el->tag());
59 2
            case Element::TYPE_OBJECT_IDENTIFIER:
60 1
                return new self($el->asObjectIdentifier()->oid(), $el->tag());
61
        }
62 1
        throw new \UnexpectedValueException(
63 1
            "Type " . Element::tagToName($el->tag()) . " not supported.");
64
    }
65
    
66
    /**
67
     * Initialize from octet string.
68
     *
69
     * @param string $octets
70
     * @return self
71
     */
72 2
    public static function fromOctets(string $octets): self
73
    {
74 2
        return new self($octets, Element::TYPE_OCTET_STRING);
75
    }
76
    
77
    /**
78
     * Initialize from UTF-8 string.
79
     *
80
     * @param string $str
81
     * @return self
82
     */
83 5
    public static function fromString(string $str): self
84
    {
85 5
        return new self($str, Element::TYPE_UTF8_STRING);
86
    }
87
    
88
    /**
89
     * Initialize from OID.
90
     *
91
     * @param string $oid
92
     * @return self
93
     */
94 2
    public static function fromOID(string $oid): self
95
    {
96 2
        return new self($oid, Element::TYPE_OBJECT_IDENTIFIER);
97
    }
98
    
99
    /**
100
     * Get type tag.
101
     *
102
     * @return int
103
     */
104 3
    public function type(): int
105
    {
106 3
        return $this->_type;
107
    }
108
    
109
    /**
110
     * Whether value type is octets.
111
     *
112
     * @return bool
113
     */
114 1
    public function isOctets(): bool
115
    {
116 1
        return $this->_type === Element::TYPE_OCTET_STRING;
117
    }
118
    
119
    /**
120
     * Whether value type is OID.
121
     *
122
     * @return bool
123
     */
124 1
    public function isOID(): bool
125
    {
126 1
        return $this->_type === Element::TYPE_OBJECT_IDENTIFIER;
127
    }
128
    
129
    /**
130
     * Whether value type is string.
131
     *
132
     * @return bool
133
     */
134 1
    public function isString(): bool
135
    {
136 1
        return $this->_type === Element::TYPE_UTF8_STRING;
137
    }
138
    
139
    /**
140
     * Get value.
141
     *
142
     * @return string
143
     */
144 1
    public function value(): string
145
    {
146 1
        return $this->_value;
147
    }
148
    
149
    /**
150
     * Generate ASN.1 structure.
151
     *
152
     * @throws \LogicException
153
     * @return Element
154
     */
155 6
    public function toASN1(): Element
156
    {
157 6
        switch ($this->_type) {
158 6
            case Element::TYPE_OCTET_STRING:
159 1
                return new OctetString($this->_value);
160 6
            case Element::TYPE_UTF8_STRING:
161 5
                return new UTF8String($this->_value);
162 2
            case Element::TYPE_OBJECT_IDENTIFIER:
163 1
                return new ObjectIdentifier($this->_value);
164
        }
165 1
        throw new \LogicException(
166 1
            "Type " . Element::tagToName($this->_type) . " not supported.");
167
    }
168
    
169
    /**
170
     *
171
     * @return string
172
     */
173 4
    public function __toString()
174
    {
175 4
        return $this->_value;
176
    }
177
}
178