Passed
Push — master ( 38ee16...16673d )
by Tim
02:01
created

Manifest::setReferences()   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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
12
/**
13
 * Class representing a ds:Manifest element.
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
final class Manifest extends AbstractDsElement
18
{
19
    /** @var \SimpleSAML\XMLSecurity\XML\ds\Reference[] */
20
    protected array $references;
21
22
    /** @var string|null $Id */
23
    protected ?string $Id;
24
25
26
    /**
27
     * Initialize a ds:Manifest
28
     *
29
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
30
     * @param string|null $Id
31
     */
32
    public function __construct(
33
        array $references,
34
        ?string $Id = null
35
    ) {
36
        $this->setReferences($references);
37
        $this->setId($Id);
38
    }
39
40
41
    /**
42
     * @return \SimpleSAML\XMLSecurity\XML\ds\Reference[]
43
     */
44
    public function getReferences(): array
45
    {
46
        return $this->references;
47
    }
48
49
50
    /**
51
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
52
     */
53
    protected function setReferences(array $references): void
54
    {
55
        Assert::allIsInstanceOf($references, Reference::class);
56
        $this->references = $references;
57
    }
58
59
60
    /**
61
     * @return string|null
62
     */
63
    public function getId(): ?string
64
    {
65
        return $this->Id;
66
    }
67
68
69
    /**
70
     * @param string|null $Id
71
     */
72
    private function setId(?string $Id): void
73
    {
74
        Assert::nullOrValidNCName($Id);
75
        $this->Id = $Id;
76
    }
77
78
79
    /**
80
     * Convert XML into a Manifest element
81
     *
82
     * @param \DOMElement $xml The XML element we should load
83
     * @return static
84
     *
85
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
86
     *   If the qualified name of the supplied element is wrong
87
     */
88
    public static function fromXML(DOMElement $xml): static
89
    {
90
        Assert::same($xml->localName, 'Manifest', InvalidDOMElementException::class);
91
        Assert::same($xml->namespaceURI, Manifest::NS, InvalidDOMElementException::class);
92
93
        $Id = self::getAttribute($xml, 'Id', null);
94
95
        /** @psalm-var \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references */
96
        $references = Reference::getChildrenOfClass($xml);
97
        Assert::minCount(
98
            $references,
99
            1,
100
            'A <ds:Manifest> must contain at least one <ds:Reference>.',
101
            MissingElementException::class,
102
        );
103
104
        return new static(
105
            $references,
106
            $Id,
107
        );
108
    }
109
110
111
    /**
112
     * Convert this Manifest element to XML.
113
     *
114
     * @param \DOMElement|null $parent The element we should append this Manifest element to.
115
     * @return \DOMElement
116
     */
117
    public function toXML(DOMElement $parent = null): DOMElement
118
    {
119
        $e = $this->instantiateParentElement($parent);
120
121
        if ($this->Id !== null) {
122
            $e->setAttribute('Id', $this->Id);
123
        }
124
125
        foreach ($this->references as $reference) {
126
            $reference->toXML($e);
127
        }
128
129
        return $e;
130
    }
131
}
132