Passed
Pull Request — master (#41)
by Tim
01:59
created

SignatureProperties::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
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\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
    /** @var \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[] $signatureProperty */
25
    protected array $signatureProperty;
26
27
    /** @var string|null $Id */
28
    protected ?string $Id;
29
30
31
    /**
32
     * Initialize a ds:SignatureProperties
33
     *
34
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[] $signatureProperty
35
     * @param string $Target
36
     * @param string|null $Id
37
     */
38
    public function __construct(array $signatureProperty, ?string $Id = null) {
39
        $this->setSignatureProperty($signatureProperty);
40
        $this->setId($Id);
41
    }
42
43
44
    /**
45
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[]
46
     */
47
    public function getSignatureProperty(): array
48
    {
49
        return $this->signatureProperty;
50
    }
51
52
53
    /**
54
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[] $signatureProperty
55
     */
56
    protected function setSignatureProperty(array $signatureProperty): void
57
    {
58
        Assert::allIsInstanceOf($signatureProperty, SignatureProperty::class, SchemaViolationException::class);
59
        $this->signatureProperty = $signatureProperty;
60
    }
61
62
63
    /**
64
     * @return string|null
65
     */
66
    public function getId(): ?string
67
    {
68
        return $this->Id;
69
    }
70
71
72
    /**
73
     * @param string|null $Id
74
     */
75
    private function setId(?string $Id): void
76
    {
77
        Assert::nullOrValidNCName($Id);
78
        $this->Id = $Id;
79
    }
80
81
82
    /**
83
     * Convert XML into a SignatureProperties element
84
     *
85
     * @param \DOMElement $xml The XML element we should load
86
     * @return static
87
     *
88
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
89
     *   If the qualified name of the supplied element is wrong
90
     */
91
    public static function fromXML(DOMElement $xml): static
92
    {
93
        Assert::same($xml->localName, 'SignatureProperties', InvalidDOMElementException::class);
94
        Assert::same($xml->namespaceURI, SignatureProperties::NS, InvalidDOMElementException::class);
95
96
        $signatureProperty = SignatureProperty::getChildrenOfClass($xml);
97
        $Id = self::getAttribute($xml, 'Id', null);
98
99
        Assert::minCount(
100
            $signatureProperty,
101
            1,
102
            'A <ds:SignatureProperties> must contain at least one <ds:SignatureProperty>.',
103
            MissingElementException::class,
104
        );
105
106
        return new static(
107
            $signatureProperty,
108
            $Id,
109
        );
110
    }
111
112
113
    /**
114
     * Convert this SignatureProperties element to XML.
115
     *
116
     * @param \DOMElement|null $parent The element we should append this SignatureProperties element to.
117
     * @return \DOMElement
118
     */
119
    public function toXML(DOMElement $parent = null): DOMElement
120
    {
121
        $e = $this->instantiateParentElement($parent);
122
123
        if ($this->Id !== null) {
124
            $e->setAttribute('Id', $this->Id);
125
        }
126
127
        foreach ($this->signatureProperty as $signatureProperty) {
128
            $signatureProperty->toXML($e);
129
        }
130
131
        return $e;
132
    }
133
}
134