PrintableString::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ocsp\Asn1\Element;
4
5
use Ocsp\Asn1\Encoder;
6
use Ocsp\Asn1\TaggableElement;
7
use Ocsp\Asn1\TaggableElementTrait;
8
use Ocsp\Asn1\UniversalTagID;
9
use Ocsp\Exception\InvalidAsn1Value;
10
11
/**
12
 * ASN.1 element: PrintableString.
13
 */
14
class PrintableString implements TaggableElement
15
{
16
    use TaggableElementTrait;
17
18
    /**
19
     * The value of the element.
20
     *
21
     * @var string
22
     */
23
    private $value;
24
25
    /**
26
     * Create a new instance.
27
     *
28
     * @param string $value the value of the element
29
     *
30
     * @return static
31
     */
32 3
    public static function create($value)
33
    {
34 3
        $result = new static();
35
36 3
        return $result->setValue($value);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @see \Ocsp\Asn1\Element::getClass()
43
     */
44 2
    public function getClass()
45
    {
46 2
        return static::CLASS_UNIVERSAL;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @see \Ocsp\Asn1\Element::getTypeID()
53
     */
54 2
    public function getTypeID()
55
    {
56 2
        return UniversalTagID::PRINTABLESTRING;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @see \Ocsp\Asn1\Element::isConstructed()
63
     */
64 2
    public function isConstructed()
65
    {
66 2
        return false;
67
    }
68
69
    /**
70
     * Get the value of the element.
71
     *
72
     * @return string
73
     */
74 2
    public function getValue()
75
    {
76 2
        return $this->value;
77
    }
78
79
    /**
80
     * Update the value of the element.
81
     *
82
     * @param string $value
83
     *
84
     * @throws \Ocsp\Exception\InvalidAsn1Value
85
     *
86
     * @return $this
87
     */
88 3
    public function setValue($value)
89
    {
90 3
        $value = (string) $value;
91 3
        if (!preg_match('/^[A-Za-z0-9 \'()+,\-.\/:=?]*$/', $value)) {
92
            throw InvalidAsn1Value::create('Invalid ASN.1 PrintableString value');
93
        }
94 3
        $this->value = (string) $value;
95
96 3
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     *
102
     * @see \Ocsp\Asn1\Element::getEncodedValue()
103
     */
104 2
    public function getEncodedValue(Encoder $encoder)
105
    {
106 2
        return $encoder->encodePrintableString($this->getValue());
107
    }
108
}
109