Passed
Pull Request — master (#15)
by Jaime Pérez
02:21
created

SignatureValue::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
11
/**
12
 * Class representing a ds:SignatureValue element.
13
 *
14
 * @package simplesaml/xml-security
15
 */
16
final class SignatureValue extends AbstractDsElement
17
{
18
    /** @var string */
19
    protected string $value;
20
21
22
    /**
23
     * Initialize a SignatureValue element.
24
     *
25
     * @param string $value
26
     */
27
    public function __construct(string $value) {
28
        $this->setValue($value);
29
    }
30
31
32
    /**
33
     * Get the signature value.
34
     *
35
     * @return string
36
     */
37
    public function getValue(): string
38
    {
39
        return $this->value;
40
    }
41
42
43
    /**
44
     * Set the value.
45
     *
46
     * @param string $value
47
     */
48
    private function setValue(string $value): void
49
    {
50
        Assert::notEmpty($value, 'SignatureValue cannot be empty');
51
        Assert::stringPlausibleBase64($value, 'SignatureValue is not a valid Base64 encoded string');
52
        $this->value = $value;
53
    }
54
55
56
    /**
57
     * Convert XML into a SignatureValue
58
     *
59
     * @param DOMElement $xml The XML element we should load
60
     * @return self
61
     *
62
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException If the qualified name of the supplied element is
63
     * wrong.
64
     */
65
    public static function fromXML(DOMElement $xml): object
66
    {
67
        Assert::same($xml->localName, 'SignatureValue', InvalidDOMElementException::class);
68
        Assert::same($xml->namespaceURI, SignatureValue::NS, InvalidDOMElementException::class);
69
70
        return new self($xml->textContent);
71
    }
72
73
74
    /**
75
     * Convert this SignatureValue to XML.
76
     *
77
     * @param DOMElement|null $parent The element we should append this SignatureValue element to.
78
     * @return DOMElement
79
     */
80
    public function toXML(DOMElement $parent = null): \DOMElement
81
    {
82
        $e = $this->instantiateParentElement($parent);
83
        $e->textContent = $this->value;
84
85
        return $e;
86
    }
87
}