SignatureProperty::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\Exception\InvalidDOMElementException;
9
use SimpleSAML\XML\Exception\MissingElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XML\XsNamespace as NS;
15
use SimpleSAML\XMLSecurity\Assert\Assert;
16
17
/**
18
 * Class representing a ds:SignatureProperty element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class SignatureProperty extends AbstractDsElement implements SchemaValidatableElementInterface
23
{
24
    use ExtendableElementTrait;
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\SignatureProperty: $message, $line
Loading history...
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
29
30
31
    /**
32
     * Initialize a ds:SignatureProperty
33
     *
34
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
35
     * @param string $Target
36
     * @param string|null $Id
37
     */
38
    public function __construct(
39
        array $elements,
40
        protected string $Target,
41
        protected ?string $Id = null,
42
    ) {
43
        Assert::validURI($Target, SchemaViolationException::class); // Covers the empty string
44
        Assert::nullOrValidNCName($Id);
45
46
        $this->setElements($elements);
47
    }
48
49
50
    /**
51
     * @return string
52
     */
53
    public function getTarget(): string
54
    {
55
        return $this->Target;
56
    }
57
58
59
    /**
60
     * @return string|null
61
     */
62
    public function getId(): ?string
63
    {
64
        return $this->Id;
65
    }
66
67
68
    /**
69
     * Convert XML into a SignatureProperty element
70
     *
71
     * @param \DOMElement $xml The XML element we should load
72
     * @return static
73
     *
74
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
75
     *   If the qualified name of the supplied element is wrong
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'SignatureProperty', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, SignatureProperty::NS, InvalidDOMElementException::class);
81
82
        $Target = self::getAttribute($xml, 'Target');
83
        $Id = self::getOptionalAttribute($xml, 'Id', null);
84
85
        $children = self::getChildElementsFromXML($xml);
86
        Assert::minCount(
87
            $children,
88
            1,
89
            'A <ds:SignatureProperty> must contain at least one element.',
90
            MissingElementException::class,
91
        );
92
93
        return new static(
94
            $children,
95
            $Target,
96
            $Id,
97
        );
98
    }
99
100
101
    /**
102
     * Convert this SignatureProperty element to XML.
103
     *
104
     * @param \DOMElement|null $parent The element we should append this SignatureProperty element to.
105
     * @return \DOMElement
106
     */
107
    public function toXML(?DOMElement $parent = null): DOMElement
108
    {
109
        $e = $this->instantiateParentElement($parent);
110
        $e->setAttribute('Target', $this->getTarget());
111
112
        if ($this->getId() !== null) {
113
            $e->setAttribute('Id', $this->getId());
114
        }
115
116
        foreach ($this->getElements() as $element) {
117
            $element->toXML($e);
118
        }
119
120
        return $e;
121
    }
122
}
123