Passed
Pull Request — master (#39)
by Tim
06:59 queued 04:40
created

SignatureValue::toXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
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
use SimpleSAML\XML\XMLBase64ElementTrait;
11
12
/**
13
 * Class representing a ds:SignatureValue element.
14
 *
15
 * @package simplesaml/xml-security
16
 */
17
final class SignatureValue extends AbstractDsElement
18
{
19
    use XMLBase64ElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\XMLBase64ElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\SignatureValue: $localName, $namespaceURI
Loading history...
20
21
    /** @var string|null */
22
    protected ?string $Id;
23
24
25
    /**
26
     * @param string $content
27
     * @param string|null $id
28
     */
29
    public function __construct(string $content, ?string $id = null)
30
    {
31
        $this->setContent($content);
32
        $this->setId($id);
33
    }
34
35
36
    /**
37
     * Get the Id used for this signature value.
38
     *
39
     * @return string|null
40
     */
41
    public function getId(): ?string
42
    {
43
        return $this->Id;
44
    }
45
46
47
    /**
48
     * Set the Id used for this signature value.
49
     *
50
     * @param string|null $Id
51
     */
52
    protected function setId(?string $Id): void
53
    {
54
        Assert::nullOrValidNCName($Id);
55
        $this->Id = $Id;
56
    }
57
58
59
    /**
60
     * Convert XML into a SignatureValue element
61
     *
62
     * @param \DOMElement $xml
63
     * @return \SimpleSAML\XML\AbstractXMLElement
64
     *
65
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
66
     *   If the qualified name of the supplied element is wrong
67
     */
68
    public static function fromXML(DOMElement $xml): self
69
    {
70
        Assert::same($xml->localName, 'SignatureValue', InvalidDOMElementException::class);
71
        Assert::same($xml->namespaceURI, SignatureValue::NS, InvalidDOMElementException::class);
72
73
        $Id = self::getAttribute($xml, 'Id', null);
74
75
        return new self($xml->textContent, $Id);
76
    }
77
78
79
    /**
80
     * Convert this SignatureValue element to XML.
81
     *
82
     * @param \DOMElement|null $parent The element we should append this SignatureValue element to.
83
     * @return \DOMElement
84
     */
85
    public function toXML(DOMElement $parent = null): DOMElement
86
    {
87
        $e = $this->instantiateParentElement($parent);
88
        $e->textContent = $this->getContent();
89
90
        if ($this->Id !== null) {
91
            $e->setAttribute('Id', $this->Id);
92
        }
93
94
        return $e;
95
    }
96
}
97