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

SignatureValue   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 70
rs 10
wmc 5

5 Methods

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