Passed
Branch php72 (a3eac9)
by Joni
03:37
created

PrintableStringValue::fromASN1()   A

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 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X501\ASN1\AttributeValue\Feature;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Primitive\PrintableString;
9
use Sop\ASN1\Type\UnspecifiedType;
10
use Sop\X501\ASN1\AttributeValue\AttributeValue;
11
use Sop\X501\DN\DNParser;
12
use Sop\X501\MatchingRule\CaseIgnoreMatch;
13
use Sop\X501\MatchingRule\MatchingRule;
14
15
/**
16
 * Base class for attribute values having <i>PrintableString</i> syntax.
17
 */
18
abstract class PrintableStringValue extends AttributeValue
19
{
20
    /**
21
     * String value.
22
     *
23
     * @var string
24
     */
25
    protected $_string;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param string $value String value
31
     */
32 6
    public function __construct(string $value)
33
    {
34 6
        $this->_string = $value;
35 6
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @return self
41
     */
42 4
    public static function fromASN1(UnspecifiedType $el): AttributeValue
43
    {
44 4
        return new static($el->asPrintableString()->string());
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function toASN1(): Element
51
    {
52 2
        return new PrintableString($this->_string);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function stringValue(): string
59
    {
60 1
        return $this->_string;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function equalityMatchingRule(): MatchingRule
67
    {
68
        // default to caseIgnoreMatch
69 1
        return new CaseIgnoreMatch(Element::TYPE_PRINTABLE_STRING);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function rfc2253String(): string
76
    {
77 1
        return DNParser::escapeString($this->_transcodedString());
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    protected function _transcodedString(): string
84
    {
85
        // PrintableString maps directly to UTF-8
86 1
        return $this->_string;
87
    }
88
}
89