UnknownAttributeValue::toASN1()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X501\ASN1\AttributeValue;
6
7
use Sop\ASN1\Element;
8
use Sop\X501\DN\DNParser;
9
use Sop\X501\MatchingRule\BinaryMatch;
10
use Sop\X501\MatchingRule\MatchingRule;
11
use Sop\X501\StringPrep\TranscodeStep;
12
13
/**
14
 * Class to hold ASN.1 structure of an unimplemented attribute value.
15
 */
16
class UnknownAttributeValue extends AttributeValue
17
{
18
    /**
19
     * ASN.1 element.
20
     *
21
     * @var Element
22
     */
23
    protected $_element;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param string  $oid
29
     * @param Element $el
30
     */
31 6
    public function __construct(string $oid, Element $el)
32
    {
33 6
        $this->_oid = $oid;
34 6
        $this->_element = $el;
35 6
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 4
    public function toASN1(): Element
41
    {
42 4
        return $this->_element;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 6
    public function stringValue(): string
49
    {
50
        // if value is encoded as a string type
51 6
        if ($this->_element->isType(Element::TYPE_STRING)) {
52 2
            return $this->_element->asUnspecified()->asString()->string();
53
        }
54
        // return DER encoding as a hexstring (see RFC2253 section 2.4)
55 4
        return '#' . bin2hex($this->_element->toDER());
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function equalityMatchingRule(): MatchingRule
62
    {
63 2
        return new BinaryMatch();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 3
    public function rfc2253String(): string
70
    {
71 3
        $str = $this->_transcodedString();
72
        // if value has a string representation
73 3
        if ($this->_element->isType(Element::TYPE_STRING)) {
74 1
            $str = DNParser::escapeString($str);
75
        }
76 3
        return $str;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 4
    protected function _transcodedString(): string
83
    {
84
        // if transcoding is defined for the value type
85 4
        if (TranscodeStep::isTypeSupported($this->_element->tag())) {
86 2
            $step = new TranscodeStep($this->_element->tag());
87 2
            return $step->apply($this->stringValue());
88
        }
89 2
        return $this->stringValue();
90
    }
91
}
92