Passed
Pull Request — master (#64)
by Tim
02:10
created

DEREncodedKeyValue::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\dsig11;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Base64ElementTrait;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
17
/**
18
 * Class representing a dsig11:DEREncodedKeyValue element.
19
 *
20
 * @package simplesaml/xml-security
21
 */
22
final class DEREncodedKeyValue extends AbstractDsig11Element implements SchemaValidatableElementInterface
23
{
24
    use Base64ElementTrait;
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\X...ig11\DEREncodedKeyValue: $message, $line
Loading history...
26
27
28
    /**
29
     * Initialize a DEREncodedKeyValue element.
30
     *
31
     * @param string $value
32
     * @param string|null $Id
33
     */
34
    public function __construct(
35
        string $value,
36
        protected ?string $Id = null,
37
    ) {
38
        Assert::validNCName($Id, SchemaViolationException::class);
39
40
        $this->setContent($value);
41
    }
42
43
44
    /**
45
     * Collect the value of the Id-property
46
     *
47
     * @return string|null
48
     */
49
    public function getId(): ?string
50
    {
51
        return $this->Id;
52
    }
53
54
55
    /**
56
     * Convert XML into a DEREncodedKeyValue
57
     *
58
     * @param \DOMElement $xml The XML element we should load
59
     * @return static
60
     *
61
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
62
     *   If the qualified name of the supplied element is wrong
63
     */
64
    public static function fromXML(DOMElement $xml): static
65
    {
66
        Assert::same($xml->localName, static::LOCALNAME, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\XMLSecurity\X...odedKeyValue::LOCALNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
68
69
        return new static(
70
            $xml->textContent,
71
            self::getOptionalAttribute($xml, 'Id', null),
72
        );
73
    }
74
75
76
    /**
77
     * Convert this DEREncodedKeyValue element to XML.
78
     *
79
     * @param \DOMElement|null $parent The element we should append this DEREncodedKeyValue element to.
80
     * @return \DOMElement
81
     */
82
    public function toXML(?DOMElement $parent = null): DOMElement
83
    {
84
        $e = $this->instantiateParentElement($parent);
85
        $e->textContent = $this->getContent();
86
87
        if ($this->getId() !== null) {
88
            $e->setAttribute('Id', $this->getId());
89
        }
90
91
        return $e;
92
    }
93
}
94