Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

SignatureProperties::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 18
rs 9.9
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\Chunk;
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\SchemaViolationException;
14
use SimpleSAML\XML\ExtendableElementTrait;
15
use SimpleSAML\XML\SerializableElementInterface;
16
17
/**
18
 * Class representing a ds:SignatureProperties element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class SignatureProperties extends AbstractDsElement
23
{
24
    /**
25
     * Initialize a ds:SignatureProperties
26
     *
27
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[] $signatureProperty
28
     * @param string $Target
29
     * @param string|null $Id
30
     */
31
    public function __construct(
32
        protected array $signatureProperty,
33
        protected ?string $Id = null,
34
    ) {
35
        Assert::allIsInstanceOf($signatureProperty, SignatureProperty::class, SchemaViolationException::class);
36
        Assert::nullOrValidNCName($Id);
37
    }
38
39
40
    /**
41
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[]
42
     */
43
    public function getSignatureProperty(): array
44
    {
45
        return $this->signatureProperty;
46
    }
47
48
49
    /**
50
     * @return string|null
51
     */
52
    public function getId(): ?string
53
    {
54
        return $this->Id;
55
    }
56
57
58
    /**
59
     * Convert XML into a SignatureProperties element
60
     *
61
     * @param \DOMElement $xml The XML element we should load
62
     * @return static
63
     *
64
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
65
     *   If the qualified name of the supplied element is wrong
66
     */
67
    public static function fromXML(DOMElement $xml): static
68
    {
69
        Assert::same($xml->localName, 'SignatureProperties', InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, SignatureProperties::NS, InvalidDOMElementException::class);
71
72
        $signatureProperty = SignatureProperty::getChildrenOfClass($xml);
73
        $Id = self::getAttribute($xml, 'Id', null);
74
75
        Assert::minCount(
76
            $signatureProperty,
77
            1,
78
            'A <ds:SignatureProperties> must contain at least one <ds:SignatureProperty>.',
79
            MissingElementException::class,
80
        );
81
82
        return new static(
83
            $signatureProperty,
84
            $Id,
85
        );
86
    }
87
88
89
    /**
90
     * Convert this SignatureProperties element to XML.
91
     *
92
     * @param \DOMElement|null $parent The element we should append this SignatureProperties element to.
93
     * @return \DOMElement
94
     */
95
    public function toXML(DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
99
        if ($this->getId() !== null) {
100
            $e->setAttribute('Id', $this->getId());
101
        }
102
103
        foreach ($this->getSignatureProperty() as $signatureProperty) {
104
            $signatureProperty->toXML($e);
105
        }
106
107
        return $e;
108
    }
109
}
110